- 修复模版路径问题
  路径在cli下和直接运行脚本的目录不一致。修复后采用了绝对路径
This commit is contained in:
Titor 2021-02-10 18:38:36 +08:00
parent 9115db836f
commit 3414175d86
2 changed files with 17 additions and 9 deletions

View File

@ -5,14 +5,21 @@ import * as os from "os";
import ejs from "ejs"; import ejs from "ejs";
import chalk from "chalk"; import chalk from "chalk";
import { exec } from "child_process"; import { exec } from "child_process";
import { resolve } from "path";
// ------------------------------------------------ // ------------------------------------------------
// cli结果 // cli结果
let answers: { appName: string; pkgManager: number; author: string }; let answers: { appName: string; pkgManager: number; author: string };
// 脚本运行目录
const path: string = process.cwd() + "/"; // npm项目包 => 绝对路径
// 模版所在目录 const path: string = resolve(__dirname) + "/";
const tpl: string = `${path}/template/`; // 项目安装后 => 固定目录
const projectName = path.substring(0, path.lastIndexOf("/bin"));
// 项目安装后 => 模版目录(复制模版使用)
const tpl: string = `${projectName}/template/`;
// 脚本运行所在目录(模版去向使用)
const runPath: string = resolve(process.cwd()) + "/";
// ======================================================== // ========================================================
// 1、CLI 交互 // 1、CLI 交互
@ -84,7 +91,7 @@ async function writing() {
// Copy Static 文件 // Copy Static 文件
function copyStatic(files: string[][]) { function copyStatic(files: string[][]) {
files.forEach(file => { 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( console.log(
chalk.green("+") + chalk.green("+") +
` ${chalk.grey(answers.appName + "/" + file[1])} ${chalk.green( ` ${chalk.grey(answers.appName + "/" + file[1])} ${chalk.green(
@ -99,7 +106,7 @@ function copyStatic(files: string[][]) {
function copyTemplate(files: string[][]) { function copyTemplate(files: string[][]) {
files.forEach(async file => { files.forEach(async file => {
let compileContent = await ejs.renderFile(`${tpl}${file[0]}`, answers); 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( console.log(
chalk.green("+") + chalk.green("+") +
` ${chalk.grey(answers.appName + "/" + file[1])} ${chalk.green( ` ${chalk.grey(answers.appName + "/" + file[1])} ${chalk.green(

View File

@ -1,6 +1,6 @@
{ {
"name": "@h2ostudio/koa-program", "name": "koa-program",
"version": "1.0.0", "version": "1.1.0",
"bin": { "bin": {
"koa": "bin/index.js" "koa": "bin/index.js"
}, },
@ -35,7 +35,8 @@
"cli" "cli"
], ],
"author": { "author": {
"name": "titor" "name": "titor",
"email": "foolsecret@163.com"
}, },
"publishConfig": { "publishConfig": {
"registry": "https://npm.pkg.github.com/h2ostudio" "registry": "https://npm.pkg.github.com/h2ostudio"