index.js 4.2 KB
Newer Older
super-lin0's avatar
upgrade  
super-lin0 committed
1 2 3 4 5 6 7 8 9 10 11
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const glob = require('glob');
const exec = require('execa');
const BasicGenerator = require('../../BasicGenerator');
const filterPkg = require('./filterPkg');
const prettier = require('prettier');
const sylvanas = require('sylvanas');
const sortPackage = require('sort-package-json');
// const { getFastGithub } = require('umi-utils');
super-lin0's avatar
super-lin0 committed
12 13

function log(...args) {
super-lin0's avatar
upgrade  
super-lin0 committed
14
  console.log(`${chalk.gray('>')}`, ...args);
super-lin0's avatar
super-lin0 committed
15 16 17 18 19 20 21 22 23 24 25 26
}

function globList(patternList, options) {
  let fileList = [];
  patternList.forEach(pattern => {
    fileList = [...fileList, ...glob.sync(pattern, options)];
  });

  return fileList;
}

const getGithubUrl = async () => {
super-lin0's avatar
docs  
super-lin0 committed
27
  // const fastGithub = await getFastGithub();
super-lin0's avatar
upgrade  
super-lin0 committed
28 29 30
  // if (fastGithub === 'github.com.cnpmjs.org') {
  //   return 'https://github.com.cnpmjs.org/ant-design/ant-design-pro';
  // }
super-lin0's avatar
docs  
super-lin0 committed
31
  return 'http://platform.kuopu.net:9999/gitlab/nemean-group/nemean-pro';
super-lin0's avatar
super-lin0 committed
32 33
};

super-lin0's avatar
upgrade  
super-lin0 committed
34
const PRO_PATH = process.env.INIT_CWD || process.env.npm_rootpath || process.cwd();
super-lin0's avatar
super-lin0 committed
35 36 37 38 39

class AntDesignProGenerator extends BasicGenerator {
  prompting() {
    if (this.opts.args.language) {
      this.prompts = {
super-lin0's avatar
upgrade  
super-lin0 committed
40
        language: this.opts.args.language,
super-lin0's avatar
super-lin0 committed
41 42 43 44
      };
    } else {
      const prompts = [
        {
super-lin0's avatar
upgrade  
super-lin0 committed
45 46 47 48 49
          name: 'language',
          type: 'list',
          message: 'Which language do you want to use?',
          choices: ['TypeScript', 'JavaScript'],
        },
super-lin0's avatar
super-lin0 committed
50 51 52 53 54 55 56 57 58
      ];
      return this.prompt(prompts).then(props => {
        this.prompts = props;
      });
    }
  }

  async writing() {
    const { language } = this.prompts;
super-lin0's avatar
upgrade  
super-lin0 committed
59
    const isTypeScript = language === 'TypeScript';
super-lin0's avatar
super-lin0 committed
60 61 62 63 64 65

    const projectName = this.opts.name || this.opts.env.cwd;

    const projectPath = path.resolve(projectName);

    const envOptions = {
super-lin0's avatar
upgrade  
super-lin0 committed
66
      cwd: projectPath,
super-lin0's avatar
super-lin0 committed
67 68 69 70 71 72 73
    };

    const githubUrl = await getGithubUrl();
    const gitArgs = [`clone`, githubUrl, `--depth=1`];

    // Set branch if provided
    if (this.opts.args.branch) {
super-lin0's avatar
upgrade  
super-lin0 committed
74
      gitArgs.push('--branch', this.opts.args.branch);
super-lin0's avatar
super-lin0 committed
75 76 77 78 79
    }

    gitArgs.push(projectName);

    // Clone remote branch
super-lin0's avatar
upgrade  
super-lin0 committed
80
    log(`git ${gitArgs.join(' ')}`);
super-lin0's avatar
super-lin0 committed
81 82
    await exec(`git`, gitArgs);

super-lin0's avatar
upgrade  
super-lin0 committed
83
    const packageJsonPath = path.resolve(projectPath, 'package.json');
super-lin0's avatar
super-lin0 committed
84 85 86
    const pkg = require(packageJsonPath);
    // Handle js version
    if (!isTypeScript) {
super-lin0's avatar
upgrade  
super-lin0 committed
87 88
      log('[Sylvanas] Prepare js environment...');
      const tsFiles = globList(['**/*.tsx', '**/*.ts'], {
super-lin0's avatar
super-lin0 committed
89
        ...envOptions,
super-lin0's avatar
upgrade  
super-lin0 committed
90
        ignore: ['**/*.d.ts'],
super-lin0's avatar
super-lin0 committed
91 92 93 94
      });

      sylvanas(tsFiles, {
        ...envOptions,
super-lin0's avatar
upgrade  
super-lin0 committed
95
        action: 'overwrite',
super-lin0's avatar
super-lin0 committed
96 97
      });

super-lin0's avatar
upgrade  
super-lin0 committed
98 99
      log('[JS] Clean up...');
      const removeTsFiles = globList(['tsconfig.json', '**/*.d.ts'], envOptions);
super-lin0's avatar
super-lin0 committed
100 101 102 103 104 105 106
      removeTsFiles.forEach(filePath => {
        const targetPath = path.resolve(projectPath, filePath);
        fs.removeSync(targetPath);
      });
    }

    // copy readme
super-lin0's avatar
upgrade  
super-lin0 committed
107 108
    const babelConfig = path.resolve(__dirname, 'README.md');
    fs.copySync(babelConfig, path.resolve(projectPath, 'README.md'));
super-lin0's avatar
super-lin0 committed
109 110

    // gen package.json
super-lin0's avatar
upgrade  
super-lin0 committed
111 112
    if (pkg['create-nemean']) {
      const { ignoreScript = [], ignoreDependencies = [] } = pkg['create-nemean'];
super-lin0's avatar
super-lin0 committed
113 114 115
      // filter scripts and devDependencies
      const projectPkg = {
        ...pkg,
super-lin0's avatar
upgrade  
super-lin0 committed
116
        version: '1.0.0',
super-lin0's avatar
super-lin0 committed
117
        scripts: filterPkg(pkg.scripts, ignoreScript),
super-lin0's avatar
upgrade  
super-lin0 committed
118
        devDependencies: filterPkg(pkg.devDependencies, ignoreDependencies),
super-lin0's avatar
super-lin0 committed
119 120
      };
      // remove create-nemean config
super-lin0's avatar
upgrade  
super-lin0 committed
121
      delete projectPkg['create-nemean'];
super-lin0's avatar
super-lin0 committed
122
      fs.writeFileSync(
super-lin0's avatar
upgrade  
super-lin0 committed
123
        path.resolve(projectPath, 'package.json'),
super-lin0's avatar
super-lin0 committed
124 125 126
        // 删除一个包之后 json会多了一些空行。sortPackage 可以删除掉并且排序
        // prettier 会容忍一个空行
        prettier.format(JSON.stringify(sortPackage(projectPkg)), {
super-lin0's avatar
upgrade  
super-lin0 committed
127 128
          parser: 'json',
        }),
super-lin0's avatar
super-lin0 committed
129 130 131 132
      );
    }

    // Clean up useless files
super-lin0's avatar
upgrade  
super-lin0 committed
133 134 135
    if (pkg['create-nemean'] && pkg['create-nemean'].ignore) {
      log('Clean up...');
      const ignoreFiles = pkg['create-nemean'].ignore;
super-lin0's avatar
super-lin0 committed
136 137 138 139 140 141 142 143 144 145 146
      const fileList = globList(ignoreFiles, envOptions);

      fileList.forEach(filePath => {
        const targetPath = path.resolve(projectPath, filePath);
        fs.removeSync(targetPath);
      });
    }
  }
}

module.exports = AntDesignProGenerator;