-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.templaterc.ts
216 lines (184 loc) · 7.77 KB
/
.templaterc.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* eslint-disable import/no-extraneous-dependencies */
import path from 'path'
import { upperFirst, camelCase, kebabCase } from 'lodash'
import { SyntaxKind, type TemplateSchema } from '@dumlj/create-cli'
const configureCli = async (): Promise<TemplateSchema> => {
const alias = (name: string) => upperFirst(camelCase(name))
const nameClass = (name: string) => `${alias(name)}Cli`
const nameModule = (name: string) => `@dumlj/${kebabCase(`${name}-cli`)}`
const nameDirectory = (name: string) => `@cli/${kebabCase(`${name}-cli`)}`
const folder = path.join(__dirname, '@cli')
return {
template: path.join(folder, '__template__'),
output: folder,
name: 'Cli Template',
description: 'Create a cli which inherits seed-cli.',
egName: 'CustomCli',
nameTransform: (input: string) => {
const SUFFIX_NAME = 'Cli'
let length = 0
while (true) {
if (SUFFIX_NAME.length === length) {
break
}
const t = SUFFIX_NAME.substring(0, length + 1)
if (new RegExp(t, 'i').test(input)) {
length++
continue
}
break
}
const suffixSameStr = SUFFIX_NAME.substring(0, length)
const nameSameStr = input.substring(input.length - length)
if (suffixSameStr.toUpperCase() !== nameSameStr.toUpperCase()) {
return { shortName: input, name: nameClass(input), same: '', suffix: SUFFIX_NAME }
}
const shortName = input.substring(0, input.length - nameSameStr.length)
const name = alias(`${shortName}${SUFFIX_NAME}`)
const suffix = SUFFIX_NAME.substring(length)
return { shortName, name, same: suffixSameStr, suffix }
},
outputPathResolver: (kebabCaseName: string) => nameDirectory(kebabCaseName),
pkgTransform: () => {
return async ({ name, description, source }) => {
source.name = nameModule(name)
source.description = description
source.repository.directory = nameDirectory(name)
}
},
tsTransform: (file) => {
switch (file) {
case 'src/actions/entry.ts': {
return async ({ name, ast }) => {
const iOptions = ast.getInterfaceOrThrow('EntryOptions')
iOptions.rename(`${upperFirst(name)}Options`)
const iConst = ast.getVariableDeclarationOrThrow('entry')
iConst.rename(name)
return { output: `src/actions/${name}.ts` }
}
}
case 'src/commands/entry.ts': {
return async ({ name, ast }) => {
const declaration = ast.getImportDeclarationOrThrow('../actions/entry')
declaration.setModuleSpecifier(`../actions/${name}`)
const imports = declaration.getNamedImports()
const vEntry = imports.find((item) => item.getName() === 'entry')
const vEntryOptions = imports.find((item) => item.getName() === 'EntryOptions')
vEntry?.getNameNode().rename(name)
vEntryOptions?.getNameNode().rename(`${upperFirst(name)}Options`)
vEntry?.setName(name).removeAlias()
vEntryOptions?.setName(`${upperFirst(name)}Options`).removeAlias()
for (const callExpression of ast.getDescendantsOfKind(SyntaxKind.CallExpression)) {
callExpression
.getArguments()
.find((arg) => arg.getText() === `'entry'`)
?.replaceWithText(`'${name}'`)
}
return { output: `src/commands/${name}.ts` }
}
}
case 'src/index.ts': {
return async ({ name, ast }) => {
const declarations = ast.getExportDeclarations()
const declaration = declarations.find((d) => d.getModuleSpecifier()!.getLiteralText() === './commands/entry')!
declaration.setModuleSpecifier(`./commands/${name}`)
const entry = declaration.getNamedExports().find((item) => item.getAliasNode()?.getText() === 'entry')
entry?.getAliasNode()?.rename(name)
return
}
}
}
},
}
}
const configureFeature = async (): Promise<TemplateSchema> => {
const nameModule = (name: string) => `@dumlj/feature-${kebabCase(name)}`
const nameDirectory = (name: string) => `@feature/feature-${kebabCase(name)}`
const folder = path.join(__dirname, '@feature')
return {
template: path.join(folder, '__template__'),
output: folder,
name: 'Feature Template',
description: 'Create a common features.',
outputPathResolver: (kebabCaseName: string) => nameDirectory(kebabCaseName),
pkgTransform: () => {
return async ({ name, description, source }) => {
source.name = nameModule(name)
source.description = description
source.repository.directory = nameDirectory(name)
}
},
}
}
const configureWebpack = async (): Promise<TemplateSchema> => {
const alias = (name: string) => upperFirst(camelCase(name))
const nameClass = (name: string) => `${alias(name)}WebpackPlugin`
const nameOptions = (name: string) => `${alias(name)}WebpackPluginOptions`
const namePlugin = (name: string) => `${kebabCase(name)}-webpack-plugin`
const nameModule = (name: string) => `@dumlj/${kebabCase(name)}-webpack-plugin`
const nameDirectory = (name: string) => `@webpack-plugin/${kebabCase(name)}-webpack-plugin`
const folder = path.join(__dirname, '@webpack-plugin')
return {
template: path.join(folder, '__template__'),
output: folder,
name: 'Webpack Plugin Template',
description: 'Create a webpack plugin which inherits seed-webpack-plugin.',
egName: 'CustomWebpackPlugin',
nameTransform: (input: string) => {
const SUFFIX_NAME = 'WebpackPlugin'
let length = 0
while (true) {
if (SUFFIX_NAME.length === length) {
break
}
const t = SUFFIX_NAME.substring(0, length + 1)
if (new RegExp(t, 'i').test(input)) {
length++
continue
}
break
}
const suffixSameStr = SUFFIX_NAME.substring(0, length)
const nameSameStr = input.substring(input.length - length)
if (suffixSameStr.toUpperCase() !== nameSameStr.toUpperCase()) {
return { shortName: input, name: nameClass(input), same: '', suffix: SUFFIX_NAME }
}
const shortName = input.substring(0, input.length - nameSameStr.length)
const name = alias(`${shortName}${SUFFIX_NAME}`)
const suffix = SUFFIX_NAME.substring(length)
return { shortName, name, same: suffixSameStr, suffix }
},
outputPathResolver: (kebabCaseName: string) => nameDirectory(kebabCaseName),
pkgTransform: () => {
return async ({ name, description, source }) => {
source.name = nameModule(name)
source.description = description
source.repository.directory = nameDirectory(name)
}
},
tsTransform: (file) => {
switch (file) {
case 'src/WebpackPlugin.ts':
return async ({ name, ast }) => {
const nOptions = ast.getInterfaceOrThrow('WebpackPluginOptions')
nOptions.rename(nameOptions(name))
const nClass = ast.getClassOrThrow('WebpackPlugin')
nClass.rename(nameClass(name))
const nPluginName = nClass.getStaticProperty('PLUGIN_NAME')!
nPluginName.set({ initializer: JSON.stringify(namePlugin(name)) })
return { output: `src/${nameClass(name)}.ts` }
}
case 'src/index.ts':
return async ({ name, ast }) => {
const declarations = ast.getExportDeclarations()
const declaration = declarations.find((d) => d.getModuleSpecifier()!.getLiteralText() === './WebpackPlugin')!
declaration.setModuleSpecifier(`./${nameClass(name)}`)
return
}
}
},
}
}
export const configure = async () => {
return [await configureCli(), await configureFeature(), await configureWebpack()]
}