UNPKG

3.64 kBJavaScriptView Raw
1const Promise = require('bluebird');
2const c = require('chalk');
3const _ = require('lodash/fp');
4
5const bumpDependencies = require('./bump-dependencies');
6const bumpVersion = require('./bump-version');
7const {setup, validate} = require('./scaffold-config');
8const {UPGRADE, BUMP, VALIDATE, SETUP, DIRTY, selectCommand} = require('./commands');
9const {getConfig} = require('./core/config');
10const {makeError} = require('./core/utils');
11
12let cmd;
13const setCommand = _cmd => () => {
14 cmd = _cmd;
15};
16
17// eslint-disable-next-line import/order
18const yargs = require('yargs')
19 .command({
20 command: UPGRADE,
21 aliases: ['upgrade', 'bd'],
22 describe: 'Upgrades defined dependencies and open Pull request for them',
23 handler: setCommand(UPGRADE)
24 })
25 .command({
26 command: BUMP,
27 aliases: ['version', 'ab'],
28 describe: 'Auto Bump package version',
29 handler: setCommand(BUMP)
30 })
31 .command({
32 command: VALIDATE,
33 aliases: ['check'],
34 describe: 'Validate a update-node configuration',
35 handler: setCommand(VALIDATE)
36 })
37 .command({
38 command: SETUP,
39 aliases: ['scaffold'],
40 describe: 'Scaffold a update-node configuration',
41 handler: setCommand(SETUP)
42 })
43 .option('local', {
44 describe: 'Run in local mode with github publication',
45 boolean: true,
46 alias: 'l'
47 })
48 .option('token', {
49 describe: 'Token to authentificate to github',
50 string: true,
51 alias: 't'
52 })
53 .option('config', {
54 describe: 'Override update-node configuration default path',
55 string: true,
56 alias: 'c'
57 })
58 .option('auto', {
59 describe: 'Select automatically behavior to adopt based on current commit and branch',
60 boolean: true,
61 alias: 'A'
62 });
63
64const COMMANDS = {
65 [UPGRADE]: ['config', bumpDependencies, ['local', 'token']],
66 [BUMP]: ['config', bumpVersion, ['local', 'token']],
67 [VALIDATE]: ['argv', validate, []],
68 [SETUP]: ['argv', setup, []],
69 [DIRTY]: [
70 'argv',
71 argv =>
72 Promise.reject(
73 makeError('State is currently dirty, auto cant run', {
74 exitCode: 6
75 })
76 )
77 ],
78 default: [
79 'argv',
80 argv =>
81 Promise.reject(
82 makeError('😴 No command was selected', {
83 exitCode: 4,
84 details: _.isEmpty(argv._) ? 'No command given' : `Command ${argv._[0]} does not exist`
85 })
86 )
87 ]
88};
89
90const runUpdateNode = async argv => {
91 if (!cmd && argv.auto) {
92 cmd = await selectCommand();
93 if (cmd) process.stdout.write(c.bold.blue(`🎚 Decided to run the command ${cmd}\n`));
94 }
95 const [commandType, mainCommand, requiredOptions] = COMMANDS[cmd] || COMMANDS.default;
96 if (!_.isEmpty(requiredOptions) && !_.some(opt => _.has(opt, argv), requiredOptions)) {
97 const error = new Error(
98 `Could not run command without one of following options: ${requiredOptions
99 .map(opt => `--${opt}`)
100 .join(', ')}`
101 );
102 error.details = 'Update-Node behavior was changed starting from 2.0';
103 error.exitCode = 22;
104 throw error;
105 }
106 // FIXME perform schema validation
107 if (commandType === 'config') {
108 const config = await getConfig(argv);
109 await mainCommand(config);
110 } else if (commandType === 'argv') {
111 await mainCommand(argv);
112 } else {
113 await mainCommand();
114 }
115};
116
117const main = () => {
118 const argv = yargs.parse(process.argv.slice(2));
119 runUpdateNode(argv).catch(err => {
120 process.stderr.write(`${c.bold.red(err.message)}\n`);
121 if (err.details) process.stderr.write(`${err.details}\n`);
122 process.stderr.write('\n');
123 process.exit(err.exitCode || 2);
124 });
125};
126
127module.exports = {main, runUpdateNode};
128
129if (!module.parent) {
130 main();
131}