gen-config.js
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const inquirer = require('inquirer');
const shelljs = require('shelljs');
const path = require('path');
const _ = require('lodash');
const fs = require('fs');
module.exports = ({code_path, env}) => {
const dirs = shelljs.ls(path.join(__dirname, '../mode'));
return inquirer.prompt([
{
type: 'list',
name: 'mode',
message: '选择小程序主体开发主体',
choices: dirs
}
]).then(({mode}) => {
const configs = shelljs.ls(path.join(__dirname, '../mode', mode));
_.each(configs, c => {
if (/\.js$/.test(c)) {
const fileName = c.replace(/\.js$/, '');
const baseConfig = require(path.join(__dirname, '../config', `${fileName}.base.js`));
let envConfig, modeConfig;
if (env === 'production') {
envConfig = require(path.join(__dirname, '../config', `${fileName}.prod.js`));
} else {
envConfig = require(path.join(__dirname, '../config', `${fileName}.dev.js`));
}
modeConfig = require(path.join(__dirname, '../mode', mode, `${fileName}.js`));
const fullConfig = _.merge(baseConfig, envConfig, modeConfig);
fs.writeFile(path.join(__dirname, '../', code_path, c),
`/* eslint-disable */
export default ${JSON.stringify(fullConfig, null, 2)}`,
err => {
if (err) {
throw err;
}
});
} else if (/\.json$/.test(c)) {
const baseConfig = require(path.join(__dirname, '../config', c));
const modeConfig = require(path.join(__dirname, '../mode', mode, c));
const fullConfig = _.merge(baseConfig, modeConfig);
fs.writeFile(path.join(__dirname, '../', code_path, c), JSON.stringify(fullConfig, null, 2), err => {
if (err) {
throw err;
}
});
}
});
});
};