-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcli.js
executable file
·60 lines (52 loc) · 2.12 KB
/
cli.js
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
#!/usr/bin/env node
const program = require('commander')
const path = require('path')
const { readFileSync } = require('fs')
const { getDirectories } = require('./src/utilities')
const { processPromptCommand, processUpgradeCommand, processCreateComponentCommand, processInitCommand } = require('./src/commands')
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('1.4.1')
.arguments('<component-name>')
.option( '-t, --type <type>', 'Component type, default: atoms')
.option( '-f, --flavour <flavour>', 'Component flavour')
.action( async function(componentName, env) {
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') {
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)