-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsetup.js
executable file
·295 lines (284 loc) · 12.5 KB
/
setup.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
#!/usr/bin/env node
const { resolve } = require('path');
const { mkdirpSync, removeSync, readFileSync, pathExistsSync, writeFileSync, existsSync, createWriteStream } = require('fs-extra');
const { argv } = require('yargs');
const { execSync, exec } = require('child_process');
const deasync = require('deasync');
const treeKill = require('tree-kill');
let { MongodHelper } = require('@josepmc/mongodb-prebuilt');
const findProcess = require('find-process');
if (!argv.params || !argv.params.setup || !(argv.params.setup === true || argv.params.setup instanceof Object)) {
throw `Usage: setup.js [--params.setup.apps=[apps directory]] [--params.setup.ganache]
All parameters are mutually exclusive`;
}
let currentDir = __dirname;
let checkoutDir = process.env.TEST_CHECKOUT_DIR || resolve(__dirname, 'git-checkout');
let pidsFile = resolve(checkoutDir, 'pids.pid');
console.log('Performing cleanup...');
if (existsSync(pidsFile)) {
for (let p of readFileSync(pidsFile, 'utf8').split('\n')) {
try { deasync(r => treeKill(p, 'SIGKILL', r))(); } catch (error) { }
}
removeSync(pidsFile);
}
if (process.env.TEST_COVERAGE !== false) process.env.TEST_COVERAGE = true;
if (!process.env.TEST_NO_DELETE_ENV) removeSync(checkoutDir);
mkdirpSync(checkoutDir);
let logDir = process.env.TEST_LOG_DIR || resolve(currentDir, 'logs');
mkdirpSync(logDir);
let findAndKill = port => {
deasync(async callback => {
let proc = await findProcess('port', port);
for (let p of proc) {
console.log(`Killing process '${p.cmd}' as it's using port ${port}`);
process.kill(p.pid, 'SIGKILL');
}
callback();
})();
}
let sources = {
apps: {
url: "https://github.com/PolymathNetwork/polymath-apps.git",
},
};
let logs = {
issuer: resolve(logDir, "issuer.log"),
investor: resolve(logDir, "investor.log"),
offchain: resolve(logDir, "offchain.log"),
ganache: resolve(logDir, "ganache.log"),
};
let pids = {};
let defaultBranch = 'develop';
let branch = process.env.TEST_BRANCH || process.env.TRAVIS_BRANCH || defaultBranch;
let charSep = process.platform === "win32" ? ';' : ':';
const setNodeVersion = () => {
let path = process.env.PATH.replace(/[:;]?[^:;]*node_modules[^:;]*/g, '');
if (path.endsWith(charSep)) path = path.substr(0, path.length - 1);
path = `${process.env.TEST_EXTRA_PATH ? process.env.TEST_EXTRA_PATH + charSep : ''}${resolve(__dirname, 'node_modules', '.bin')}${charSep}${path}`;
console.log(`Using path: ${path}`);
return path;
}
if (!process.env.TEST_MM_NETWORK || !process.env.TEST_MM_SECRET) {
console.log(`Metamask network or secret is not set, using defaults`);
process.env.TEST_MM_SECRET = "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat";
process.env.TEST_MM_NETWORK = "l";
}
let mongo;
const setup = {
git: async function (source, dir) {
// Git mode
let branchExists = execSync(`git ls-remote --heads ${source.url} ${branch}`, { cwd: checkoutDir }).toString();
if (!branchExists) console.log(`Warning! Branch ${branch} doesn't exist in remote repository ${source.url}, defaulting to ${defaultBranch}`);
else console.log(`Using branch ${branch} for ${source.url}, checking out to ${dir}`);
if (!pathExistsSync(dir)) {
execSync(`git clone --depth=1 --branch=${branchExists ? branch : defaultBranch} "${source.url}" "${dir}"`, { cwd: checkoutDir, stdio: 'inherit' });
}
else {
execSync(`git checkout ${branchExists ? branch : defaultBranch}`, { cwd: dir, stdio: 'inherit' });
execSync('git pull', { cwd: dir, stdio: 'inherit' });
}
},
ganache: async function (baseOpts) {
console.log('Starting ganache...');
if (!(typeof baseOpts === 'string' || baseOpts instanceof String)) {
process.env.TEST_NO_BUILD = true;
baseOpts = await setup.apps(true);
}
let folder = resolve(baseOpts, 'packages', 'new-polymath-scripts');
if (!existsSync(folder))
throw `Can't find new-polymath-scripts`;
let path = setNodeVersion();
findAndKill(8545);
if (!process.env.TEST_NO_STARTUP) execSync('yarn --network-timeout=100000', { cwd: folder, stdio: 'inherit', env: { ...process.env, PATH: path, NODE_ENV: 'development' } });
path = `${resolve(folder, 'node_modules', '.bin')}${charSep}${path}`;
path = `${resolve(baseOpts, 'node_modules', '.bin')}${charSep}${path}`;
console.log(`Using path: ${path}`);
let pid = exec(`yarn local-blockchain:start`, { cwd: folder, env: { ...process.env, PATH: path, NODE_ENV: 'development' } });
let file = createWriteStream(logs.ganache);
await new Promise((r, e) => {
let oldWrite = file.write;
let seenOn = false;
file.write = (data, error) => {
oldWrite.call(file, data, error);
console.log(data);
if (file.write != oldWrite) {
if (data.indexOf('Listening on') !== -1) {
console.log(`Ganache is listening on ${data}, waiting for init script to finish...`);
seenOn = true;
}
if (seenOn && data.indexOf('child process exited with code') !== -1) {
file.write = oldWrite;
r();
}
if (data.toLowerCase().indexOf('error') !== -1) {
file.write = oldWrite;
e(data);
}
}
}
pid.stdout.pipe(file);
pid.stderr.pipe(file);
});
pids.ganache = pid;
writeFileSync(pidsFile, Object.values(pids).map(p => p.pid).join('\n'));
console.log(`Ganache started with pid ${pid.pid}`);
},
offchain: async function (baseOpts) {
console.log('Starting offchain...');
// TODO: Reset offchain on test end
let folder = `${baseOpts}/packages/polymath-offchain`;
let path = setNodeVersion();
process.env.TEST_MONGO_DIRECTORY = resolve(__dirname, 'mongo');
let port = 3001;
findAndKill(port);
let db = resolve(checkoutDir, 'mongo');
mkdirpSync(db);
let mongodHelper = new MongodHelper([
'--port', "27017",
'--dbpath', db,
]);
await mongodHelper.run().then((started) => {
console.log('mongod is running');
mongo = mongodHelper;
}, (e) => {
console.log('error starting mongodb', e);
});
if (!process.env.TEST_NO_STARTUP) execSync('yarn --network-timeout=100000', { cwd: folder, stdio: 'inherit', env: { ...process.env, PATH: path, NODE_ENV: 'development' } });
path = `${resolve(folder, 'node_modules', '.bin')}${charSep}${path}`;
path = `${resolve(baseOpts, 'node_modules', '.bin')}${charSep}${path}`;
console.log(`Using path: ${path}`);
let pid = exec(`yarn start`, { cwd: folder, env: { ...process.env, PATH: path, PORT: port, NODE_ENV: 'development' } });
let file = createWriteStream(logs.offchain);
await new Promise((r, e) => {
let oldWrite = file.write;
file.write = (data, error) => {
oldWrite.call(file, data, error);
console.log(data);
if (file.write != oldWrite) {
if (data.indexOf('Server is listening on port') !== -1) {
file.write = oldWrite;
r();
}
if (data.toLowerCase().indexOf('error') !== -1) {
file.write = oldWrite;
e(data);
}
}
}
pid.stdout.pipe(file);
pid.stderr.pipe(file);
});
pids.offchain = pid;
writeFileSync(pidsFile, Object.values(pids).map(p => p.pid).join('\n'));
console.log(`Offchain started with pid ${pid.pid}`);
},
issuer: async function (baseOpts) {
console.log('Starting issuer...');
let port = 3000;
findAndKill(port);
let pid = exec(`yarn serve -s "${baseOpts}/packages/polymath-issuer/build"`, { cwd: currentDir, env: { ...process.env, PORT: port } });
let file = createWriteStream(logs.issuer);
pid.stdout.pipe(file);
pid.stderr.pipe(file);
pids.issuer = pid;
writeFileSync(pidsFile, Object.values(pids).map(p => p.pid).join('\n'));
console.log(`Issuer started with pid ${pid.pid}`);
},
investor: async function (baseOpts) {
console.log('Starting investor...');
let port = 3002;
findAndKill(port);
let pid = exec(`yarn serve -s "${baseOpts}/packages/polymath-investor/build"`, { cwd: currentDir, env: { ...process.env, PORT: port } });
let file = createWriteStream(logs.investor);
pid.stdout.pipe(file);
pid.stderr.pipe(file);
pids.investor = pid;
writeFileSync(pidsFile, Object.values(pids).map(p => p.pid).join('\n'));
console.log(`Investor started with pid ${pid.pid}`);
},
apps: async function (baseOpts) {
let folder = resolve(checkoutDir, 'apps');
if (baseOpts === true) await this.git(sources.apps, folder);
else folder = baseOpts;
folder = resolve(folder);
if (!process.env.TEST_SKIP_OFFCHAIN) {
process.env.REACT_APP_POLYMATH_OFFCHAIN_ADDRESS = `http://${process.env.TEST_LOCALHOST}:3001`;
process.env.POLYMATH_OFFCHAIN_URL = `http://${process.env.TEST_LOCALHOST}:3001`;
process.env.POLYMATH_ISSUER_URL = `http://${process.env.TEST_LOCALHOST}:3000`;
process.env.WEB3_NETWORK_LOCAL_WS = `ws://localhost:8545`;
process.env.TEST_MONGODB_URI = `mongodb://localhost:27017/polymath`;
process.env.REACT_APP_DEPLOYMENT_STAGE = `local`;
}
process.env.REACT_APP_NETWORK_LOCAL_WS = `ws://${process.env.TEST_LOCALHOST}:8545`;
if (!process.env.TEST_NO_STARTUP && existsSync(resolve(folder, 'package.json'))) {
console.log('Installing apps...');
let path = setNodeVersion();
execSync('yarn --network-timeout=100000', { cwd: folder, stdio: 'inherit', env: { ...process.env, PATH: path, NODE_ENV: 'development' } });
path = `${resolve(folder, 'node_modules', '.bin')}${charSep}${path}`;
// This should be removed in the near future
//process.env.REACT_APP_NODE_WS = `ws://${process.env.TEST_LOCALHOST}:8545`;
if (!process.env.TEST_NO_BUILD) execSync('yarn build:apps', { cwd: folder, stdio: 'inherit', env: { ...process.env, PATH: path, NODE_ENV: 'development' } });
}
return folder;
},
all: function (folder) {
deasync(async function (callback) {
try {
folder = await setup.apps(folder);
await setup.ganache(folder);
if (!process.env.TEST_SKIP_OFFCHAIN) await setup.offchain(folder);
await setup.issuer(folder);
await setup.investor(folder);
callback(null);
} catch (error) {
callback(error);
}
})();
}
}
const kill = () => {
if (mongo) {
mongo.closeHandler(0);
mongo = null;
}
if (!pids) return;
console.log('Killing processes...');
for (let proc in pids) {
try {
treeKill(pids[proc].pid, 'SIGKILL');
} catch (error) {
console.log(`Error while terminating process ${proc}: ${error}`);
}
}
pids = null;
removeSync(pidsFile);
if (process.env.TEST_PRINT_LOGS) for (let log in logs) {
console.log(`Printing output of ${log}: ${logs[log]}`);
console.log(readFileSync(logs[log], 'utf8'));
}
};
process.on('SIGINT', function () {
console.log("Caught interrupt signal, exiting...");
kill();
process.exit(0);
});
process.on('exit', function () {
kill();
});
if (argv.params.setup.ganache) {
deasync(async function (callback) {
try {
await setup.ganache(argv.params.setup.ganache);
callback(null);
} catch (error) {
callback(error);
}
})();
} else if (argv.params.setup.apps) {
setup.all(argv.params.setup.apps);
} else {
throw `Unknown parameter for setup ${JSON.stringify(argv.params.setup)}`;
}
module.exports = kill;
console.log(`Setup complete, started the following processes: ${Object.entries(pids).map(e => e[0] + ': ' + e[1].pid).join(', ')}
Press Ctrl+C to terminate them.`);