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 5639146 commit b446e9f
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 22 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "tov-template",
"version": "1.4.0",
"type": "module",
"description": "vite + vue3 + ts 开箱即用现代开发模板",
"scripts": {
"dev": "vite",
Expand Down Expand Up @@ -36,13 +37,14 @@
"@vueuse/core": "^7.7.1",
"axios": "^0.26.1",
"c8": "^7.11.0",
"fast-glob": "^3.2.11",
"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",
"nprogress": "^0.2.0",
"ora": "^6.1.0",
"pinia": "^2.0.12",
"plop": "^3.0.5",
"prism-theme-vars": "^0.2.2",
Expand Down
6 changes: 4 additions & 2 deletions pnpm-lock.yaml

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

19 changes: 9 additions & 10 deletions scripts/create.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
const { lightRed } = require('kolorist')
const { pathExistsSync } = require('fs-extra')
import fse from 'fs-extra'

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

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

plop.setGenerator('controller', {
description: '生成器',
description: '自动创建',
prompts: [
{
name: 'type',
Expand Down Expand Up @@ -54,7 +53,7 @@ const create = function (plop) {
const dir = showDir(type)
const ext = showExt(type, isMarkdown)
modulePath = `src/${dir}/${name}.${ext}`
exist = pathExistsSync(modulePath)
exist = fse.pathExistsSync(modulePath)
if (exist) {
return true
}
Expand All @@ -64,7 +63,7 @@ const create = function (plop) {
actions(answer) {
const { type, shouldReset } = answer
if (exist && !shouldReset) {
console.log(lightRed(`${type} 创建失败`))
throw new Error(`${type} 创建失败`)
return []
}
return [
Expand All @@ -79,4 +78,4 @@ const create = function (plop) {
})
}

module.exports = create
export default create
79 changes: 79 additions & 0 deletions scripts/remove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import ora from 'ora'
import fg from 'fast-glob'
import fse from 'fs-extra'
import { basename } from 'path'
import { showDir, moduleTypes } from './shared/base.js'

/**
* 自动删除
* @param {import('plop').NodePlopAPI} plop
*/
function remove(plop) {
const spinner = ora()

plop.setActionType('remove', (answers, config, plop) => {
const { name, type, shouldRemove } = answers
const dir = showDir(type)
const target = `./src/${dir}/${name}`
if (shouldRemove) {
return fse.removeSync(target)
}
throw new Error(`删除 ${target} 失败`)
})

plop.setGenerator('controller', {
description: '自动删除',
prompts: [
{
name: 'type',
type: 'list',
message: '请选择您要删除的类型',
async choices() {
const dirs = await fg('./src/**/*', {
deep: 1,
onlyDirectories: true
})
const types = moduleTypes.filter(type => {
const dir = showDir(type)
return dirs.includes(`./src/${dir}`)
})
spinner.stop()
return types
}
},
{
name: 'name',
type: 'list',
message({ type }) {
return `请选择您要删除的 ${type} 模块`
},
async choices({ type }) {
spinner.start(`读取现有 ${type} 模块中~~`)
const dir = showDir(type)
let modules = await fg(`./src/${dir}/*.*`, {
deep: 1,
onlyFiles: true
})
modules = modules.map(module => {
return basename(module)
})
spinner.stop()
return modules
}
},
{
name: 'shouldRemove',
type: 'confirm',
default: false,
message: '再次确认是否删除'
}
],
actions: [
{
type: 'remove'
}
]
})
}

export default remove
12 changes: 3 additions & 9 deletions scripts/shared/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @param {boolean} isMarkdown 是否是 markdown,默认为 false
* @returns {string} 扩展名
*/
const showExt = (type, isMarkdown = false) => {
export const showExt = (type, isMarkdown = false) => {
const isTs =
type === 'api' || type === 'store' || type === 'module'
const ext = isMarkdown ? 'md' : isTs ? 'ts' : 'vue'
Expand All @@ -14,7 +14,7 @@ const showExt = (type, isMarkdown = false) => {
/**
* 模块类型
*/
const moduleTypes = [
export const moduleTypes = [
'api',
'page',
'store',
Expand All @@ -28,15 +28,9 @@ const moduleTypes = [
* 获取目录
* @param {string} type 类型
*/
const showDir = type => {
export const showDir = type => {
if (type === 'api') {
return 'api'
}
return `${type}s`
}

module.exports = {
showExt,
showDir,
moduleTypes
}

0 comments on commit b446e9f

Please sign in to comment.