This repository has been archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·122 lines (104 loc) · 3.24 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const Promise = require('bluebird');
const yargs = require('yargs');
const _ = require('lodash');
if (!fs.existsSync(path.join(__dirname, 'dist'))) {
throw new Error('You must run the build first (usually, gulp build)');
}
/**
* @typedef {Object} cli.command
* @property {string} command
* @property {string} describe
* @property {cli.builder} builder
* @property {cli.handler} handler
*/
/**
* @typedef {function(yargs): yargs} cli.builder
*/
/**
* @typedef {function(argv): Promise} cli.handler
*/
const config = require('./dist/config').default();
const deps = require('./dist/deps').default(config);
const Log = require('./dist/util/log').Log;
const libdebug = require('debug'); // This must be the first use of 'debug' (after config is loaded)
const debug = libdebug('ecs-cleaner:index');
debug('Starting ecs-cleaner CLI interface');
const container = deps.container;
const log = container.cli.log;
const cli = yargs
.strict()
.usage('Usage: ecs-cleaner <command> [options]')
.options({
apply: {
alias: 'a',
describe: 'Actually apply the operation (default is always a dry run)',
global: true,
type: 'boolean',
},
verbose: {
alias: 'v',
describe: 'Output more information (provide multiple times for more noise)',
global: true,
type: 'count',
},
quiet: {
alias: 'q',
describe: 'Output less information (provide multiple times for less noise)',
global: true,
type: 'count',
},
})
.help('help');
deps.container.cli.commands.forEach((service) => {
const command = _.get(container, `cli.command.${service}`, false);
const description = _.get(container, `cli.describe.${service}`, false);
if (!command || typeof command !== 'string') {
throw new Error(`Invalid command while configuring CLI command ${service}: ${typeof command}`);
}
if (!description || typeof description !== 'string') {
throw new Error(`Invalid description while configuring CLI command ${service}: ${typeof description}`);
}
cli.command(
command,
description,
(builder) => {
const original = _.get(container, `cli.builder.${service}`, {});
switch (typeof original) {
case 'function':
return original(builder);
case 'object':
return builder.options(original);
default:
throw new Error('Unexpected type of builder');
}
},
(argv) => {
const original = _.get(container, `cli.handler.${service}`, false);
if (!original || typeof original !== 'function') {
throw new Error('Unexpected type of handler');
}
Log.setLevel(2 + argv.verbose - argv.quiet);
return original(argv)
.then(() => {
log.notice('All done!');
process.exit(0);
})
.catch(err => {
if (typeof err === 'string') {
log.error(err);
} else if (err instanceof Error) {
log.error(err.message);
log.debug(err.stack);
} else {
log.error(err);
throw err;
}
process.exit(3);
});
}
);
});
const argv = cli.demand(1, 'You must supply a command to execute').argv;