-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
executable file
·84 lines (71 loc) · 2.07 KB
/
bin.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
#!/usr/bin/env node
// node modules
const fs = require('fs');
const path = require('path');
// custom modules
const colors = require('./src/cli-colors');
// setup sling instance
const sling = new (require('./index'))().set('cli', true);
// the working directory
const workingdir = process.cwd();
// default config filename
const configfilename = 'slingshot.config.js';
// paths to search for the config file
const pathsToLookForConfig = [
path.join(workingdir, './config/', configfilename),
path.join(workingdir, configfilename),
];
// valid path after lookup
let configpath = null;
// search for config file
pathsToLookForConfig.forEach((p) => {
if (!configpath && fs.existsSync(p)) {
configpath = require(p);
}
});
// default configs
let config = require('./config.defaults');
// merge defaults with found config
if (configpath) {
config = { ...config, ...configpath };
}
// set initial config for sling
sling.set('config', config);
// setup cli yargs parser
require('yargs/yargs')(process.argv.slice(2))
.scriptName('slingshot')
.usage('$0 <command> [args...]')
.option('config', {
alias: 'c',
describe: 'Path to config file.',
requiresArg: true,
})
.coerce('config', function (arg) {
configpath = arg.startsWith('/') ? path.join(arg) : path.join(workingdir, arg);
if (!fs.existsSync(configpath)) {
throw new Error(`Config file not found: ${configpath}`);
}
sling.set('config', require(configpath));
})
.command('init', 'Create slingshot config file.')
.command(
'deploy [revision]',
'Deploy specific revision. Could be any git tag, commit, branch. Defaults to "master".',
() => {},
(arg) => sling.deploy(arg.revision)
)
.command(
'rollback',
'Roll back to previously published release',
() => {},
function () {
console.log('rollback');
}
)
.demandCommand(1, 'You need to provide at least one command to execute.')
.fail((msg, err, yargs) => {
console.log(`\n${colors.fgRed}Error: ${colors.reset}${msg}\n`);
console.log(yargs.help());
process.exit(1);
})
.help().argv;