-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathindex.ts
188 lines (165 loc) · 4.82 KB
/
index.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
import superEjs from 'super-ejs'
import changeCase from '@juln/change-case'
import path from 'path'
import fsExtra from 'fs-extra'
const logInfo = (...args: string[]): void =>
console.info('\x1B[33m', ...args, '\x1B[0m')
const logError = (...args: string[]): void =>
console.error('\x1B[31m', ...args, '\x1B[0m')
const updatedFiles: string[] = []
const compName: string = fetchCompName()
const displayName = `${changeCase(compName, 'upper-camel-case')}`
const __dirname: string = path.resolve()
const outputDir: string = path.resolve(
__dirname,
'../fighting-design',
compName
)
const mainFilePath: string = path.join(
'packages/fighting-design',
compName,
`${compName}.vue`
)
const detectPublic = async (): Promise<void> => {
/** 如果已经存在 */
if (fsExtra.existsSync(outputDir)) {
logError(`组件 ${compName}` + '\n' + `已存在${mainFilePath}`)
return
}
try {
await generate()
logInfo(
'本次创建/修改的文件有:' +
'\n' +
'\n' +
`${updatedFiles.join('\n')}` +
'\n'
)
} catch (error: any) {
logError(`不好意思,组件[${compName}]创建失败了` + '\n' + `error: ${error}`)
process.exit(0)
}
logInfo(`F${displayName} 组件创建完成 🎉🎉🎉` + '\n')
}
detectPublic()
/** 检测组件名是否规范 */
function fetchCompName (): string {
const input: string = process.argv[2]
if (input === void 0) {
logError(
'\n' +
'命令使用方法为: pnpm new <component-name>' +
'\n' +
'例如: pnpm new user-avatar' +
'\n'
)
process.exit(0)
}
if (
input.match(/^[a-zA-Z]+?[-|_|a-zA-Z0-9]*?$/) &&
!input.endsWith('-') &&
!input.endsWith('_')
) {
const compName: string = changeCase(input, 'param-case')
return compName
}
logError('组件名不规范')
process.exit(0)
}
async function generate (): Promise<[void, void, void, void, void]> {
updatedFiles.push(
`packages/fighting-design/${compName}/**`,
'packages/fighting-design/index.ts',
`packages/fighting-theme/src/${compName}.scss`,
'packages/fighting-theme/index.scss',
`packages/fighting-design/${compName}/__test__/${compName}.spec.ts`
)
const catchError = async (
callback: Function,
info: string
): Promise<void> => {
try {
await callback()
} catch (error: any) {
logError(info + '\n' + `error: ${error}`)
}
}
return Promise.all([
catchError(generateComponentDir, '🚧 组件源文件创建失败'),
catchError(updateComponentEntry, '🚧 组件入口修改失败'),
catchError(incrementStyle, '🚧 样式文件创建失败'),
catchError(updateStyleEntry, '🚧 样式入口修改失败'),
catchError(incrementTest, '🚧 测试文件创建失败')
] as const)
}
async function generateComponentDir (): Promise<void> {
const tplDir: string = path.resolve(__dirname, './template/component')
/** 编译文件内容 */
await superEjsGerenateDir(outputDir, tplDir)
}
/** 修改组件入口文件 */
async function updateComponentEntry (): Promise<void> {
const entryFilePath: string = path.resolve(
__dirname,
'../fighting-design/components.ts'
)
let content: string = (await fsExtra.readFile(entryFilePath)).toString()
content =
content.slice(0, -1) +
'\n' +
'\n' +
`export { F${displayName} } from './${compName}'` +
'\n' +
`export * from './${compName}'` +
'\n'
await fsExtra.writeFile(entryFilePath, content)
}
/** 创建样式文件 */
async function incrementStyle (): Promise<void> {
const outputDir: string = path.resolve(__dirname, '../fighting-theme/src')
const tplDir: string = path.resolve(__dirname, './template/style')
/** 编译文件内容 */
await superEjsGerenateDir(outputDir, tplDir)
}
/** 添加样式入口 */
async function updateStyleEntry (): Promise<void> {
const entryFilePath: string = path.resolve(
__dirname,
'../fighting-theme/index.scss'
)
let content: string = (await fsExtra.readFile(entryFilePath)).toString()
content = content.slice(0, -1) + '\n' + `@use './src/${compName}';` + '\n'
await fsExtra.writeFile(entryFilePath, content)
}
/** 添加测试文件 */
async function incrementTest (): Promise<void> {
const outputDir: string = path.resolve(
__dirname,
`../fighting-design/${compName}/__test__`
)
const tplDir: string = path.resolve(__dirname, './template/test')
await superEjsGerenateDir(outputDir, tplDir)
}
async function superEjsGerenateDir (
outputDir: string,
tplDir: string
): Promise<void> {
return await superEjs.gerenateDir(
outputDir,
tplDir,
{
name: compName,
displayName,
changeCase
},
{},
{
parseFilename: (original: string): string => {
return original.replace(
/(.*?)__name__([a-zA-Z0-9|\.]*?$)/,
`$1${compName}$2`
)
}
}
)
}