-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
executable file
·170 lines (142 loc) · 4.55 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env node
const join = require('path').join;
const chalk = require('chalk');
const prepare = require('push2cloud-compiler/prepare');
const compile = require('push2cloud-compiler/compile');
const buildWorkspace = require('push2cloud-compiler/build-workspace');
const writeJsonFile = require('write-json-file');
const logSymbols = require('log-symbols');
const Epochjs = require('epochjs');
const epochjs = new Epochjs();
const isAbsolute = require('path').isAbsolute;
const DEPLOYMENT_MANIFEST = 'deploymentManifest.json';
const req = (p) => {
const pathToReq = p.replace(/^\./, process.cwd());
var required = null;
try {
required = require(pathToReq);
} catch (err) {
if (pathToReq.indexOf(process.cwd()) === 0) {
throw err;
}
required = require(join(process.cwd(), 'node_modules', pathToReq));
}
return required;
};
const timeElapsed = () => {
var elapsed = epochjs.secElapsed();
if (elapsed < 60) return elapsed + ' seconds';
return (elapsed / 60).toFixed(2) + ' minutes';
};
const error = (text, err) => {
console.log(text + ' ' + logSymbols.error + ' ' + timeElapsed() + '\n\t' + chalk.red(err));
if (err.stack) console.log(err.stack);
process.exit(1);
};
const done = (text, next) => (err, result) => {
if (err) return error(text, err);
console.log(text + ' ' + chalk.green(logSymbols.success) + ' ' + timeElapsed());
if (next) next(null, result);
};
const progress = (msg) => (ctx, cb) => {
console.log(msg);
cb(null, ctx);
};
const pluginsOption = {
alias: 'plugins',
description: 'Plugins for prepare, preCompile, compile, buildWorkspace.',
required: true
};
const deploymentManifestOption = {
alias: 'deploymentManifest',
description: 'path to the deploymentManifest'
};
const clearOption = {
alias: 'clearWorkspace',
description: 'clears the __workspace directory before building',
default: false
};
const commandDefault = (yargs) => {
return yargs
.config('settings')
.default('settings', join(process.cwd(), 'push2cloud-config.json'))
.help('h')
.alias('h', 'help')
.argv;
};
const compileCmd = (yargs) => {
const argv = commandDefault(yargs
.usage('Usage: $0 compile [options]')
.option('e', deploymentManifestOption)
.option('c', clearOption)
.option('plugins', pluginsOption)
.array('plugins.prepare')
.array('plugins.compile')
.array('plugins.buildWorkspace')
.option('l', {})
.default('deploymentManifest', join(process.cwd(), DEPLOYMENT_MANIFEST), './' + DEPLOYMENT_MANIFEST)
);
const preparePlugins = argv.plugins.prepare.map(req);
const compilePlugins = argv.plugins.compile.map(req);
const buildWorkspacePlugins = argv.plugins.buildWorkspace.map(req);
console.log('prepare');
epochjs.start();
const deploymentManifestPath = isAbsolute(argv.deploymentManifest) ? argv.deploymentManifest : join(process.cwd(), argv.deploymentManifest);
const clearOptions = { clearWorkspace: argv.clearWorkspace };
prepare(
clearOptions
, preparePlugins
, deploymentManifestPath
, null
, done('prepare', () => {
console.log('compile');
epochjs.start();
const compileDone = done('compile');
compile(compilePlugins, null, null, (err, content) => {
if (err) return compileDone(err);
writeJsonFile('deploymentConfig.json', content)
.then(() => {
compileDone();
console.log('build');
epochjs.start();
buildWorkspace(clearOptions, buildWorkspacePlugins
, null, null, done('build'));
})
.catch(compileDone);
});
}));
return yargs;
};
const lintCmd = (yargs) => {
commandDefault(yargs.usage('Usage: $0 lint [options]'));
return yargs;
};
const execCmd = (yargs) => {
const argv = commandDefault(
yargs
.usage('Usage: $0 exec <workflow> [options]')
);
const wf = argv._[1];
console.log(`Workflow: ${wf} `);
epochjs.start();
const workflow = req(wf);
const deploymentConfig = require(join(process.cwd(), 'deploymentConfig.json'));
workflow(deploymentConfig
, progress
, (err, ctx) => done(`Workflow: ${wf} done`, () => process.exit(0))(err, ctx));
return yargs;
};
require('yargs')
// basic usage
.usage('Usage: $0 <command> [options]')
.demand(2)
// commands
.command('exec', 'Execute a workflow.', execCmd)
.command('compile', 'Compile the manifests to a deploymentconfig.', compileCmd)
.command('lint', 'Lint the manifests.', lintCmd)
.command('ls', 'List manifests/workflows/schemas.', lintCmd)
// help
.help('h')
.alias('h', 'help')
.epilog('http://github.com/org/repo')
.argv;