1 | #!/usr/bin/env node
|
2 |
|
3 | const fs = require('fs-extra')
|
4 | const program = require('commander')
|
5 | const chalk = require('chalk')
|
6 |
|
7 | const { keyFileProjectConfigTemp } = require('../defaults/before-build')
|
8 |
|
9 | const __ = require('../utils/translate')
|
10 |
|
11 | const sleep = require('../utils/sleep')
|
12 | const setEnvFromCommand = require('../utils/set-env-from-command')
|
13 | const validateConfig = require('../libs/validate-config')
|
14 |
|
15 | const kootBuild = require('../core/webpack/enter')
|
16 |
|
17 | program
|
18 | .version(require('../package').version, '-v, --version')
|
19 | .usage('[options]')
|
20 | .option('-c, --client', 'Set STAGE to CLIENT')
|
21 | .option('-s, --server', 'Set STAGE to SERVER')
|
22 | .option('--stage <stage>', 'Set STAGE')
|
23 | .option('--env <env>', 'Set ENV')
|
24 | .option('--dest <destination-path>', 'Set destination directory')
|
25 | .option('--config <config-file-path>', 'Set config file pathname')
|
26 | .option('--type <project-type>', 'Set project type')
|
27 | .option('--koot-test', 'Koot test mode')
|
28 | .parse(process.argv)
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | const run = async () => {
|
34 |
|
35 |
|
36 | process.stdout.write('\x1B[2J\x1B[0f')
|
37 |
|
38 | const {
|
39 | client, server,
|
40 | stage: _stage,
|
41 | env = 'prod',
|
42 | config,
|
43 | type,
|
44 | dest,
|
45 | kootTest = false
|
46 | } = program
|
47 |
|
48 |
|
49 | setEnvFromCommand({
|
50 | config,
|
51 | type,
|
52 | })
|
53 |
|
54 | process.env.KOOT_TEST_MODE = JSON.stringify(kootTest)
|
55 |
|
56 | const stage = _stage ? _stage : (client ? 'client' : (server ? 'server' : false))
|
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 | process.env.WEBPACK_BUILD_STAGE = stage || 'client'
|
88 | process.env.WEBPACK_BUILD_ENV = env
|
89 |
|
90 |
|
91 | const buildConfig = await validateConfig()
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | if (dest) buildConfig.dist = dest
|
98 |
|
99 |
|
100 | if (stage) {
|
101 |
|
102 |
|
103 |
|
104 | await kootBuild(buildConfig)
|
105 | await after(buildConfig)
|
106 | console.log(' ')
|
107 | return
|
108 | }
|
109 |
|
110 |
|
111 | await kootBuild({ ...buildConfig })
|
112 | await sleep(100)
|
113 |
|
114 |
|
115 |
|
116 | console.log('\n' + ''.padEnd(60, '=') + '\n')
|
117 | process.env.WEBPACK_BUILD_STAGE = 'server'
|
118 | await kootBuild({ ...buildConfig })
|
119 | await sleep(100)
|
120 |
|
121 | console.log('\n' + ''.padEnd(60, '=') + '\n')
|
122 | console.log(
|
123 | chalk.green('√ ')
|
124 | + chalk.yellowBright('[koot/build] ')
|
125 | + __('build.complete', {
|
126 | time: (new Date()).toLocaleString()
|
127 | })
|
128 | )
|
129 |
|
130 | await after(buildConfig)
|
131 | console.log(' ')
|
132 | }
|
133 |
|
134 | const after = async (config = {}) => {
|
135 | const ENV = process.env.WEBPACK_BUILD_ENV
|
136 |
|
137 | const {
|
138 | [keyFileProjectConfigTemp]: fileProjectConfigTemp
|
139 | } = config
|
140 |
|
141 |
|
142 | if (ENV === 'prod' && fileProjectConfigTemp) {
|
143 | await fs.remove(fileProjectConfigTemp)
|
144 | }
|
145 | }
|
146 |
|
147 | run()
|