为脚手架创建脚本demo

创建脚本

查询部分

    import type { QuestionCollection } from 'inquirer';
    import inquirer from 'inquirer';

    const query: QuestionCollection[] = [
    {
        name: 'type',
        message: '请选择创建类型',
        type: 'list',
        choices: ['模块', '组件']
    },
    {
        name: 'name',
        message: answer => `请输入${answer.type}`
    }
    ];
    inquirer.prompt(query);

写入新文件部分

    import { resolve } from 'path';
    import {
        mkdir,
        mkdirSync,
        pathExistsSync,
        readFile,
        writeFile,
    } from 'fs-extra';
    const getTemplate = (name: string): string => {
    return `
这是一个${name}
`
}; // __dirname /Users/bytedance/Desktop/0726/byteair_console_fe/scripts/init const createFile = async () => { const { type, name } = await inquirer.prompt(query); const dirname = resolve(__dirname, 'index'); mkdir(dirname); writeFile(resolve(dirname, `${name}.ts`), getTemplate(name)); }; createFile();

插入部分

    let route = await readFile(resolve(srcPath, 'routes.ts'), {
      encoding: 'utf8',
    });
    route = route.replace(
      '// import route tag',
      `{
    path: '/${kebabCase(this.name)}',
    component: lazy(() => import('@/pages/${kebabCase(this.name)}/Index')),
  },
  // import route tag`,
    );
    await writeFile(resolve(srcPath, 'routes.ts'), route);

你可能感兴趣的:(为脚手架创建脚本demo)