chore(create-vant-cli-app): improve template copy process

This commit is contained in:
chenjiahan
2020-11-29 10:32:07 +08:00
parent a49530b888
commit 4c9236c84a
6 changed files with 28 additions and 20 deletions

View File

@@ -1,3 +1,4 @@
import glob from 'fast-glob';
import chalk from 'chalk';
import consola from 'consola';
import { join } from 'path';
@@ -5,7 +6,7 @@ import { CWD, GENERATOR_DIR } from './constant';
import Yeoman from 'yeoman-environment';
import Generator from 'yeoman-generator';
const TEMPLATES = join(GENERATOR_DIR, 'templates');
const TEMPLATES_PATH = join(GENERATOR_DIR, 'templates');
const PROMPTS = [
{
name: 'preprocessor',
@@ -34,7 +35,7 @@ export class VanGenerator extends Generator {
}
async prompting() {
return this.prompt<Record<string, string>>(PROMPTS).then(inputs => {
return this.prompt<Record<string, string>>(PROMPTS).then((inputs) => {
const preprocessor = inputs.preprocessor.toLowerCase();
const cssLang = preprocessor === 'sass' ? 'scss' : preprocessor;
@@ -46,25 +47,19 @@ export class VanGenerator extends Generator {
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 templateFiles = glob.sync(join(TEMPLATES_PATH, '**', '*'), {
dot: true,
});
const destinationRoot = this.destinationRoot();
const copyTpl = (from: string, to?: string) => {
this.fs.copyTpl(
join(TEMPLATES, from),
this.destinationPath(to || from),
this.inputs
);
};
console.log(templateFiles);
copyTpl('package.json.tpl', 'package.json');
copyTpl('vant.config.js');
copyTpl('src/**/*', 'src');
copyTpl('docs/**/*', 'docs');
copy('babel.config.js');
copy('gitignore.tpl', '.gitignore');
copy('eslintignore.tpl', '.eslintignore');
templateFiles.forEach((filePath) => {
const outputPath = filePath
.replace('.tpl', '')
.replace(TEMPLATES_PATH, destinationRoot);
this.fs.copyTpl(filePath, outputPath, this.inputs);
});
}
install() {