1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const fs = require('fs');
|
12 | const path = require('path');
|
13 | const cp = require('child_process');
|
14 |
|
15 | exports.run = async function({dir = '.', environment, port, debug} /*: any */) {
|
16 | if (debug && !process.env.__FUSION_DEBUGGING__) {
|
17 | const command = process.argv.shift();
|
18 | const args = process.argv;
|
19 | args.unshift('--inspect-brk');
|
20 | return cp.spawn(command, args, {
|
21 | stdio: 'inherit',
|
22 | env: {
|
23 | ...process.env,
|
24 | __FUSION_DEBUGGING__: true,
|
25 | },
|
26 | });
|
27 | }
|
28 |
|
29 | const getEntry = env => {
|
30 | const entryPath = `.fusion/dist/${env}/server/server-main.js`;
|
31 | return path.resolve(dir, entryPath);
|
32 | };
|
33 |
|
34 | const env = environment
|
35 | ? fs.existsSync(getEntry(environment)) && environment
|
36 | : ['development', 'production'].find(e => fs.existsSync(getEntry(e)));
|
37 |
|
38 | if (env) {
|
39 | const entry = getEntry(env);
|
40 |
|
41 | const {start} = require(entry);
|
42 | return start({dir, port: port || process.env.PORT_HTTP || 3000});
|
43 | } else {
|
44 | throw new Error(`App can't start. JS isn't compiled`);
|
45 | }
|
46 | };
|