-
Notifications
You must be signed in to change notification settings - Fork 98
/
startup.js
153 lines (137 loc) · 4.73 KB
/
startup.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
const { spawnSync, spawn } = require('child_process');
const resolve = require('path').resolve;
const projectRootDir = process.cwd();
let fs = require('fs');
(async () => {
const scriptName = process.argv[2] ? process.argv[2] : 'production';
const noInstall = process.argv.includes('--no-install');
const onlySystem = process.argv.includes('--system');
const spawnOpts = { shell: true, stdio: 'inherit' };
const isCoreBuilt = () => {
return !(
!fs.existsSync(resolve(coreDir, 'common/dist')) ||
!fs.existsSync(resolve(coreDir, 'backend/dist')) ||
!fs.existsSync(resolve(coreDir, 'frontend/dist')) ||
!fs.existsSync(resolve(coreDir, 'common/es')) ||
!fs.existsSync(resolve(coreDir, 'backend/es')) ||
!fs.existsSync(resolve(coreDir, 'frontend/es'))
);
};
const coreDir = resolve(projectRootDir, 'system/core');
const rootNodeModulesDir = resolve(projectRootDir, 'node_modules');
const managerStartupPath = resolve(projectRootDir, 'system/manager/startup.js');
const adminStartupPath = resolve(projectRootDir, 'system/admin/startup.js');
const serverStartupPath = resolve(projectRootDir, 'system/server/startup.js');
const cliStartupPath = resolve(projectRootDir, 'system/cli/startup.js');
const backendNode_modules = resolve(coreDir, 'backend/node_modules');
const frontendNode_modules = resolve(coreDir, 'frontend/node_modules');
const hasNodeModules = () => {
return !(
!fs.existsSync(rootNodeModulesDir) ||
!fs.existsSync(backendNode_modules) ||
!fs.existsSync(frontendNode_modules)
);
};
// Check node_modules
if ((!hasNodeModules() || scriptName === 'build') && !noInstall) {
spawnSync(`npm i -g yarn`, spawnOpts);
spawnSync(`yarn`, spawnOpts);
fs = require('fs-extra');
} else {
fs = require('fs-extra');
}
// Build core
if (!isCoreBuilt() || scriptName === 'build') {
console.log('\x1b[36m%s\x1b[0m', 'Building Core...');
try {
spawnSync('npm run build', {
shell: true,
cwd: resolve(coreDir, 'common'),
stdio: 'inherit',
});
spawnSync('npm run build', {
shell: true,
cwd: resolve(coreDir, 'backend'),
stdio: 'inherit',
});
spawnSync('npm run build', {
shell: true,
cwd: resolve(coreDir, 'frontend'),
stdio: 'inherit',
});
} catch (e) {
console.log(e);
console.log('\x1b[31m%s\x1b[0m', 'Cromwell::startup. Failed to build Core');
throw new Error('Failed to build Core');
}
if (!isCoreBuilt()) {
console.log('\x1b[31m%s\x1b[0m', 'Cromwell::startup. Failed to build Core');
throw new Error('Failed to build Core');
}
}
// Builds manager, renderer and utils
const managerCommand = scriptName === 'build' ? 'buildService' : 'check';
spawnSync(`node ${managerStartupPath} ${managerCommand}`, {
shell: true,
cwd: projectRootDir,
stdio: 'inherit',
});
// Build cli
spawnSync(`node ${cliStartupPath} ${managerCommand}`, {
shell: true,
cwd: projectRootDir,
stdio: 'inherit',
});
if (scriptName === 'build') {
// Build server
spawnSync(`node ${serverStartupPath} build`, {
shell: true,
cwd: projectRootDir,
stdio: 'inherit',
});
// Build admin
spawnSync(`node ${adminStartupPath} build`, {
shell: true,
cwd: projectRootDir,
stdio: 'inherit',
});
}
const buildAllPackagesInDir = (dir, buildDirName = 'build', pckgType) => {
if (onlySystem || !fs.existsSync(dir)) return;
const packages = fs.readdirSync(dir);
for (let i = 0; i < packages.length; i++) {
const pckg = packages[i];
const pckgDir = resolve(dir, pckg);
if (fs.existsSync(resolve(pckgDir, 'package.json'))) {
if (!fs.existsSync(resolve(pckgDir, buildDirName)) || scriptName === 'build') {
console.log('\x1b[36m%s\x1b[0m', `Building ${pckg} ${pckgType ?? ''}...`);
spawnSync('npm run build', {
shell: true,
cwd: pckgDir,
stdio: 'inherit',
});
}
}
}
};
// Check toolkits
buildAllPackagesInDir(resolve(projectRootDir, 'toolkits'), 'dist', 'toolkit');
// Check themes
buildAllPackagesInDir(resolve(projectRootDir, 'themes'), 'build', 'theme');
// Check plugins
buildAllPackagesInDir(resolve(projectRootDir, 'plugins'), 'build', 'plugin');
// Start system
if (scriptName !== 'build') {
try {
spawn(`node ${managerStartupPath} ${scriptName}`, {
shell: true,
cwd: projectRootDir,
stdio: 'inherit',
});
} catch (e) {
console.log(e);
console.log('\x1b[31m%s\x1b[0m', 'Cromwell::startup. Manager: Failed to Start system');
throw new Error('Failed to Start system');
}
}
})();