forked from ljacobsson/aws-sdk-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·75 lines (62 loc) · 2.32 KB
/
index.js
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
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env node
import { program } from 'commander';
import { Octokit } from '@octokit/rest';
import * as input from "./src/inputUtil.js";
import * as shell from 'shelljs';
import * as fs from "fs"
import * as os from "os"
import * as path from "path"
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, "package.json")).toString("utf-8"));
program
.version(pkg.version)
.description('AWS SDK v3 CLI');
program
.option('--no-cache', 'Use cached results', true)
.action((cmd) => {
searchReadmeFiles(cmd.cache);
})
.parse(process.argv);
async function searchReadmeFiles(cache) {
if (!process.env.GITHUB_TOKEN) {
console.log("Please set GITHUB_TOKEN environment variable. Create one here https://github.com/settings/tokens");
process.exit(1);
}
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
const cachePath = path.join(os.homedir(), ".aws-sdk-cli", "cache.json");
let choice;
if (fs.existsSync(cachePath) && cache) {
console.log("Using cached results. To refresh, run with --no-cache");
choice = JSON.parse(fs.readFileSync(cachePath).toString("utf-8"));
} else {
const response = await octokit.repos.getContent({
owner: 'aws',
repo: 'aws-sdk-js-v3',
path: '/clients',
});
const files = response.data.filter((item) => item.type === 'dir');
const fileChoices = files.map(async (file) => {
const readme = await octokit.repos.getContent({
owner: 'aws',
repo: 'aws-sdk-js-v3',
path: `/clients/${file.name}/README.md`,
});
const readmeContent = Buffer.from(readme.data.content, 'base64').toString();
return {
name: `@aws-sdk/${file.name}`,
value: { name: `@aws-sdk/${file.name}`, readme: readmeContent },
};
});
choice = await Promise.all(fileChoices);
if (!fs.existsSync(path.dirname(cachePath))) {
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
}
fs.writeFileSync(cachePath, JSON.stringify(choice));
}
const selectedClient = await input.default.autocomplete('Search client:', choice);
const npmCommand = `npm install ${selectedClient.name}`;
console.log(`Running '${npmCommand}'`);
shell.default.exec(npmCommand);
}