-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregions.mjs
More file actions
62 lines (56 loc) 路 1.8 KB
/
Copy pathregions.mjs
File metadata and controls
62 lines (56 loc) 路 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import inquirer from "inquirer";
import inquirerPrompt from "inquirer-autocomplete-prompt";
inquirer.registerPrompt("autocomplete", inquirerPrompt);
import { createSpinner } from "nanospinner";
import { AccountClient, paginateListRegions } from "@aws-sdk/client-account";
class Regions {
static DEFAULTS = ["us-east-1", "us-east-2", "us-west-1", "us-west-2"];
constructor() {
this.default =
process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || "us-east-1";
this.spinner = createSpinner("Loading Regions...");
}
async askFor() {
const source = await this.source();
const { region } = await inquirer.prompt([
{
type: "autocomplete",
name: "region",
message: "Select your AWS Region:",
source: function (_, input) {
input = input || "";
return new Promise((resolve) => {
const results = source.filter((option) =>
option.toLowerCase().includes(input.toLowerCase())
);
resolve(results);
});
},
default: this.default,
},
]);
return region;
}
async source() {
this.spinner.start();
const names = await this.liveRegionNames();
const namesWoDefault = names.filter((region) => region !== this.default);
this.spinner.success({ text: "Loaded Regions" });
return [this.default, ...namesWoDefault];
}
async liveRegionNames() {
const client = new AccountClient({ region: this.default || "us-east-1" });
try {
const regionNames = [];
for await (const page of paginateListRegions({ client }, {})) {
for (const region of page.Regions) {
regionNames.push(region.RegionName);
}
}
return regionNames;
} catch (error) {
return Regions.DEFAULTS;
}
}
}
export default Regions;