-
Notifications
You must be signed in to change notification settings - Fork 13
/
leo-cli-test.js
114 lines (103 loc) · 3.3 KB
/
leo-cli-test.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
#!/usr/bin/env node
var path = require('path');
var program = require('commander');
var colors = require('colors');
const utils = require("./lib/utils.js");
const watch = require("node-watch");
const fork = require("child_process").fork;
program
.version('0.0.1')
.option("-e, --env [env]", "Environment")
.option("--region [region]", "Region to run cloudformation")
.option("-w --watch [watch]", "Watch files for changes")
.option("-i --inspect-brk [port]", "Debug")
.option("--inspect [port]", "Debug")
.usage('<dir> [options]')
.action(function(dir) {
let debugCmd = (program.inspectBrk || program.inspect);
if (debugCmd) {
debugCmd = [`--inspect${program.inspectBrk?'-brk' : ''}=${(debugCmd === true) ? "9229" : debugCmd}`];
}
let rootDir = path.resolve(process.cwd(), dir);
let watchDir = utils.findFirstPackageValue(rootDir, ["microservice"], "__directory");
var pkg = require(path.resolve(rootDir, "package.json"));
var reactRunner = require("./lib/react.js");
var buildConfig = require("./lib/build-config").build;
let c = buildConfig(rootDir);
if (pkg.config && pkg.config.leo && pkg.config.leo.type == "microservice") {
process.env.leo_config_bootstrap_path = path.resolve(c._meta.microserviceDir, "leo_config.js");
process.env.NODE_ENV = program.env || "dev";
process.env.LEO_LOCAL = "true";
reactRunner(rootDir, c, c);
} else {
let child = null;
let envVariables = {
NODE_ENV: program.env || "dev",
LEO_LOCAL: "true",
LEO_ENV: program.env || "dev",
LEO_REGION: program.region,
LEO_CONFIG: JSON.stringify(c),
LEO_PREVENT_RUN_AGAIN: "true",
leo_config_bootstrap_path: path.resolve(c._meta.microserviceDir, "leo_config.js"),
LEO_RUNNER_EXIT_ON_COMPLETE: (program.watch && !debugCmd) ? "false" : "true" // Don't exit if we are watching an on the same process
};
function runInSameProcess() {
Object.keys(require.cache).map(k => {
delete require.cache[k]
});
Object.entries(envVariables).map(([key, value]) => process.env[key] = value);
require(__dirname + "/lib/runner.js");
}
function runInChildProcess() {
function f() {
child = fork(__dirname + "/lib/runner.js", process.argv, {
cwd: rootDir,
env: Object.assign({}, process.env, envVariables),
execArgv: debugCmd || []
});
child.once("exit", () => {
child = null;
if (!program.watch) {
watcher && watcher.close();
watcher = null;
process.exit();
}
});
}
if (child) {
child.once("exit", f);
child.kill();
} else {
f();
}
}
process.on('SIGINT', () => {
if (child) {
console.log("closing child process. Ctrl-c again to cancel test");
child.kill();
} else {
watcher && watcher.close();
watcher = null;
process.exit();
}
});
let run = debugCmd ? runInChildProcess : runInSameProcess;
run();
if (program.watch) {
let watchDirs = (watchDir ? [watchDir] : []).concat(pkg.config.test ? pkg.config.test.watch : []);
var watcher = watch(watchDirs, {
recursive: true,
filter: (f) => {
return !/node_modules/.test(f)
}
}, (eventType, filename) => {
console.log("new file");
run();
});
}
}
})
.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp(colors.red);
}