gen-config.js 1.8 KB
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;
          }
        });
      }
    });
  });
};