Skip to content

Commit 5bf7d35

Browse files
Ajay Narain MathurAjay Narain Mathur
Ajay Narain Mathur
authored and
Ajay Narain Mathur
committed
added inquirer
1 parent dc40f19 commit 5bf7d35

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
16

lib/utils/builds.ts

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@ import * as times from './times';
44

55
const defaultBuildDir = path.join(__dirname, '../..');
66

7+
export async function getAllDownloadedSlugs(cwd = defaultBuildDir) {
8+
let allDownloadedSlugs = [];
9+
const pathToCache = path.join(cwd, '.data');
10+
11+
const allHosts = await fs.readDir(pathToCache);
12+
13+
await Promise.all(allHosts.map(async host => {
14+
const allUsers = await fs.readDir(path.join(pathToCache, host));
15+
await Promise.all(allUsers.map(async user => {
16+
const allRepos = await fs.readDir(path.join(pathToCache, host, user));
17+
allRepos.forEach(repo => {
18+
allDownloadedSlugs.push(`${host}:${user}/${repo}`);
19+
});
20+
}))
21+
}));
22+
23+
return allDownloadedSlugs;
24+
}
25+
726
export async function getBuildDir(cwd = defaultBuildDir, host, user, repo) {
827
const buildsDir = path.join(cwd, '.data', host, user, repo, 'builds');
928
await fs.mkdirp(buildsDir);
@@ -86,6 +105,7 @@ export async function getLastDownloadedBuildNumber(buildsDir) {
86105
}
87106

88107
module.exports = {
108+
getAllDownloadedSlugs,
89109
getBuildDir,
90110
getHistory,
91111
getLastDownloadedBuildNumber,

lib/utils/sanitize.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import inquirer from 'inquirer';
2+
import { SUPPORTED_COMMANDS } from '../../index';
3+
import { InvalidInputError } from '../error';
4+
import { getAllDownloadedSlugs } from './builds';
5+
import flatten from 'lodash.flatten';
26

37
const getHost = {
48
name: 'host',
@@ -32,12 +36,32 @@ const getRepo = {
3236
message: 'Enter the repo name',
3337
};
3438

39+
const getSlug = (choices) => ({
40+
name: 'slug',
41+
type: 'list',
42+
message: 'Select the repo:',
43+
choices
44+
})
45+
3546
export async function sanitizeInput(input): Promise<[Array<string>, string]> {
3647
if (/(.*):(.*)\/(.*)/.test(input[0])) {
3748
return [input[0].match(/(.*):(.*)\/(.*)/).slice(1), input[1]];
3849
}
3950

40-
const { host, user, repo } = await inquirer.prompt([getHost, getUser, getRepo]);
51+
if (input[0] === 'download') {
52+
const { host, user, repo } = await inquirer.prompt([getHost, getUser, getRepo]);
53+
54+
return [[host, user, repo], input[0]];
55+
}
56+
57+
if (SUPPORTED_COMMANDS.indexOf(input[0]) !== -1) {
58+
const slugs = await getAllDownloadedSlugs();
59+
const { slug } = await inquirer.prompt([getSlug(slugs)]);
60+
61+
if (/(.*):(.*)\/(.*)/.test(slug)) {
62+
return [slug.match(/(.*):(.*)\/(.*)/).slice(1), input[0]];
63+
}
64+
}
4165

42-
return [[host, user, repo], input[0]];
66+
throw new InvalidInputError(`Invalid input. Please run build-stats --help to documention for the tool.`, input);
4367
}

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
"got": "^8.0.1",
4040
"inquirer": "^8.2.0",
4141
"left-pad": "^1.2.0",
42+
"lodash.flatten": "^4.4.0",
4243
"lodash.groupby": "^4.6.0",
4344
"lodash.pick": "^4.4.0",
45+
"lodash.without": "^4.4.0",
4446
"meow": "^v9.0.0",
4547
"mkdirp": "^0.5.1",
4648
"ora": "^5.4.1",

yarn.lock

+10
Original file line numberDiff line numberDiff line change
@@ -3108,6 +3108,11 @@ lodash.debounce@^4.0.8:
31083108
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
31093109
integrity sha1-gteb/zCmfEAF/9XiUVMArZyk168=
31103110

3111+
lodash.flatten@^4.4.0:
3112+
version "4.4.0"
3113+
resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f"
3114+
integrity sha1-8xwiIlqWMtK7+OSt2+8kCqdlph8=
3115+
31113116
lodash.groupby@^4.6.0:
31123117
version "4.6.0"
31133118
resolved "https://registry.yarnpkg.com/lodash.groupby/-/lodash.groupby-4.6.0.tgz#0b08a1dcf68397c397855c3239783832df7403d1"
@@ -3123,6 +3128,11 @@ lodash.pick@^4.4.0:
31233128
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
31243129
integrity sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM=
31253130

3131+
lodash.without@^4.4.0:
3132+
version "4.4.0"
3133+
resolved "https://registry.yarnpkg.com/lodash.without/-/lodash.without-4.4.0.tgz#3cd4574a00b67bae373a94b748772640507b7aac"
3134+
integrity sha1-PNRXSgC2e643OpS3SHcmQFB7eqw=
3135+
31263136
lodash@^4.17.21, lodash@^4.7.0:
31273137
version "4.17.21"
31283138
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"

0 commit comments

Comments
 (0)