This repository has been archived by the owner on Oct 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
treeline-exec.js
executable file
·346 lines (304 loc) · 11.1 KB
/
treeline-exec.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env node
/**
* Module dependencies
*/
var program = require('commander');
var chalk = require('chalk');
var Machinepacks = require('machinepack-machines');
var Machine = require('machine');
var _ = require('lodash');
var yargs = require('yargs');
// Build CLI options
var cliOpts = yargs.argv;
delete cliOpts._;
delete cliOpts.$0;
program
.usage('[options]')
.parse(process.argv);
var machinepackIdentity;
var machineIdentity;
// Parse command-line argument (i.e. not options)
// which indicates an optional machinepack and/or machine
if (_.isString(program.args[0])){
if (program.args[0].match(/\//)) {
machinepackIdentity = program.args[0].split('/')[0];
machineIdentity = program.args[0].split('/')[1];
}
else {
machinepackIdentity = program.args[0];
}
}
// Allow unknown options.
program.unknownOption = function NOOP(){};
(Machine.build({
inputs: {},
defaultExit: 'success',
exits: {
success: {
example: {
machinepack: {
identity: 'machinepack-whatever',
variableName: 'Whatever'
},
machine: {
identity: 'do-stuff',
variableName: 'doStuff'
},
withInputs: [
{
name: 'foobar',
value: 'fiddle diddle'
// ^^^^^ this is ok because it's always a string entered on the CLI interactive prompt
}
],
exited: {
exit: 'success',
jsonValue: '{"stuff": "things"}',
inspectedValue: '{stuff: "things"}',
void: false
}
}
},
error: {},
invalidMachine: {
example: {
machine: 'do-stuff'
}
}
},
fn: function (inputs, exits) {
var util = require('util');
var inquirer = require('inquirer');
var Http = require('machinepack-http');
var Npm = require('machinepack-npm');
var Machines = require('machinepack-machines');
var enpeem = require('enpeem');
console.log('\n***WARNING***\nThis is an experimental feature!\n\n');
console.log();
console.log();
console.log('Preview machine from registry');
console.log('================================');
console.log();
var REGISTRY_BASE_URL = 'http://node-machine.org';
// Look up list of machinepacks
Http.sendHttpRequest({
baseUrl: REGISTRY_BASE_URL,
url: '/machinepacks'
}).exec({
error: exits.error,
success: function (resp){
var machinepacks;
try {
machinepacks = JSON.parse(resp.body);
}
catch (e){
return exits.error(e);
}
inquirer.prompt([{
name: 'machinepackIdentity',
message: 'Please choose a machinepack',
type: 'list',
when: function (){
return !machinepackIdentity;
},
choices: _.reduce(machinepacks, function (memo, pack){
memo.push({
name: pack.friendlyName,
value: pack.identity
});
return memo;
}, [])
}], function (answers){
// Now we have a `machinepackIdentity` if we didn't already
machinepackIdentity = machinepackIdentity || answers.machinepackIdentity;
// Look up list of machines within this machinepack
Http.sendHttpRequest({
baseUrl: REGISTRY_BASE_URL,
url: util.format('/%s', machinepackIdentity)
}).exec({
error: exits.error,
success: function (resp){
var machinepack;
try {
machinepack = JSON.parse(resp.body);
}
catch (e){
return exits.error(e);
}
inquirer.prompt([{
name: 'machineIdentity',
message: 'Now choose a machine to run',
type: 'list',
when: function (){
return !machineIdentity;
},
choices: _.reduce(machinepack.machines, function (memo, machine){
memo.push({
name: machine.friendlyName,
value: machine.identity
});
return memo;
}, [])
}], function (answers){
// Now we have a `machineIdentity` if we didn't already
machineIdentity = machineIdentity || answers.machineIdentity;
Http.sendHttpRequest({
baseUrl: REGISTRY_BASE_URL,
url: util.format('/%s/%s', machinepackIdentity, machineIdentity)
}).exec({
error: exits.error,
notOk: exits.error,
success: function (resp){
var machine;
try {
machine = JSON.parse(resp.body);
}
catch (e){
return exits.error(e);
}
console.log();
console.log('Fetching code for NPM module "%s"@%s...',machinepack.npmPackageName, machinepack.version);
Npm.downloadPackage({
name: machinepack.npmPackageName,
version: machinepack.version
}).exec({
error: exits.error,
success: function (machinepackPath){
// Set present working directory to the `machinepackPath`
// (remembering the previous cwd for later)
var cwd = process.cwd();
process.chdir(machinepackPath);
console.log('Installing NPM dependencies for %s...',machinepackIdentity);
enpeem.install({
dependencies: [],
loglevel: 'silent'
}, function (err){
if (err) return exits.error(err);
// Return to previous present working directory
process.chdir(cwd);
console.log();
console.log('Running %s...',machineIdentity);
console.log();
Machines.runMachineInteractive({
machinepackPath: machinepackPath,
identity: machineIdentity,
inputValues: (function (){
return _.reduce(cliOpts, function (memo, inputValue, inputName){
memo.push({
name: inputName,
value: inputValue,
protect: false
});
return memo;
}, []);
})()
}).exec({
error: function (err){
return exits.error(err);
},
invalidMachine: function (){
return exits.invalidMachine();
},
success: function (result){
return exits.success(_.extend({
machine: {
identity: machineIdentity,
variableName: machine.variableName
},
machinepack: {
identity: machinepackIdentity,
variableName: machinepack.variableName
}
},result));
}
});
});
}
});
}
});
});
}
});
});
}
});
}
})).exec({
error: function (err){
console.error('Unexpected error occurred:\n',typeof err === 'object' && err instanceof Error ? err.stack : err);
},
invalidMachine: function (err){
console.error('Cannot run machine `'+chalk.red(err.machine)+'`. Machine is invalid. Error details:\n',err);
},
success: function (result){
console.log('___'+repeatChar('_')+'_˛');
console.log(' '+repeatChar(' ')+' ');
console.log(' '+chalk.gray('%s.%s()'), chalk.bold(chalk.white(result.machinepack.variableName)), chalk.bold(chalk.yellow(result.machine.variableName)));
// console.log('');
// console.log(chalk.white(' * * * * * * * * * * * * * * * * * * * * * * * * '));
// console.log(chalk.white(' * OUTCOME * '));
// console.log(chalk.white(' * * * * * * * * * * * * * * * * * * * * * * * * '));
// console.log('');
// console.log(' using input values:\n', chalk.bold(chalk.yellow(identity)), _.reduce(result.withInputs, function(memo, configuredInput) {
// console.log(' Used input values:\n', _.reduce(result.withInputs, function(memo, configuredInput) {
console.log(' ');
console.log(_.reduce(result.withInputs, function(memo, configuredInput) {
memo += ' » ' + chalk.white(configuredInput.name) + ' ' + chalk.gray(JSON.stringify(configuredInput.value));
memo += '\n';
return memo;
}, ''));
console.log('___'+repeatChar('_')+'_¸ ');
console.log(' | ');
// console.log(' Triggered '+chalk.blue(result.exited.exit)+' callback'+(function (){
// if (!result.exited.void) {
// return ', returning:\n ' + chalk.gray(result.exited.jsonValue);
// }
// return '.';
// })());
// Determine chalk color
var exitColor = (function (){
if (result.exited.exit === 'error') {
return 'red';
}
if (result.exited.exit === 'success') {
return 'green';
}
return 'blue';
})();
console.log(' '+chalk.bold(chalk[exitColor]('•'))+' \n The machine triggered its '+chalk.bold(chalk[exitColor](result.exited.exit))+' exit'+(function (){
if (!result.exited.void) {
return ' and returned a value:\n '+chalk.gray(result.exited.inspectedValue);
}
return '.';
})());
console.log();
console.log();
console.log(chalk.white(' To run again:'));
console.log(chalk.white((function (){
var cmd = ' treeline exec '+result.machinepack.identity + '/' + result.machine.identity;
_.each(result.withInputs, function (configuredInput){
// Skip protected inputs (they need to be re-entered)
if (configuredInput.protect) return;
cmd += ' ';
cmd += '--'+configuredInput.name+'=\''+configuredInput.value.replace(/'/g,'\'\\\'\'')+'\'';
});
return cmd;
})()));
console.log();
}
});
/**
* private helper fn
* @param {[type]} char [description]
* @param {[type]} width [description]
* @return {[type]} [description]
*/
function repeatChar(char,width){
width = width || 60;
var borderStr = '';
for (var i=0;i<width;i++) {
borderStr += char;
}
return borderStr;
}