1 | #!/usr/bin/env node
|
2 |
|
3 | const fs = require('fs-extra')
|
4 | const path = require('path')
|
5 | const program = require('commander')
|
6 | const chalk = require('chalk')
|
7 |
|
8 | const { keyFileProjectConfigTemp, keyConfigQuiet, filenameBuilding } = require('../defaults/before-build')
|
9 |
|
10 | const __ = require('../utils/translate')
|
11 |
|
12 | const sleep = require('../utils/sleep')
|
13 | const setEnvFromCommand = require('../utils/set-env-from-command')
|
14 | const getAppType = require('../utils/get-app-type')
|
15 | const validateConfig = require('../libs/validate-config')
|
16 | const validateConfigDist = require('../libs/validate-config-dist')
|
17 | const spinner = require('../utils/spinner')
|
18 | const initNodeEnv = require('../utils/init-node-env')
|
19 |
|
20 | const kootBuild = require('../core/webpack/enter')
|
21 |
|
22 | program
|
23 | .version(require('../package').version, '-v, --version')
|
24 | .usage('[options]')
|
25 | .option('-c, --client', 'Set STAGE to CLIENT')
|
26 | .option('-s, --server', 'Set STAGE to SERVER')
|
27 | .option('--stage <stage>', 'Set STAGE')
|
28 | .option('--env <env>', 'Set ENV')
|
29 | .option('--dest <destination-path>', 'Set destination directory')
|
30 | .option('--config <config-file-path>', 'Set config file pathname')
|
31 | .option('--type <project-type>', 'Set project type')
|
32 | .option('--koot-dev', 'Koot dev mode')
|
33 | .option('--koot-test', 'Koot test mode')
|
34 | .parse(process.argv)
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | const isFromCommandStart = () => (process.env.KOOT_COMMAND_START && JSON.parse(process.env.KOOT_COMMAND_START))
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | const run = async () => {
|
45 |
|
46 | const {
|
47 | client, server,
|
48 | stage: _stage,
|
49 | env = 'prod',
|
50 | config,
|
51 | type,
|
52 | dest,
|
53 | kootDev = false,
|
54 | kootTest = false,
|
55 | } = program
|
56 |
|
57 | initNodeEnv()
|
58 |
|
59 |
|
60 |
|
61 | const fromCommandStart = isFromCommandStart()
|
62 | const fromOtherCommand = kootDev || fromCommandStart
|
63 | if (!fromOtherCommand)
|
64 |
|
65 | process.stdout.write('\x1B[2J\x1B[0f')
|
66 |
|
67 | setEnvFromCommand({
|
68 | config,
|
69 | type,
|
70 | }, fromOtherCommand)
|
71 |
|
72 | process.env.KOOT_TEST_MODE = JSON.stringify(kootTest)
|
73 |
|
74 | const stage = (() => {
|
75 | if (_stage) return _stage
|
76 | if (client) return 'client'
|
77 | if (server) return 'server'
|
78 | return false
|
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 | process.env.WEBPACK_BUILD_STAGE = stage || 'client'
|
111 | process.env.WEBPACK_BUILD_ENV = env
|
112 |
|
113 |
|
114 | const buildConfig = await validateConfig()
|
115 | await getAppType()
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | if (dest) buildConfig.dist = validateConfigDist(dest)
|
122 |
|
123 |
|
124 | if (fromCommandStart) {
|
125 |
|
126 | buildConfig[keyConfigQuiet] = true
|
127 | }
|
128 |
|
129 |
|
130 |
|
131 | if (process.env.WEBPACK_BUILD_TYPE === 'spa' || stage) {
|
132 |
|
133 |
|
134 |
|
135 | await kootBuild(buildConfig)
|
136 | await after(buildConfig)
|
137 | if (!fromCommandStart) console.log(' ')
|
138 | return
|
139 | }
|
140 |
|
141 |
|
142 | await kootBuild({ ...buildConfig })
|
143 | await sleep(100)
|
144 |
|
145 |
|
146 |
|
147 | if (!fromCommandStart) console.log('\n' + ''.padEnd(60, '=') + '\n')
|
148 | process.env.WEBPACK_BUILD_STAGE = 'server'
|
149 | await kootBuild({ ...buildConfig })
|
150 | await sleep(100)
|
151 |
|
152 | if (!fromCommandStart) console.log('\n' + ''.padEnd(60, '=') + '\n')
|
153 | if (!fromCommandStart) console.log(
|
154 | chalk.green('√ ')
|
155 | + chalk.yellowBright('[koot/build] ')
|
156 | + __('build.complete', {
|
157 | time: (new Date()).toLocaleString()
|
158 | })
|
159 | )
|
160 |
|
161 | await after(buildConfig)
|
162 | if (!fromCommandStart) console.log(' ')
|
163 | }
|
164 |
|
165 | const after = async (config = {}) => {
|
166 | const ENV = process.env.WEBPACK_BUILD_ENV
|
167 |
|
168 | const {
|
169 | dist,
|
170 | [keyFileProjectConfigTemp]: fileProjectConfigTemp
|
171 | } = config
|
172 |
|
173 |
|
174 | if (ENV === 'prod' && fileProjectConfigTemp) {
|
175 | await fs.remove(fileProjectConfigTemp)
|
176 | }
|
177 |
|
178 |
|
179 | const fileBuilding = path.resolve(dist, filenameBuilding)
|
180 |
|
181 | if (fs.existsSync(fileBuilding))
|
182 | await fs.remove(fileBuilding)
|
183 | }
|
184 |
|
185 | run().catch(err => {
|
186 | if (isFromCommandStart()) {
|
187 |
|
188 | return console.error(err)
|
189 | }
|
190 | spinner(chalk.yellowBright('[koot/build] ')).fail()
|
191 | console.trace(err)
|
192 | })
|