From 4c9236c84ac732ab9b8838964dab3dd80d6f6ac4 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Sun, 29 Nov 2020 10:32:07 +0800 Subject: [PATCH] chore(create-vant-cli-app): improve template copy process --- .../{eslintignore.tpl => .eslintignore} | 0 .../templates/{gitignore.tpl => .gitignore} | 0 packages/create-vant-cli-app/package.json | 1 + packages/create-vant-cli-app/src/generator.ts | 33 ++++++++----------- packages/create-vant-cli-app/yarn.lock | 12 +++++++ packages/vant-cli/README.md | 2 +- 6 files changed, 28 insertions(+), 20 deletions(-) rename packages/create-vant-cli-app/generators/templates/{eslintignore.tpl => .eslintignore} (100%) rename packages/create-vant-cli-app/generators/templates/{gitignore.tpl => .gitignore} (100%) diff --git a/packages/create-vant-cli-app/generators/templates/eslintignore.tpl b/packages/create-vant-cli-app/generators/templates/.eslintignore similarity index 100% rename from packages/create-vant-cli-app/generators/templates/eslintignore.tpl rename to packages/create-vant-cli-app/generators/templates/.eslintignore diff --git a/packages/create-vant-cli-app/generators/templates/gitignore.tpl b/packages/create-vant-cli-app/generators/templates/.gitignore similarity index 100% rename from packages/create-vant-cli-app/generators/templates/gitignore.tpl rename to packages/create-vant-cli-app/generators/templates/.gitignore diff --git a/packages/create-vant-cli-app/package.json b/packages/create-vant-cli-app/package.json index c32b35098..c86ed8e81 100644 --- a/packages/create-vant-cli-app/package.json +++ b/packages/create-vant-cli-app/package.json @@ -33,6 +33,7 @@ "dependencies": { "chalk": "^3.0.0", "consola": "^2.11.3", + "fast-glob": "^3.2.4", "fs-extra": "^8.1.0", "inquirer": "^7.0.6", "yeoman-environment": "^2.8.0", diff --git a/packages/create-vant-cli-app/src/generator.ts b/packages/create-vant-cli-app/src/generator.ts index d4ebb58b1..5ee5c0bc0 100644 --- a/packages/create-vant-cli-app/src/generator.ts +++ b/packages/create-vant-cli-app/src/generator.ts @@ -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>(PROMPTS).then(inputs => { + return this.prompt>(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() { diff --git a/packages/create-vant-cli-app/yarn.lock b/packages/create-vant-cli-app/yarn.lock index 6b8ceb112..8a6f9d7e6 100644 --- a/packages/create-vant-cli-app/yarn.lock +++ b/packages/create-vant-cli-app/yarn.lock @@ -1178,6 +1178,18 @@ fast-glob@^3.1.1: micromatch "^4.0.2" picomatch "^2.2.1" +fast-glob@^3.2.4: + version "3.2.4" + resolved "https://registry.npm.taobao.org/fast-glob/download/fast-glob-3.2.4.tgz#d20aefbf99579383e7f3cc66529158c9b98554d3" + integrity sha1-0grvv5lXk4Pn88xmUpFYybmFVNM= + dependencies: + "@nodelib/fs.stat" "^2.0.2" + "@nodelib/fs.walk" "^1.2.3" + glob-parent "^5.1.0" + merge2 "^1.3.0" + micromatch "^4.0.2" + picomatch "^2.2.1" + fastq@^1.6.0: version "1.6.0" resolved "https://registry.npm.taobao.org/fastq/download/fastq-1.6.0.tgz#4ec8a38f4ac25f21492673adb7eae9cfef47d1c2" diff --git a/packages/vant-cli/README.md b/packages/vant-cli/README.md index e78b541f5..7532fcdb2 100644 --- a/packages/vant-cli/README.md +++ b/packages/vant-cli/README.md @@ -14,7 +14,7 @@ Vant Cli 是一个 Vue 组件库构建工具,通过 Vant Cli 可以快速搭 执行以下命令可以快速创建一个基于 Vant Cli 的项目: ```bash -npx create-vant-cli-app +yarn create vant-cli-app ``` ### 手动安装