UNPKG

1.86 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const fs = require('fs-extra')
4const program = require('commander')
5// const chalk = require('chalk')
6
7const before = require('./_before')
8
9// const __ = require('../utils/translate')
10const validateConfig = require('../libs/validate-config')
11// const readBuildConfigFile = require('../utils/read-build-config-file')
12const setEnvFromCommand = require('../utils/set-env-from-command')
13const initNodeEnv = require('../utils/init-node-env')
14const getDirTemp = require('../libs/get-dir-tmp')
15
16const kootBuild = require('../core/webpack/enter')
17
18program
19 .version(require('../package').version, '-v, --version')
20 .usage('[options]')
21 .option('-c, --client', 'Set STAGE to CLIENT')
22 .option('-s, --server', 'Set STAGE to SERVER')
23 .option('--stage <stage>', 'Set STAGE')
24 .option('--config <config-file-path>', 'Set config file pathname')
25 .option('--type <project-type>', 'Set project type')
26 .option('--koot-test', 'Koot test mode')
27 .parse(process.argv)
28
29const run = async () => {
30 // 清空 log
31 process.stdout.write('\x1B[2J\x1B[0f')
32
33 const {
34 client, server,
35 stage: _stage,
36 config,
37 type,
38 } = program
39
40 initNodeEnv()
41 // console.log(program)
42
43 setEnvFromCommand({
44 config,
45 type,
46 })
47
48 const stage = _stage ||
49 (client ? 'client' : undefined) ||
50 (server ? 'server' : undefined) ||
51 'client'
52
53 process.env.WEBPACK_BUILD_STAGE = stage || 'client'
54 process.env.WEBPACK_BUILD_ENV = 'prod'
55
56 await before()
57
58 // 读取构建配置
59 const kootConfig = await validateConfig()
60
61 await kootBuild({
62 analyze: true,
63 ...kootConfig
64 })
65
66 // 清理临时目录
67 await fs.remove(getDirTemp())
68
69 console.log(' ')
70}
71
72run()