-
Notifications
You must be signed in to change notification settings - Fork 708
/
Copy pathcomponent.mjs
95 lines (83 loc) · 2.57 KB
/
component.mjs
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
import { existsSync, promises as fsp } from 'node:fs'
import { resolve } from 'pathe'
import { defineCommand } from 'citty'
import { consola } from 'consola'
import { splitByCase, upperFirst, camelCase, kebabCase } from 'scule'
import { appendFile, sortFile } from '../../utils.mjs'
import templates from '../../templates.mjs'
export default defineCommand({
meta: {
name: 'component',
description: 'Make a new component.'
},
args: {
name: {
type: 'positional',
required: true,
description: 'Name of the component.'
},
primitive: {
type: 'boolean',
description: 'Create a primitive component.'
},
pro: {
type: 'boolean',
description: 'Create a pro component.'
},
prose: {
type: 'boolean',
description: 'Create a prose component (with --pro).'
},
content: {
type: 'boolean',
description: 'Create a content component (with --pro).'
},
template: {
type: 'string',
description: 'Only generate template.'
}
},
async setup({ args }) {
const name = args.name
if (!name) {
consola.error('`name` argument is missing!')
process.exit(1)
}
if (args.prose && !args.pro) {
consola.error('`--prose` flag can only be used with `--pro` flag!')
process.exit(1)
}
if (args.content && !args.pro) {
consola.error('`--content` flag can only be used with `--pro` flag!')
process.exit(1)
}
const path = resolve('.')
for (const template of Object.keys(templates)) {
if (args.template && template !== args.template) {
continue
}
const { filename, contents } = templates[template](args)
if (!contents) {
continue
}
const filePath = resolve(path, filename)
if (existsSync(filePath)) {
consola.error(`🚨 ${filePath} already exists!`)
continue
}
await fsp.writeFile(filePath, contents.trim() + '\n')
consola.success(`🪄 Generated ${filePath}!`)
}
if (args.template) {
return
}
const themePath = resolve(path, `src/theme/${args.prose ? 'prose/' : ''}${args.content ? 'content/' : ''}index.ts`)
await appendFile(themePath, `export { default as ${camelCase(name)} } from './${kebabCase(name)}'`)
await sortFile(themePath)
if (!args.prose) {
const typesPath = resolve(path, 'src/runtime/types/index.ts')
await appendFile(typesPath, `export * from '../components/${args.content ? 'content/' : ''}${splitByCase(name).map(p => upperFirst(p)).join('')}.vue'`)
await sortFile(typesPath)
}
}
})