From 3414175d86f14cd629cfedb01b0161380598be72 Mon Sep 17 00:00:00 2001 From: Titor Date: Wed, 10 Feb 2021 18:38:36 +0800 Subject: [PATCH] 1.1.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复模版路径问题 路径在cli下和直接运行脚本的目录不一致。修复后采用了绝对路径 --- bin/index.ts | 19 +++++++++++++------ package.json | 7 ++++--- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/bin/index.ts b/bin/index.ts index d383da3..36e2ef3 100644 --- a/bin/index.ts +++ b/bin/index.ts @@ -5,14 +5,21 @@ import * as os from "os"; import ejs from "ejs"; import chalk from "chalk"; import { exec } from "child_process"; +import { resolve } from "path"; // ------------------------------------------------ // cli结果: let answers: { appName: string; pkgManager: number; author: string }; -// 脚本运行目录 -const path: string = process.cwd() + "/"; -// 模版所在目录 -const tpl: string = `${path}/template/`; + +// npm项目包 => 绝对路径 +const path: string = resolve(__dirname) + "/"; +// 项目安装后 => 固定目录 +const projectName = path.substring(0, path.lastIndexOf("/bin")); + +// 项目安装后 => 模版目录(复制模版使用) +const tpl: string = `${projectName}/template/`; +// 脚本运行所在目录(模版去向使用) +const runPath: string = resolve(process.cwd()) + "/"; // ======================================================== // 1、CLI 交互 @@ -84,7 +91,7 @@ async function writing() { // Copy Static 文件 function copyStatic(files: string[][]) { files.forEach(file => { - fs.copy(`${tpl}${file[0]}`, `${path}${answers.appName}/${file[1]}`); + fs.copy(`${tpl}${file[0]}`, `${runPath}${answers.appName}/${file[1]}`); console.log( chalk.green("+") + ` ${chalk.grey(answers.appName + "/" + file[1])} ${chalk.green( @@ -99,7 +106,7 @@ function copyStatic(files: string[][]) { function copyTemplate(files: string[][]) { files.forEach(async file => { let compileContent = await ejs.renderFile(`${tpl}${file[0]}`, answers); - fs.write(`${path}${answers.appName}/${file[1]}`, compileContent); + fs.write(`${runPath}${answers.appName}/${file[1]}`, compileContent); console.log( chalk.green("+") + ` ${chalk.grey(answers.appName + "/" + file[1])} ${chalk.green( diff --git a/package.json b/package.json index 1e266ca..aee5b89 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "@h2ostudio/koa-program", - "version": "1.0.0", + "name": "koa-program", + "version": "1.1.0", "bin": { "koa": "bin/index.js" }, @@ -35,7 +35,8 @@ "cli" ], "author": { - "name": "titor" + "name": "titor", + "email": "foolsecret@163.com" }, "publishConfig": { "registry": "https://npm.pkg.github.com/h2ostudio"