-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
98 lines (80 loc) · 3.19 KB
/
cli.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import arg from 'arg';
import dotenv from 'dotenv';
import { FetchJobs } from './lib/fetchJobs';
import { Convert } from './lib/convert';
import { ShowTiming } from './lib/showTiming';
import { ShowSteps } from './lib/showSteps';
import { HELP } from './lib/help';
dotenv.config();
const parseArgumentsIntoOptions = ({ rawArgs, classObj }) => {
const requiredArgs = classObj ? classObj.requiredArgs : { '--help': Boolean, '-h': '--help' };
const returnArgs = classObj ? classObj.returnArgs : () => {};
const excludeNodeAndCommandPath = 2;
const args = arg({ ...requiredArgs },
{
argv: rawArgs.slice(excludeNodeAndCommandPath),
permissive: true,
}
);
return {
...returnArgs(args),
help: args['--help'] || false,
command: args._[0],
};
};
const baseHost = process.env.CIRCLECI_HOST || 'https://circleci.com';
const runFetch = async () => {
const { limit, offset, projectSlug, isServer } = parseArgumentsIntoOptions({ rawArgs: process.argv, classObj: FetchJobs })
if (!['CIRCLECI_TOKEN'].every(key => Object.keys(process.env).includes(key))) {
throw new Error('Please set CIRCLECI_TOKEN as environment valiable');
}
if (projectSlug && ! FetchJobs.validateProjectSlug(projectSlug)) {
throw new Error(`--project value ${projectSlug} is worng, please set the right format <github or bitbucket>/<org>/<project>`);
}
const fetch = new FetchJobs({ baseHost, token: process.env.CIRCLECI_TOKEN, limit, offset, isServer, projectSlug });
await fetch.exec();
}
const runConvert = async () => {
const { jobName, workflowName, inputFilename, outputFilename } = parseArgumentsIntoOptions({ rawArgs: process.argv, classObj: Convert })
if (!jobName || !workflowName) {
throw new Error(`This command requires --jobName and --workflowName options`);
}
const convert = new Convert({ jobName, workflowName, inputFilename, outputFilename });
await convert.exec();
}
const runShowTiming = async () => {
const { jobSlug, stepNumber, stepName } = parseArgumentsIntoOptions({ rawArgs: process.argv, classObj: ShowTiming })
if (!jobSlug) {
throw new Error(`This command requires --jobSlug`);
}
if (stepNumber >= 0 && stepName) {
throw new Error(`This command requires to specify only one of --stepNumber or --stepName`);
}
const showTiming = new ShowTiming({ jobSlug, stepName, stepNumber, baseHost, token: process.env.CIRCLECI_TOKEN });
await showTiming.exec();
}
const runShowSteps = async () => {
const { jobSlug } = parseArgumentsIntoOptions({ rawArgs: process.argv, classObj: ShowSteps })
if (!jobSlug) {
throw new Error(`This command requires --jobSlug`);
}
const showSteps = new ShowSteps({ jobSlug, baseHost, token: process.env.CIRCLECI_TOKEN });
await showSteps.exec();
}
const cli = async () => {
const { command, help } = parseArgumentsIntoOptions({ rawArgs: process.argv });
const commands = {
fetch: runFetch,
convert: runConvert,
showTiming: runShowTiming,
showSteps: runShowSteps
};
if (help) {
console.log(HELP.join(''));
} else if (command && commands[command]) {
await commands[command]();
} else {
console.log('No command available, please run `cjc --help` command');
}
};
export default cli;