Skip to content

Commit

Permalink
chore: 更健全的自动创建
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Mar 15, 2022
1 parent e229aee commit 5639146
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 284 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"deps:fresh:patch": "npx taze patch -w",
"preview:host": "vite preview --host",
"preview:open": "vite preview --open",
"auto:create": "esno scripts/create.ts",
"auto:remove": "esno scripts/remove.ts",
"auto:create": "plop --plopfile scripts/create.js",
"auto:remove": "plop --plopfile scripts/remove.js",
"analysis:build": "windicss-analysis --html dist"
},
"dependencies": {
Expand All @@ -36,8 +36,9 @@
"@vueuse/core": "^7.7.1",
"axios": "^0.26.1",
"c8": "^7.11.0",
"esno": "^0.14.1",
"fs-extra": "^10.0.1",
"ityped": "^1.0.3",
"kolorist": "^1.5.1",
"markdown-it-anchor": "^8.4.1",
"markdown-it-prism": "^2.2.3",
"mockjs": "^1.1.0",
Expand Down
54 changes: 4 additions & 50 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions scripts/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const { lightRed } = require('kolorist')
const { pathExistsSync } = require('fs-extra')

const {
showExt,
showDir,
moduleTypes
} = require('./shared/base')

/**
* 模板生成
* @param {import('plop').NodePlopAPI} plop
*/
const create = function (plop) {
let exist = null
let modulePath = null

plop.setGenerator('controller', {
description: '生成器',
prompts: [
{
name: 'type',
type: 'list',
default: 'component',
message: '您希望生成哪种类型的模块?',
choices: moduleTypes
},
{
name: 'isMarkdown',
type: 'confirm',
message: '是否 markdown 类型?',
default: false,
// 如果是 page 类型需要询问是否为 markdown 类型
when({ type }) {
return type === 'page'
}
},
{
name: 'name',
type: 'input',
message({ type }) {
return `请输入 ${type} 的命名`
}
},
{
name: 'shouldReset',
type: 'confirm',
default: false,
message({ type }) {
return `目标 ${type} 已存在,是否重置?`
},
// 确认模块是否已存在,是则询问是否重置
when({ type, name, isMarkdown }) {
const dir = showDir(type)
const ext = showExt(type, isMarkdown)
modulePath = `src/${dir}/${name}.${ext}`
exist = pathExistsSync(modulePath)
if (exist) {
return true
}
}
}
],
actions(answer) {
const { type, shouldReset } = answer
if (exist && !shouldReset) {
console.log(lightRed(`${type} 创建失败`))
return []
}
return [
{
type: 'add',
force: true,
path: `../${modulePath}`,
templateFile: `./template/${type}.hbs`
}
]
}
})
}

module.exports = create
91 changes: 0 additions & 91 deletions scripts/create.ts

This file was deleted.

Empty file added scripts/remove.js
Empty file.
Loading

0 comments on commit 5639146

Please sign in to comment.