1 | const fs = require('fs-extra')
|
2 | const sh = require('shelljs')
|
3 | const path = require('path')
|
4 |
|
5 | const env = process.env.NODE_ENV
|
6 | const isEnvDevelopment = env === 'development'
|
7 | const isEnvTest = env === 'test'
|
8 | const isEnvProduction = env === 'production'
|
9 |
|
10 | const appDirectory = fs.realpathSync(process.cwd())
|
11 | const resolveApp = relativePath => path.resolve(appDirectory, relativePath)
|
12 |
|
13 | function shellExec(com) {
|
14 | if (sh.exec(com).code !== 0) {
|
15 | sh.exit(1)
|
16 | }
|
17 | }
|
18 |
|
19 | const getLocalConfig = () => {
|
20 | let config = {}
|
21 | try {
|
22 | config = require(appDirectory + '/config/local')
|
23 | } catch (err) {
|
24 |
|
25 | }
|
26 |
|
27 | return config
|
28 | }
|
29 |
|
30 | const getConfig = () => {
|
31 | let config
|
32 | try {
|
33 | config = require(appDirectory + '/config/deploy')
|
34 | } catch (err) {
|
35 | throw new Error('没有找到 /config/deploy.js 文件')
|
36 | }
|
37 |
|
38 |
|
39 | if (isEnvDevelopment) {
|
40 | config.publicPath = '/build/'
|
41 | }
|
42 |
|
43 |
|
44 | Object.assign(config, getLocalConfig())
|
45 |
|
46 |
|
47 | if (!isEnvDevelopment && !config.publicPath) {
|
48 | throw new Error('production 没有提供 publicPath 字段')
|
49 | }
|
50 |
|
51 | return config
|
52 | }
|
53 |
|
54 | const PATH = {
|
55 | appDirectory,
|
56 | appBuild: resolveApp('build'),
|
57 | appConfig: resolveApp('config'),
|
58 | appSrc: resolveApp('src'),
|
59 | appIndexTemplate: resolveApp('src/index.html'),
|
60 | appIndexJs: resolveApp('src/index.js'),
|
61 | appPackageJson: resolveApp('package.json'),
|
62 | }
|
63 |
|
64 |
|
65 | const commonInclude = [
|
66 | PATH.appSrc,
|
67 | /gm-/,
|
68 | /gmfe/,
|
69 | /gm-touch/,
|
70 | /gm-common/,
|
71 | /gm_static_storage/,
|
72 | /react-mgm/,
|
73 | /query-string/,
|
74 | /split-on-first/,
|
75 | /strict-uri-encode/,
|
76 | ]
|
77 |
|
78 | const packageJson = JSON.parse(fs.readFileSync(PATH.appPackageJson))
|
79 |
|
80 | module.exports = {
|
81 | isEnvDevelopment,
|
82 | isEnvTest,
|
83 | isEnvProduction,
|
84 | appDirectory,
|
85 | PATH,
|
86 | packageJson,
|
87 | commonInclude,
|
88 | shellExec,
|
89 | getConfig,
|
90 | }
|