Skip to content

Commit

Permalink
feat: Download project template from zhi-ts-template
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Mar 30, 2023
1 parent 89afd2e commit b4f37fb
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 133 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"git-clone": "^0.2.0",
"handlebars": "^4.7.7",
"loglevel": "^1.8.1",
"loglevel-plugin-prefix": "^0.8.4",
"ora": "^6.1.2"
"loglevel-plugin-prefix": "^0.8.4"
}
}
2 changes: 1 addition & 1 deletion packages/zhi-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zhi-cli",
"version": "1.2.1",
"version": "1.2.2",
"type": "module",
"description": "a tool for generating zhi framework related projects",
"repository": "terwer/zhi",
Expand Down
14 changes: 8 additions & 6 deletions packages/zhi-cli/src/lib/init/commnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import { printVerboseHook } from "../utils"
import LogFactory, { LogLevelEnum } from "zhi-log"
import fs from "fs-extra"
import path from "path"
// import { downloadTemplate } from "./download"
// import { modifyPackageJson } from "./modify"
import { downloadTemplate } from "./download"
import { modifyPackageJson } from "./modify"
import { prompt } from "enquirer"
import Select from "enquirer/lib/prompts/select"

Expand Down Expand Up @@ -58,16 +58,18 @@ export const initCommand = () => {
logger.info("start init zhi project:", name)
logger.info("using template:", branch)

const description = "please input project description"
const author = "please input author"
const projectOptions = await prompt([
{
type: "input",
name: "description",
message: "please input project description",
message: description,
},
{
type: "input",
name: "author",
message: "please input author",
message: author,
},
])

Expand All @@ -82,8 +84,8 @@ export const initCommand = () => {
}

// 系在仓库并替换参数
// await downloadTemplate(templateGitUrl, downloadPath, branch)
// modifyPackageJson(downloadPath, { name, ...initOptions })
await downloadTemplate(templateGitUrl, downloadPath, branch)
modifyPackageJson(downloadPath, { name, description, author })

// 删除git信息
fs.removeSync(path.join(downloadPath, ".git"))
Expand Down
13 changes: 4 additions & 9 deletions packages/zhi-cli/src/lib/init/download.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
import gitclone from "git-clone/promise"
import ora from "ora"
import LogFactory, { LogLevelEnum } from "zhi-log"

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

export const downloadTemplate = (templateGitUrl: string, downloadPath: string, branch: string) => {
const loading = ora("download template")
logger.info("download template")
return new Promise((resolve, reject) => {
logger.info("prepare to checkout templateGitUrl=>", templateGitUrl)
logger.info("prepare to checkout downloadPath=>", downloadPath)
logger.info("prepare to checkout branch=>", branch)
loading.start("start download template")
logger.info("start download template ...")

gitclone(templateGitUrl, downloadPath, {
checkout: branch,
shallow: false,
})
.then((r: any) => {
loading.succeed("download success")
loading.stop()

logger.info("download success")
resolve("download success")
})
.catch((error: any) => {
loading.stop()
loading.fail("download fail")

logger.error("download fail")
reject(error)
})
})
Expand Down
12 changes: 5 additions & 7 deletions packages/zhi-cli/src/lib/init/modify.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fs from "fs-extra"
import path from "path"
import handlebars from "handlebars"
import ora from "ora"
import LogFactory, { LogLevelEnum } from "zhi-log"

const log = ora("modify")
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")
log.start("start modifying package.json")
logger.info("start modifying package.json ...")
if (fs.existsSync(packagePath)) {
const content = fs.readFileSync(packagePath).toString()
const template = handlebars.compile(content)
Expand All @@ -20,11 +20,9 @@ export const modifyPackageJson = function (downloadPath: string, options: any) {

const result = template(param)
fs.writeFileSync(packagePath, result)
log.stop()
log.succeed("modify package.json complate")
logger.info("modify package.json complete")
} else {
log.stop()
log.fail("modify package.json fail")
logger.error("modify package.json fail")
throw new Error("no package.json")
}
}
2 changes: 1 addition & 1 deletion packages/zhi-cli/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default defineConfig({
},
rollupOptions: {
// External packages that should not be bundled into your library.
external: ["fs", "path", "process", "events", "assert", "readline"],
external: ["fs", "path", "process", "events", "assert", "readline", "child_process"],
output: {
banner: "#!/usr/bin/env node",
},
Expand Down
Loading

0 comments on commit b4f37fb

Please sign in to comment.