chore(cli): extract create-vant-cli-app package

This commit is contained in:
陈嘉涵
2020-01-16 18:02:34 +08:00
parent 62a1b39b2b
commit 5bb9a31e28
32 changed files with 2492 additions and 697 deletions

View File

@@ -0,0 +1,4 @@
import { join } from 'path';
export const CWD = process.cwd();
export const GENERATOR_DIR = join(__dirname, '../generators');

View File

@@ -0,0 +1,87 @@
import chalk from 'chalk';
import consola from 'consola';
import { join } from 'path';
import { CWD, GENERATOR_DIR } from './constant';
import Generator from 'yeoman-generator';
const TEMPLATES = join(GENERATOR_DIR, 'templates');
const PROMPTS = [
{
name: 'preprocessor',
message: 'Select css preprocessor',
type: 'list',
choices: ['Less', 'Sass']
}
];
export class VanGenerator extends Generator {
inputs = {
name: '',
preprocessor: ''
};
constructor(name: string) {
super([], {
env: {
cwd: join(CWD, name)
},
resolved: GENERATOR_DIR
});
this.inputs.name = name;
}
async prompting() {
return this.prompt(PROMPTS).then(inputs => {
this.inputs.preprocessor = inputs.preprocessor as string;
});
}
writing() {
consola.info(`Creating project in ${join(CWD, this.inputs.name)}\n`);
const copy = (from: string, to?: string) => {
this.fs.copy(join(TEMPLATES, from), this.destinationPath(to || from));
};
const copyTpl = (name: string) => {
this.fs.copyTpl(
join(TEMPLATES, name),
this.destinationPath(name),
this.inputs
);
};
copy('.gitignore');
copy('.eslintignore');
copy('babel.config.js');
copy('src/**/*', 'src');
copy('docs/**/*', 'docs');
copyTpl('package.json');
copyTpl('vant.config.js');
}
install() {
console.log();
consola.info('Install dependencies...\n');
process.chdir(this.inputs.name);
this.installDependencies({
npm: false,
bower: false,
yarn: true,
skipMessage: true
});
}
end() {
const { name } = this.inputs;
console.log();
consola.success(`Successfully created ${chalk.yellow(name)}.`);
consola.success(
`Run ${chalk.yellow(`cd ${name} && yarn dev`)} to start development!`
);
}
}

View File

@@ -0,0 +1,27 @@
import inquirer from 'inquirer';
import { mkdirSync, existsSync } from 'fs-extra';
import { VanGenerator } from './generator';
const PROMPTS = [
{
type: 'input',
name: 'name',
message: 'Your package name'
}
];
export default async function run() {
const { name } = await inquirer.prompt(PROMPTS);
if (!existsSync(name)) {
mkdirSync(name);
}
const generator = new VanGenerator(name);
return new Promise(resolve => {
generator.run(resolve);
});
}
run();