Skip to content

Add inquirer #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16
20 changes: 20 additions & 0 deletions lib/utils/builds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ import * as times from './times';

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

export async function getAllDownloadedSlugs(cwd = defaultBuildDir) {
let allDownloadedSlugs = [];
const pathToCache = path.join(cwd, '.data');

const allHosts = await fs.readDir(pathToCache);

await Promise.all(allHosts.map(async host => {
const allUsers = await fs.readDir(path.join(pathToCache, host));
await Promise.all(allUsers.map(async user => {
const allRepos = await fs.readDir(path.join(pathToCache, host, user));
allRepos.forEach(repo => {
allDownloadedSlugs.push(`${host}:${user}/${repo}`);
});
}))
}));

return allDownloadedSlugs;
}

export async function getBuildDir(cwd = defaultBuildDir, host, user, repo) {
const buildsDir = path.join(cwd, '.data', host, user, repo, 'builds');
await fs.mkdirp(buildsDir);
Expand Down Expand Up @@ -86,6 +105,7 @@ export async function getLastDownloadedBuildNumber(buildsDir) {
}

module.exports = {
getAllDownloadedSlugs,
getBuildDir,
getHistory,
getLastDownloadedBuildNumber,
Expand Down
30 changes: 27 additions & 3 deletions lib/utils/sanitize.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import inquirer from 'inquirer';
import { SUPPORTED_COMMANDS } from '../../index';
import { InvalidInputError } from '../error';
import { getAllDownloadedSlugs } from './builds';
import flatten from 'lodash.flatten';

const getHost = {
name: 'host',
Expand Down Expand Up @@ -29,15 +33,35 @@ const getUser = {
const getRepo = {
name: 'repo',
type: 'input',
message: 'Enter the repo name',
message: 'Enter the repository name',
};

const getSlug = (choices) => ({
name: 'slug',
type: 'list',
message: 'Select the repo:',
choices
})

export async function sanitizeInput(input): Promise<[Array<string>, string]> {
if (/(.*):(.*)\/(.*)/.test(input[0])) {
return [input[0].match(/(.*):(.*)\/(.*)/).slice(1), input[1]];
}

const { host, user, repo } = await inquirer.prompt([getHost, getUser, getRepo]);
if (input[0] === 'download') {
const { host, user, repo } = await inquirer.prompt([getHost, getUser, getRepo]);

return [[host, user, repo], input[0]];
}

return [[host, user, repo], input[0]];
if (SUPPORTED_COMMANDS.indexOf(input[0]) !== -1) {
const downloadedSlugs = await getAllDownloadedSlugs();
const { slug } = await inquirer.prompt([getSlug(downloadedSlugs)]);

if (/(.*):(.*)\/(.*)/.test(slug)) {
return [slug.match(/(.*):(.*)\/(.*)/).slice(1), input[0]];
}
}

throw new InvalidInputError(`Invalid input. Please run build-stats --help to documention for the tool.`, input);
}
47 changes: 44 additions & 3 deletions lib/utils/tests/builds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,53 @@ import fixtures from 'fixturez';
import * as builds from '../builds';
const f = fixtures(__dirname);

test('builds.getAllDownloadsSlugs', async () => {
const cwd = f.copy('with-builds');
const downloadedSlugs = await builds.getAllDownloadedSlugs(cwd);
expect(downloadedSlugs.length).toBe(2);
expect(downloadedSlugs).toEqual(expect.arrayContaining([
'bitbucket:atlassian/build-stats',
'travis:boltpkg/bolt'
]));
});

test('builds.getBuildDir()', async () => {
let cwd = f.copy('testRepo');
let dirPath = await builds.getBuildDir(cwd, 'bitbucket', 'test', 'test-repo');
expect(dirPath).toMatch(/testRepo\/\.data\/bitbucket\/test\/test-repo\/builds/);
});

test.todo('builds.getHistory()');
test.todo('builds.findLongest()');
test.todo('builds.toTimeRanges()');
test('builds.getHistory()',async () => {
const cwd = f.copy('with-builds');
const history = await builds.getHistory(cwd, 'bitbucket', 'atlassian', 'build-stats', {
branch: '*',
result: '*'
});
expect(history.length).toBe(10);
});
test('builds.findLongest()',async () => {
const cwd = f.copy('with-builds');
const allBuilds = await builds.getHistory(cwd, 'bitbucket', 'atlassian', 'build-stats', {
branch: '*',
result: '*'
});

const longestBuild = builds.findLongest(allBuilds);
expect(longestBuild.id).toBe('7')
});

test('builds.toTimeRanges()',async () => {
const cwd = f.copy('with-builds');
const allBuilds = await builds.getHistory(cwd, 'bitbucket', 'atlassian', 'build-stats', {
branch: '*',
result: '*'
});

const ranges = builds.toTimeRanges(allBuilds, {
period: 365, // period of 365 days/ 1 year
last: 100 // last 100 years
});

// One of the fixtures build is from 1900, so it should be excluded
expect(ranges[0].length).toBe(9);
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
"got": "^8.0.1",
"inquirer": "^8.2.0",
"left-pad": "^1.2.0",
"lodash.flatten": "^4.4.0",
"lodash.groupby": "^4.6.0",
"lodash.pick": "^4.4.0",
"lodash.without": "^4.4.0",
"meow": "^v9.0.0",
"mkdirp": "^0.5.1",
"ora": "^5.4.1",
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3108,6 +3108,11 @@ lodash.debounce@^4.0.8:
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
integrity sha1-gteb/zCmfEAF/9XiUVMArZyk168=

lodash.flatten@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f"
integrity sha1-8xwiIlqWMtK7+OSt2+8kCqdlph8=

lodash.groupby@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash.groupby/-/lodash.groupby-4.6.0.tgz#0b08a1dcf68397c397855c3239783832df7403d1"
Expand All @@ -3123,6 +3128,11 @@ lodash.pick@^4.4.0:
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
integrity sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM=

lodash.without@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.without/-/lodash.without-4.4.0.tgz#3cd4574a00b67bae373a94b748772640507b7aac"
integrity sha1-PNRXSgC2e643OpS3SHcmQFB7eqw=

lodash@^4.17.21, lodash@^4.7.0:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
Expand Down