-
Notifications
You must be signed in to change notification settings - Fork 1
/
plopfile.ts
80 lines (70 loc) · 2.33 KB
/
plopfile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { NodePlopAPI } from 'plop'
import { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
const resolveRoot = (...paths: string[]) => resolve(__dirname, '.', ...paths)
const resolvePackages = (...paths: string[]) => resolveRoot('packages', ...paths)
const resolveTemplates = (...paths: string[]) => resolveRoot('templates', ...paths)
const notEmpty = (name: string) => {
return (v: string) => {
if (!v || v.trim() === '') {
return `${name} is required`
} else {
return true
}
}
}
const PLUGIN_PREFIX = 'axios-ext-'
export default function (plop: NodePlopAPI) {
plop.setGenerator('plugin', {
description: 'Generate plugin template of AxiosExt.',
prompts: [
{
type: 'input',
name: 'name',
message: 'Plugin name',
validate: notEmpty('name')
},
{
type: 'input',
name: 'description',
message: 'Plugin description'
},
{
type: 'input',
name: 'version',
message: 'Plugin version',
default: '0.0.0',
validate: notEmpty('version')
}
],
actions: (data) => {
const { name = '', description = '', version = '0.0.0' } = data ?? {}
const pkgName = plop.getHelper('kebabCase')(name).trim().toLowerCase()
const dirName = PLUGIN_PREFIX + pkgName
const pluginName = plop.getHelper('pascalCase')(pkgName)
const actionData = {
pkgName,
pluginName,
description: description.trim(),
version: version.trim()
}
const pkgPath = resolvePackages(dirName, 'package.json')
const readMePath = resolvePackages(dirName, 'README.md')
const indexPath = resolvePackages(dirName, 'lib', 'index.ts')
const tsconfigPath = resolvePackages(dirName, 'tsconfig.json')
const createAction = (path = '', templateFile = '') => ({
type: 'add',
path,
templateFile,
data: actionData
})
return [
createAction(pkgPath, resolveTemplates('plugins/package.json.hbs')),
createAction(readMePath, resolveTemplates('plugins/README.md.hbs')),
createAction(indexPath, resolveTemplates('plugins/index.ts.hbs')),
createAction(tsconfigPath, resolveTemplates('plugins/tsconfig.json.hbs'))
]
}
})
}