forked from erda-project/erda-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
101 lines (88 loc) · 3.47 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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
const fs = require('fs');
const { resolve } = require('path');
const { join } = require('path');
const cp = require('child_process');
const os = require('os');
const process = require('process')
// get library path
const root = resolve(__dirname, '.');
// runCmd binary based on OS
const yarnCmd = os.platform().startsWith('win') ? 'yarn.cmd' : 'yarn';
const npmCmd = os.platform().startsWith('win') ? 'npm.cmd' : 'npm';
const runCmd = process.argv[2] === 'online' ? npmCmd : yarnCmd
const installArg = process.argv[2] === 'online' ? ['i'] : []
console.log(`==============${runCmd} mode=============`)
const coreDir = join(root, 'core');
const schedulerDir = join(root, 'scheduler');
const shellDir = join(root, 'shell');
const log = msg => {
console.log(`============================ ${msg} ============================\n`);
}
const installDependencies = () => {
if (!fs.existsSync(join(root, 'node_modules')) && process.argv[2] !== 'online') {
cp.spawnSync(runCmd, installArg, { env: process.env, cwd: root, stdio: 'inherit' });
return;
};
[
coreDir,
schedulerDir,
shellDir
].forEach((dir) => {
if (fs.existsSync(join(dir, 'node_modules'))) {
log(`${dir.split('/').pop()} dependencies have installed 😁`);
return;
};
log(`Performing "${runCmd}" inside ${dir} folder`);
// install dependencies
cp.spawnSync(runCmd, ['config', 'set', 'registry', 'https://registry.npm.terminus.io/'], { env: process.env, cwd: dir, stdio: 'inherit' });
cp.spawnSync(runCmd, installArg, { env: process.env, cwd: dir, stdio: 'inherit' });
});
}
const registerErdaCmd = async () => {
log('register erda-ui command globally 😁');
await cp.spawnSync(npmCmd, ['i', '-g', '@erda-ui/cli'], { env: process.env, stdio: 'inherit' });
log('register npm-check-updates globally to check npm version😁');
await cp.spawnSync(npmCmd, ['i', '-g', 'npm-check-updates'], { env: process.env, stdio: 'inherit' });
}
const setupCore = async (port) => {
log('create .erda/config in module core 😁');
await cp.spawnSync('erda-ui', ['setup', 'core', port], { env: process.env, cwd: coreDir, stdio: 'inherit' }) ;
}
const setupShell = async (port) => {
log('create .erda/config in module shell');
await cp.spawnSync('erda-ui', ['setup', 'shell', port], { env: process.env, cwd: shellDir, stdio: 'inherit' }) ;
}
const initEnvFile = async () => {
const envContent = `
DEV_HOST=https://terminus-org.dev.terminus.io
TEST_HOST=https://terminus-org.test.terminus.io
SCHEDULER_HOST=http://localhost
SCHEDULER_PORT=3000
DEV_MODULES=core,shell
PROD_MODULES=core,shell
SCHEDULER_DIR=${schedulerDir}
`
await fs.writeFileSync(join(root, '.env'), envContent, 'utf8', '0777');
log(`create .env file in ${root}`)
}
const setupModules = async () => {
await initEnvFile();
await registerErdaCmd();
await setupCore(5000);
await setupShell(8080);
}
// start
installDependencies();
setupModules();