1 | #!/usr/bin/env node
|
2 |
|
3 | const fs = require('fs-extra')
|
4 | const path = require('path')
|
5 | const program = require('commander')
|
6 | const pm2 = require('pm2')
|
7 | const chalk = require('chalk')
|
8 | const npmRunScript = require('npm-run-script')
|
9 | const opn = require('opn')
|
10 |
|
11 | const __ = require('../utils/translate')
|
12 | const sleep = require('../utils/sleep')
|
13 | const getPort = require('../utils/get-port')
|
14 | const spinner = require('../utils/spinner')
|
15 | const readBuildConfigFile = require('../utils/read-build-config-file')
|
16 | const getAppType = require('../utils/get-app-type')
|
17 | const setEnvFromCommand = require('../utils/set-env-from-command')
|
18 | const getChunkmapPath = require('../utils/get-chunkmap-path')
|
19 | const initNodeEnv = require('../utils/init-node-env')
|
20 |
|
21 | program
|
22 | .version(require('../package').version, '-v, --version')
|
23 | .usage('[options]')
|
24 | .option('-c, --client', 'Set STAGE to CLIENT')
|
25 | .option('-s, --server', 'Set STAGE to SERVER')
|
26 | .option('--stage <stage>', 'Set STAGE')
|
27 | .option('--config <config-file-path>', 'Set config file')
|
28 | .option('--type <project-type>', 'Set project type')
|
29 | .parse(process.argv)
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | const run = async () => {
|
45 |
|
46 | process.stdout.write('\x1B[2J\x1B[0f')
|
47 |
|
48 | const {
|
49 | client, server,
|
50 | stage: _stage,
|
51 | config,
|
52 | type,
|
53 | } = program
|
54 |
|
55 | initNodeEnv()
|
56 | setEnvFromCommand({
|
57 | config,
|
58 | type,
|
59 | })
|
60 |
|
61 | let stage = _stage ? _stage : (client ? 'client' : (server ? 'server' : false))
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | const appType = await getAppType()
|
76 | const packageInfo = await fs.readJson(path.resolve(process.cwd(), 'package.json'))
|
77 | const { dist, port } = await readBuildConfigFile()
|
78 | const {
|
79 | name
|
80 | } = packageInfo
|
81 |
|
82 |
|
83 | if (process.env.WEBPACK_BUILD_TYPE === 'spa') {
|
84 | process.env.WEBPACK_BUILD_STAGE = 'client'
|
85 | stage = 'client'
|
86 | }
|
87 |
|
88 |
|
89 | if (typeof port !== 'undefined')
|
90 | process.env.SERVER_PORT = getPort(port, 'dev')
|
91 |
|
92 |
|
93 | if (stage) {
|
94 | const cmd = `super-build --stage ${stage} --env dev`
|
95 | const child = npmRunScript(cmd, {})
|
96 | child.once('error', (error) => {
|
97 | console.trace(error)
|
98 | process.exit(1)
|
99 | })
|
100 | child.once('exit', () => {
|
101 |
|
102 |
|
103 | })
|
104 | return
|
105 | } else {
|
106 |
|
107 |
|
108 | let waitingSpinner = false
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | const processes = []
|
119 | const pathChunkmap = getChunkmapPath(dist)
|
120 | const pathServerJS = path.resolve(dist, 'server/index.js')
|
121 | const contentWaiting = '// WAITING FOR SERVER BUNDLING'
|
122 |
|
123 | {
|
124 | process.stdin.resume()
|
125 | const exitHandler = async () => {
|
126 |
|
127 | if (Array.isArray(processes) && processes.length) {
|
128 | if (waitingSpinner) waitingSpinner.stop()
|
129 | await sleep(300)
|
130 | process.stdout.write('\x1B[2J\x1B[0f')
|
131 | const w = spinner('WAITING FOR ENDING')
|
132 | await Promise.all(processes.map(proc =>
|
133 | new Promise((resolve/*, reject*/) => {
|
134 | setTimeout(() => {
|
135 | processes.splice(processes.indexOf(proc), 1)
|
136 | resolve()
|
137 | }, 500)
|
138 |
|
139 | pm2.delete(proc)
|
140 | })
|
141 | ))
|
142 | await Promise.all(processes.map(proc =>
|
143 | new Promise((resolve/*, reject*/) => {
|
144 | setTimeout(() => resolve(), 500)
|
145 |
|
146 | pm2.delete(proc)
|
147 | })
|
148 | ))
|
149 |
|
150 | pm2.disconnect()
|
151 | await sleep(300)
|
152 | w.stop()
|
153 | try {
|
154 |
|
155 | process.exit(1)
|
156 |
|
157 | } catch (e) {
|
158 | console.log(e)
|
159 | }
|
160 | } else {
|
161 | process.removeListener('exit', exitHandler)
|
162 | process.removeListener('SIGINT', exitHandler)
|
163 | process.removeListener('SIGUSR1', exitHandler)
|
164 | process.removeListener('SIGUSR2', exitHandler)
|
165 | process.removeListener('uncaughtException', exitHandler)
|
166 |
|
167 | process.stdout.write('\x1B[2J\x1B[0f')
|
168 | console.log('Press CTRL+C again to terminate.')
|
169 | process.exit(1)
|
170 | }
|
171 | }
|
172 |
|
173 | process.on('exit', exitHandler);
|
174 |
|
175 | process.on('SIGINT', exitHandler);
|
176 |
|
177 | process.on('SIGUSR1', exitHandler);
|
178 | process.on('SIGUSR2', exitHandler);
|
179 |
|
180 | process.on('uncaughtException', exitHandler);
|
181 | }
|
182 |
|
183 |
|
184 | const start = (stage) => new Promise(async (resolve, reject) => {
|
185 | const pathLogOut = path.resolve(process.cwd(), `logs/dev/${stage}.log`)
|
186 | const pathLogErr = path.resolve(process.cwd(), `logs/dev/${stage}-error.log`)
|
187 | if (fs.existsSync(pathLogOut)) await fs.remove(pathLogOut)
|
188 | if (fs.existsSync(pathLogErr)) await fs.remove(pathLogErr)
|
189 | const config = {
|
190 | name: `dev-${stage}-${name}`,
|
191 | script: path.resolve(__dirname, './build.js'),
|
192 | args: `--stage ${stage} --env dev`,
|
193 | cwd: process.cwd(),
|
194 | output: pathLogOut,
|
195 | error: pathLogErr,
|
196 | autorestart: false,
|
197 | }
|
198 | if (stage === 'run') {
|
199 | config.script = pathServerJS
|
200 | config.watch = true
|
201 | delete config.args
|
202 | }
|
203 | processes.push(config.name)
|
204 | pm2.start(
|
205 | config,
|
206 | (err, proc) => {
|
207 |
|
208 | if (err) return reject(err)
|
209 | resolve(proc)
|
210 | }
|
211 | )
|
212 | })
|
213 |
|
214 |
|
215 | pm2.connect(true, async (err) => {
|
216 | if (err) {
|
217 |
|
218 | process.exit(2)
|
219 | }
|
220 |
|
221 | console.log(
|
222 | ` `
|
223 | + chalk.yellowBright('[super/build] ')
|
224 | + __('build.build_start', {
|
225 | type: chalk.cyanBright(appType),
|
226 | stage: chalk.green('client'),
|
227 | env: chalk.green('dev'),
|
228 | })
|
229 | )
|
230 |
|
231 |
|
232 | await fs.ensureFile(pathChunkmap)
|
233 | await fs.writeFile(pathChunkmap, contentWaiting)
|
234 |
|
235 |
|
236 | await fs.ensureFile(pathServerJS)
|
237 | await fs.writeFile(pathServerJS, contentWaiting)
|
238 |
|
239 |
|
240 | await start('client')
|
241 |
|
242 |
|
243 | await new Promise(resolve => {
|
244 | const waiting = () => setTimeout(async () => {
|
245 | if (!fs.existsSync(pathChunkmap)) return waiting()
|
246 | const content = await fs.readFile(pathChunkmap, 'utf-8')
|
247 | if (!content || content === contentWaiting) return waiting()
|
248 | await sleep(100)
|
249 | resolve()
|
250 | }, 500)
|
251 | waiting()
|
252 | })
|
253 |
|
254 | console.log(
|
255 | chalk.green('√ ')
|
256 | + chalk.yellowBright('[super/build] ')
|
257 | + __('build.build_complete', {
|
258 | type: chalk.cyanBright(appType),
|
259 | stage: chalk.green('client'),
|
260 | env: chalk.green('dev'),
|
261 | })
|
262 | )
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 | console.log(
|
274 | ` `
|
275 | + chalk.yellowBright('[super/build] ')
|
276 | + __('build.build_start', {
|
277 | type: chalk.cyanBright(appType),
|
278 | stage: chalk.green('server'),
|
279 | env: chalk.green('dev'),
|
280 | })
|
281 | )
|
282 | await start('server')
|
283 |
|
284 |
|
285 | await new Promise(resolve => {
|
286 | const waiting = () => setTimeout(async () => {
|
287 | if (!fs.existsSync(pathServerJS)) return waiting()
|
288 | const content = await fs.readFile(pathServerJS, 'utf-8')
|
289 | if (!content || content === contentWaiting) return waiting()
|
290 | await sleep(100)
|
291 | resolve()
|
292 | }, 500)
|
293 | waiting()
|
294 | })
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | await start('run')
|
303 | await sleep(500)
|
304 |
|
305 | console.log(
|
306 | chalk.green('√ ')
|
307 | + chalk.yellowBright('[super/build] ')
|
308 | + __('build.build_complete', {
|
309 | type: chalk.cyanBright(appType),
|
310 | stage: chalk.green('server'),
|
311 | env: chalk.green('dev'),
|
312 | })
|
313 | )
|
314 |
|
315 |
|
316 |
|
317 | npmRunScript(`pm2 logs`)
|
318 | opn(`http://localhost:${process.env.SERVER_PORT}/`)
|
319 | })
|
320 | }
|
321 | }
|
322 |
|
323 | run()
|