forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.js
142 lines (124 loc) · 3.26 KB
/
start.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
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const extend = require('util')._extend;
const { spawn } = require('child_process');
const net = require('net');
const processes = [];
let exitCode;
const baseDir = path.resolve(__dirname, '..');
const srcDir = path.resolve(baseDir);
const isPortTaken = (port) => new Promise((resolve, reject) => {
const tester = net.createServer()
.once('error', (err) => (err.code === 'EADDRINUSE' ? resolve(true) : reject(err)))
.once('listening', () => tester.once('close', () => resolve(false)).close())
.listen(port);
});
const waitPortRelease = (port, count = 0) => new Promise((resolve, reject) => {
isPortTaken(port).then((taken) => {
if (!taken) {
return resolve();
}
if (count > 60) {
return reject();
}
console.log('Port', port, 'not release, waiting 1s...');
setTimeout(() => {
waitPortRelease(port, ++count).then(resolve).catch(reject);
}, 1000);
});
});
const appOptions = {
env: {
PORT: 3000,
ROOT_URL: 'http://localhost:3000',
// MONGO_URL: 'mongodb://localhost:27017/test',
// MONGO_OPLOG_URL: 'mongodb://localhost:27017/local',
},
};
function startProcess(opts, callback) {
const proc = spawn(
opts.command,
opts.params,
opts.options,
);
if (opts.waitForMessage) {
proc.stdout.on('data', function waitForMessage(data) {
if (data.toString().match(opts.waitForMessage)) {
if (callback) {
callback();
}
}
});
}
if (!opts.silent) {
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
}
if (opts.logFile) {
const logStream = fs.createWriteStream(opts.logFile, { flags: 'a' });
proc.stdout.pipe(logStream);
proc.stderr.pipe(logStream);
}
proc.on('exit', function(code, signal) {
if (code != null) {
exitCode = code;
console.log(opts.name, `exited with code ${ code }`);
} else {
console.log(opts.name, `exited with signal ${ signal }`);
}
processes.splice(processes.indexOf(proc), 1);
processes.forEach((p) => {
console.log('Killing process', p.pid);
p.kill();
});
if (processes.length === 0) {
waitPortRelease(appOptions.env.PORT).then(() => {
console.log(`Port ${ appOptions.env.PORT } was released, exiting with code ${ exitCode }`);
process.exit(exitCode);
}).catch((error) => {
console.error(`Error waiting port ${ appOptions.env.PORT } to be released, exiting with code ${ exitCode }`);
console.error(error);
process.exit(exitCode);
});
}
});
processes.push(proc);
}
function startApp(callback) {
startProcess({
name: 'Meteor App',
command: 'node',
params: ['/tmp/build-test/bundle/main.js'],
// command: 'node',
// params: ['.meteor/local/build/main.js'],
waitForMessage: appOptions.waitForMessage,
options: {
cwd: srcDir,
env: extend(appOptions.env, process.env),
},
}, callback);
}
function startChimp() {
startProcess({
name: 'Chimp',
command: 'npm',
params: ['test'],
// command: 'exit',
// params: ['2'],
options: {
env: Object.assign({}, process.env, {
NODE_PATH: `${ process.env.NODE_PATH
+ path.delimiter + srcDir
+ path.delimiter + srcDir }/node_modules`,
}),
},
});
}
function chimpNoMirror() {
appOptions.waitForMessage = 'SERVER RUNNING';
startApp(function() {
startChimp();
});
}
chimpNoMirror();