index.js 2.1 KB
Newer Older
super-lin0's avatar
super-lin0 committed
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69
const debug = require('debug')('create-umi:generator');
const BasicGenerator = require('../../BasicGenerator');

class Generator extends BasicGenerator {
  prompting() {
    if (this.opts.args && 'isTypeScript' in this.opts.args && 'reactFeatures' in this.opts.args) {
      this.prompts = {
        isTypeScript: this.opts.args.isTypeScript,
        reactFeatures: this.opts.args.reactFeatures,
      };
    } else {
      const prompts = [
        {
          name: 'isTypeScript',
          type: 'confirm',
          message: 'Do you want to use typescript?',
          default: false,
        },
        {
          name: 'reactFeatures',
          message: 'What functionality do you want to enable?',
          type: 'checkbox',
          choices: [
            {name: 'antd', value: 'antd'},
            {name: 'dva', value: 'dva'},
            {name: 'code splitting', value: 'dynamicImport'},
            {name: 'dll', value: 'dll'},
            {name: 'internationalization', value: 'locale'},
          ],
        },
      ];
      return this.prompt(prompts).then(props => {
        this.prompts = props;
      });
    }
  }

  writing() {
    this.writeFiles({
      context: {
        name: this.name,
        ...this.prompts,
      },
      filterFiles: f => {
        const { isTypeScript, reactFeatures } = this.prompts;
        if (isTypeScript) {
          if (f.endsWith('.js')) return false;
          if (!reactFeatures.includes('dva')) {
            if (f.startsWith('src/models') || f === 'src/app.ts') return false;
          }
          if (!reactFeatures.includes('locale')) {
            if (f.startsWith('src/locales') || f.includes('umi-plugin-locale')) return false;
          }
        } else {
          if (this.isTsFile(f)) return false;
          if (!reactFeatures.includes('dva')) {
            if (f.startsWith('src/models') || f === 'src/app.js') return false;
          }
          if (!reactFeatures.includes('locale')) {
            if (f.startsWith('src/locales') || f.includes('umi-plugin-locale')) return false;
          }
        }
        return true;
      },
    });
  }
}

module.exports = Generator;