-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-snippets-cli.js
More file actions
151 lines (130 loc) · 4.21 KB
/
react-snippets-cli.js
File metadata and controls
151 lines (130 loc) · 4.21 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
144
145
146
147
148
149
150
151
const path = require('path')
const commander = require('commander')
const fs = require('fs-extra')
const chalk = require('chalk')
const handlebars = require('handlebars')
const { lowerCaseFirst, pascalCase, paramCase } = require('change-case')
const packageJson = require('./package.json')
const program = new commander.Command(packageJson.name)
const GEN_TYPES = {
page: 'page',
fc: 'fc',
}
const TEMPLATES_DIRS = {
fcCssInJs: 'templates/fc/cssInJs',
fcCssModule: 'templates/fc/cssModule',
}
const FORMAT_TYPES = {
kebabCase: 'kebabCase', // 'this-is-a-case',
upperCamelCase: 'upperCamelCase', // ThisIsACase
lowerCamelCase: 'lowerCamelCase', // thisIsACase
}
const FC_TYPE = {
cssInJs: 'css-in-js',
cssModule: 'css-module',
}
function init() {
program
.version(packageJson.version)
.name('rsc')
.argument('<genType>')
.argument('<displayName>')
.option('--type <dir>', `${FC_TYPE.cssInJs} | ${FC_TYPE.cssModule}. default is css-module`)
.option('--out <dir>', 'Files generation directory')
.allowUnknownOption()
.usage('<genType> <displayName> [options]')
.action((genType, displayName, options) => {
switch (genType) {
case GEN_TYPES.page:
generatePage(displayName, options.out)
break
case GEN_TYPES.fc:
generateFc(displayName, options)
break
default:
console.log(chalk.redBright('Only support fc types'))
}
})
.on('--help', () => {
console.log(` Only ${chalk.green('<displayName>')} is required.`)
})
.parse(process.argv)
}
function format(displayName, type) {
switch (type) {
case FORMAT_TYPES.kebabCase:
return paramCase(displayName)
case FORMAT_TYPES.lowerCamelCase:
return lowerCaseFirst(displayName)
default:
return pascalCase(displayName)
}
}
function getOutputPath(copyOutDir, compileOpts) {
const outputPath = copyOutDir
.replace(/{{pageName}}/g, compileOpts.pageName)
.replace(/{{componentName}}/g, compileOpts.componentName)
.replace(/\.hbs$/, '')
return outputPath
}
function readFilesList(dir) {
const filesList = []
function inner(innerDir) {
const files = fs.readdirSync(innerDir)
files.forEach((item) => {
const fullPath = path.join(innerDir, item)
const stat = fs.statSync(fullPath)
filesList.push(fullPath)
if (stat.isDirectory()) {
inner(path.join(innerDir, item), filesList)
}
})
}
inner(dir)
return filesList
}
function createFiles(templateDir, outRootDir, { compileOptions, relativePathCreator }) {
try {
const fileList = readFilesList(templateDir)
fileList.forEach((item) => {
const stat = fs.statSync(item)
const relativePath = relativePathCreator(item)
const outputPath = getOutputPath(path.resolve(outRootDir, relativePath), compileOptions)
if (stat.isFile()) {
const content = fs.readFileSync(item).toString()
const result = handlebars.compile(content)(compileOptions)
fs.writeFileSync(outputPath, result)
} else {
fs.mkdirSync(outputPath)
}
})
} catch (error) {
console.log(`create files error: ${chalk.redBright(error)}`)
}
}
function generatePage(displayName, optionOut) {
const pageName = format(displayName, FORMAT_TYPES.kebabCase)
const componentName = format(displayName, FORMAT_TYPES.upperCamelCase)
const outRootDir = path.resolve(process.cwd(), optionOut || '')
const templateDir = path.resolve(__dirname, TEMPLATES_DIRS.page)
createFiles(templateDir, outRootDir, {
compileOptions: { pageName, componentName },
relativePathCreator: (item) => item.substring(item.indexOf('{{pageName}}')),
})
}
function generateFc(displayName, options) {
const { out, type = FC_TYPE.cssModule } = options
const componentName = format(displayName, FORMAT_TYPES.upperCamelCase)
const outRootDir = path.resolve(process.cwd(), out || '')
const templateDir = path.resolve(
__dirname,
type === FC_TYPE.cssInJs ? TEMPLATES_DIRS.fcCssInJs : TEMPLATES_DIRS.fcCssModule
)
createFiles(templateDir, outRootDir, {
compileOptions: { componentName },
relativePathCreator: (item) => item.substring(item.indexOf('{{componentName}}')),
})
}
module.exports = {
init,
}