mirror of
https://github.com/youzan/vant.git
synced 2025-10-17 08:37:23 +00:00
chore(cli): extract create-vant-cli-app package
This commit is contained in:
4
packages/create-vant-cli-app/src/constant.ts
Normal file
4
packages/create-vant-cli-app/src/constant.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { join } from 'path';
|
||||
|
||||
export const CWD = process.cwd();
|
||||
export const GENERATOR_DIR = join(__dirname, '../generators');
|
87
packages/create-vant-cli-app/src/generator.ts
Normal file
87
packages/create-vant-cli-app/src/generator.ts
Normal 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!`
|
||||
);
|
||||
}
|
||||
}
|
27
packages/create-vant-cli-app/src/index.ts
Normal file
27
packages/create-vant-cli-app/src/index.ts
Normal 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();
|
Reference in New Issue
Block a user