Skip to content

Commit

Permalink
feat: zhi-cli allow multi files modify
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Apr 15, 2023
1 parent 3ee397c commit ce382ed
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
10 changes: 4 additions & 6 deletions apps/zhi-cli/src/init/commnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import LogFactory, { LogLevelEnum } from "zhi-log"
import fs from "fs-extra"
import path from "path"
import { downloadTemplate } from "./download"
import { modifyPackageJson } from "./modify"
import modifyFiles from "./modify"
import { prompt } from "enquirer"
import Select from "enquirer/lib/prompts/select"

Expand Down Expand Up @@ -86,18 +86,16 @@ export const initCommand = () => {
fs.removeSync(downloadPath)
}

// 系在仓库并替换参数
// 下载仓库并替换参数
await downloadTemplate(templateGitUrl, downloadPath, branch)
modifyPackageJson(downloadPath, { name, description, author })
modifyFiles(downloadPath, ["package.json", "README.md", "src/index.spec.ts"], { name, ...projectOptions })

// 删除git信息
fs.removeSync(path.join(downloadPath, ".git"))
// 删除模板
fs.removeSync(path.join(downloadPath, "package-template.json"))
logger.info(".git cleaned.")

logger.info("project created.")
logger.info("Now you can do `cd " + downloadPath + "`" + " and `pnpm install`")
logger.info("Now you can do `cd " + downloadPath + "`" + " and run `pnpm install`")
} catch (error) {
console.error(error)
}
Expand Down
41 changes: 25 additions & 16 deletions apps/zhi-cli/src/init/modify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,33 @@ import LogFactory, { LogLevelEnum } from "zhi-log"

const logger = LogFactory.customLogFactory(LogLevelEnum.LOG_LEVEL_INFO, "zhi-cli").getLogger("init:modify")

export const modifyPackageJson = function (downloadPath: string, options: any) {
const packagePath = path.join(downloadPath, "package.json")
logger.info("start modifying package.json ...")
if (fs.existsSync(packagePath)) {
const content = fs.readFileSync(packagePath).toString()
const modifyTemplate = function (downloadPath: string, file: string, options: any) {
const templatePath = path.join(downloadPath, file)
logger.info(`start modifying ${file} ...`)
if (fs.existsSync(templatePath)) {
const content = fs.readFileSync(templatePath).toString()
const template = handlebars.compile(content)

const param = {
name: options.name,
description: options.description,
author: options.author,
}

const result = template(param)
fs.writeFileSync(packagePath, result)
logger.info("modify package.json complete")
const result = template(options)
fs.writeFileSync(templatePath, result)
logger.info(`modify ${file} complete`)
} else {
logger.error("modify package.json fail")
throw new Error("no package.json")
logger.error(`modify ${file} fail`)
throw new Error(`no ${file}`)
}
}

/**
* 模板字符串替换
*
* @param downloadPath - 项目根路径
* @param fileList - 文件路径列表,注意:相对于项目根路径,例如:["package.json", "README.md"]
* @param options - 字符串Object
*/
const modifyFiles = (downloadPath: string, fileList: string[], options: any) => {
for (const file of fileList) {
modifyTemplate(downloadPath, file, options)
}
}

export default modifyFiles
2 changes: 2 additions & 0 deletions apps/zhi-core/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# zhi-core

core module for zhi

## Thanks

[esbuild-plugin-inline-image](https://github.com/natrim/esbuild-plugin-inline-image)
Expand Down

0 comments on commit ce382ed

Please sign in to comment.