-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·73 lines (61 loc) · 2.37 KB
/
cli.js
File metadata and controls
executable file
·73 lines (61 loc) · 2.37 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
#!/usr/bin/env node
import { program } from 'commander'
import {
processCreateComponentCommand,
processInitCommand,
processPromptCommand,
processUpgradeCommand,
} from './src/commands.js'
import { getDirectories } from './src/utilities.js'
import { readFileSync } from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
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 = {
types: ['atoms', 'molecules', 'organisms'],
templatePath: CONFIG_DIRECTORY + '/templates',
componentPath: 'src/components',
nameStyle: 'pascalCase'
}
/**
* @return {object}
*/
function loadConfig() {
const filePath = path.resolve(process.cwd(), '.create-frontend-component', 'config.json')
const configFromFile = JSON.parse(
readFileSync(filePath, 'utf8').replace(/^\ufeff/u, '')
)
return {
...configDefaults,
...configFromFile
}
}
program
.version('2.0.0')
.command('create-frontend-component [component-name]') // Define the command
.option( '-t, --type <type>', 'Component type, default: atoms')
.option( '-f, --flavour <flavour>', 'Component flavour')
.action( async function(componentNameArg, env) {
const componentName = componentNameArg || ''
if (componentName.toLowerCase() === 'init') {
await processInitCommand(PRESET_PATH, CONFIG_DIRECTORY, CONFIG_FILE_NAME, configDefaults)
return
}
const { types, templatePath, componentPath, nameStyle } = loadConfig()
const allowedComponentTypes = types || []
const fullTemplatePath = path.join(process.cwd(), templatePath)
const availableFlavours = getDirectories(fullTemplatePath)
if (componentName.toLowerCase() === 'prompt' || !componentName.trim()) {
await processPromptCommand(allowedComponentTypes, availableFlavours, fullTemplatePath, componentPath, nameStyle)
} else if (componentName.toLowerCase() === 'upgrade') {
await processUpgradeCommand(availableFlavours, allowedComponentTypes, fullTemplatePath, componentPath, nameStyle)
} else {
processCreateComponentCommand(env, allowedComponentTypes, fullTemplatePath, componentPath, componentName, availableFlavours, nameStyle)
}
})
.parse(process.argv)