1 | #!/usr/bin/env node
|
2 |
|
3 | const fs = require('fs-extra')
|
4 | const path = require('path')
|
5 | const program = require('commander')
|
6 |
|
7 |
|
8 | const before = require('./_before')
|
9 |
|
10 |
|
11 | const validateConfig = require('../libs/validate-config')
|
12 |
|
13 | const setEnvFromCommand = require('../utils/set-env-from-command')
|
14 | const initNodeEnv = require('../utils/init-node-env')
|
15 | const getDirTemp = require('../libs/get-dir-tmp')
|
16 |
|
17 | const kootWebpackBuild = require('koot-webpack/build')
|
18 |
|
19 | program
|
20 | .version(require('../package').version, '-v, --version')
|
21 | .usage('[options]')
|
22 | .option('-c, --client', 'Set STAGE to CLIENT')
|
23 | .option('-s, --server', 'Set STAGE to SERVER')
|
24 | .option('--stage <stage>', 'Set STAGE')
|
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 | const run = async () => {
|
31 |
|
32 | process.stdout.write('\x1B[2J\x1B[0f')
|
33 |
|
34 | const {
|
35 | client, server,
|
36 | stage: _stage,
|
37 | config,
|
38 | type,
|
39 | } = program
|
40 |
|
41 | initNodeEnv()
|
42 |
|
43 |
|
44 | setEnvFromCommand({
|
45 | config,
|
46 | type,
|
47 | })
|
48 |
|
49 | const stage = _stage ||
|
50 | (client ? 'client' : undefined) ||
|
51 | (server ? 'server' : undefined) ||
|
52 | 'client'
|
53 |
|
54 | process.env.WEBPACK_BUILD_STAGE = stage || 'client'
|
55 | process.env.WEBPACK_BUILD_ENV = 'prod'
|
56 |
|
57 | await before(program)
|
58 |
|
59 |
|
60 | const dirAnalyzeBuild = require('../libs/get-dir-dev-tmp')(undefined, 'analyze')
|
61 | await fs.ensureDir(dirAnalyzeBuild)
|
62 | await fs.emptyDir(dirAnalyzeBuild)
|
63 | await fs.ensureDir(path.resolve(dirAnalyzeBuild, 'public'))
|
64 | await fs.ensureDir(path.resolve(dirAnalyzeBuild, 'server'))
|
65 |
|
66 |
|
67 | const kootConfig = {
|
68 | ...await validateConfig(),
|
69 |
|
70 | dist: dirAnalyzeBuild,
|
71 | bundleVersionsKeep: false,
|
72 | }
|
73 |
|
74 | await kootWebpackBuild({
|
75 | analyze: true,
|
76 | ...kootConfig
|
77 | })
|
78 |
|
79 |
|
80 | await fs.remove(getDirTemp())
|
81 |
|
82 |
|
83 | await fs.remove(dirAnalyzeBuild)
|
84 |
|
85 | console.log(' ')
|
86 | }
|
87 |
|
88 | run()
|