-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
83 lines (71 loc) · 3 KB
/
Copy pathapp.js
File metadata and controls
83 lines (71 loc) · 3 KB
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
/*
Adapted from the original work of https://github.com/trayio/script-connector-tester/blob/master/run.js
Modified and improved by Enrico Simonetti @ www.naonis.tech
*/
const processArgs = process.argv.slice(2);
const fs = require('fs');
const util = require('util');
const path = require('path');
if (processArgs.length !== 1) {
console.log('Please provide the path with the script and its inputs. Usage: node app.js <step path>');
process.exit(1);
}
let providedPath = processArgs[0];
if (!fs.existsSync(providedPath)) {
console.log('The provided path "' + providedPath + '" does not exist');
process.exit(1);
} else {
providedPath = path.resolve(providedPath);
}
if (!fs.existsSync(path.join(providedPath, 'script.js'))) {
console.log('The provided path "' + providedPath + '" does not contain "script.js"');
process.exit(1);
}
if (!fs.existsSync(path.join(providedPath, 'inputs'))) {
console.log('The provided path "' + providedPath + '" does not contain the "inputs" directory');
process.exit(1);
}
const inputFiles = fs.readdirSync(path.join(providedPath, 'inputs'))
.filter(fileName => path.extname(fileName) === '.json')
.map(fileName => path.join(providedPath, 'inputs', fileName));
if (inputFiles.length < 1) {
console.log('The provided input directory "' + path.join(providedPath, 'inputs') + '" does not contain input files');
process.exit(1);
}
let trayStep = require(path.join(providedPath, 'script.js')).step;
global._ = require('lodash');
global.mout = require('mout');
global.moment = require('moment');
inputFiles.forEach((file) => {
console.log('====================================================================================================');
console.log('===== Executing: ' + path.join(providedPath, 'script.js') + ' with input file: ' + file);
console.log('====================================================================================================');
console.log('');
let input = require(file);
let trayVariables = {};
input.variables.forEach((v) => {
trayVariables[v.name] = v.value;
});
console.log('');
console.log('Script logs:');
console.log('');
let output;
try {
output = trayStep(trayVariables);
} catch (error) {
console.error('Error while executing the script: ', error);
process.exit(1);
}
console.log('');
console.log('Script output:');
console.log('');
// Use util.inspect to log the entire object with complete nested structures
console.log(util.inspect(output, { depth: null, colors: true }));
console.log('');
console.log('====================================================================================================');
console.log('===== Output of: ' + path.join(providedPath, 'script.js') + ' with input file: ' + file);
console.log('====================================================================================================');
console.log('');
console.log('');
console.log('');
});