-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.ts
54 lines (41 loc) · 1.47 KB
/
component.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
import {Args, Command} from '@oclif/core'
import {camelCase, pascalCase} from 'change-case'
import fs from 'node:fs'
import Template from '../template.js'
import componentTemplate from '../templates/component.js'
import {projectPath} from '../utils.js'
export default class Component extends Command {
static override args = {
name: Args.string({description: 'name of component', required: true}),
}
static override description = 'Adds a new component to the project'
static override examples = ['<%= config.bin %> <%= command.id %> movement']
static override flags = {}
public async run(): Promise<void> {
const {args} = await this.parse(Component)
this.checkFolderStructure()
this.writeNewComponent(args.name)
}
private checkFolderStructure(): void {
if (!fs.existsSync(projectPath('src'))) {
this.error('The current directory does not contain a src folder.')
}
if (!fs.existsSync(projectPath('src', 'components'))) {
fs.mkdirSync(projectPath('src', 'components'))
}
}
private writeNewComponent(name: string): void {
const componentPath = projectPath('src', 'components', `${name}.ts`)
const tpl = new Template({
close: '%>',
open: '<%',
})
const template = tpl.render(componentTemplate, {
camelCaseName: camelCase(name),
name,
pascalCaseName: pascalCase(name),
})
fs.writeFileSync(componentPath, template)
this.log(`Component ${name} created at ${componentPath}`)
}
}