-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli.ts
More file actions
143 lines (118 loc) · 4.82 KB
/
cli.ts
File metadata and controls
143 lines (118 loc) · 4.82 KB
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
#!/usr/bin/env node
import { program } from 'commander'
import {
processCreateComponentCommand,
processInitCommand,
processPromptCommand,
processUpgradeCommand,
type CommandEnv,
} from './src/commands.js'
import { getDirectories } from './src/utilities.js'
import { readFileSync, existsSync } from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
interface AppConfig {
types: string[] | null
templatePath: string
componentPath: string
nameStyle: string
}
const CONFIG_DIRECTORY = '.create-frontend-component'
const CONFIG_FILE_NAME = 'config.json'
const PRESET_DIR = 'presets'
const PRESET_PATH = path.join(__dirname, '..', PRESET_DIR)
const configDefaults: AppConfig = {
types: ['atoms', 'molecules', 'organisms'],
templatePath: `${CONFIG_DIRECTORY}/templates`,
componentPath: 'src/components',
nameStyle: 'pascalCase'
}
function loadConfig(): AppConfig {
const filePath = path.resolve(process.cwd(), CONFIG_DIRECTORY, CONFIG_FILE_NAME)
try {
if (!existsSync(filePath)) {
console.error(`Error: Configuration file not found at ${filePath}.`)
console.error('Run "npx create-frontend-component init" to generate the configuration file.')
process.exit(1)
}
const fileContent = readFileSync(filePath, 'utf8').replace(/^\ufeff/u, '')
const configFromFile = JSON.parse(fileContent)
return {
types: configDefaults.types ?? null,
templatePath: configDefaults.templatePath,
componentPath: configDefaults.componentPath,
nameStyle: configDefaults.nameStyle,
...configFromFile
}
} catch (error) {
console.error(`Error loading configuration file: ${(error as Error).message}`)
console.error('Try running "npx create-frontend-component init" to reset the configuration.')
process.exit(1)
}
}
const packageJsonPath = path.join(__dirname, '..', 'package.json')
const { version } = JSON.parse(readFileSync(packageJsonPath, { encoding: 'utf-8' }))
program
.name('create-frontend-component')
.description('Frontend Component Generator')
.version(version)
program
.command('init [preset]')
.description('Initialize project with preset')
.action(async (presetArg?: string) => {
await processInitCommand(PRESET_PATH, CONFIG_DIRECTORY, CONFIG_FILE_NAME, configDefaults, presetArg)
})
program
.command('prompt')
.description('Interactive component creation')
.action(async () => {
const { types, templatePath, componentPath, nameStyle } = loadConfig()
const allowedComponentTypes = types || []
const fullTemplatePath = path.join(process.cwd(), templatePath)
const availableFlavours = getDirectories(fullTemplatePath)
if (availableFlavours.length === 0) {
console.error('Error: No flavours found in template directory.')
console.error(`Please ensure templates exist in ${fullTemplatePath}`)
process.exit(1)
}
await processPromptCommand(allowedComponentTypes, availableFlavours, fullTemplatePath, componentPath, nameStyle)
})
program
.command('upgrade')
.description('Add missing files from a different flavour')
.action(async () => {
const { types, templatePath, componentPath, nameStyle } = loadConfig()
const allowedComponentTypes = types || []
const fullTemplatePath = path.join(process.cwd(), templatePath)
const availableFlavours = getDirectories(fullTemplatePath)
if (availableFlavours.length <= 1) {
console.error('Could not detect more than 1 flavour, upgrade is not possible')
process.exit(1)
}
await processUpgradeCommand(availableFlavours, allowedComponentTypes, fullTemplatePath, componentPath, nameStyle)
})
program
.argument('[component-name]', 'Component name')
.option('-t, --type <type>', 'Component type, default: atoms')
.option('-f, --flavour <flavour>', 'Component flavour')
.action(async function(componentNameArg: string | undefined, env: CommandEnv) {
const componentName = componentNameArg || ''
if (!componentName.trim()) {
console.error('Error: Component name is required.')
console.error('Use "npx create-frontend-component prompt" for interactive creation.')
process.exit(1)
}
const { types, templatePath, componentPath, nameStyle } = loadConfig()
const allowedComponentTypes = types || []
const fullTemplatePath = path.join(process.cwd(), templatePath)
const availableFlavours = getDirectories(fullTemplatePath)
if (availableFlavours.length === 0) {
console.error('Error: No flavours found in template directory.')
console.error(`Please ensure templates exist in ${fullTemplatePath}`)
process.exit(1)
}
processCreateComponentCommand(env, allowedComponentTypes, fullTemplatePath, componentPath, componentName, availableFlavours, nameStyle)
})
program.parse(process.argv)