forked from rthor/cra-generate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.js
132 lines (98 loc) · 3.08 KB
/
generate.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
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
'use strict'
const { EOL } = require('os')
const changeCase = require('change-case')
const fs = require('fs')
const mkdir = require('mkpath')
const chalk = require('chalk')
const path = require('path')
const allowedNameTransforms = {
camelCase: true,
constantCase: true,
headerCase: true,
paramCase: true,
pascalCase: true,
snakeCase: true,
}
function template(file) {
const filePath = path.resolve(__dirname, 'templates/component/', file)
return {
filePath,
content: fs.readFileSync(filePath, 'utf8'),
}
}
function save(componentName, fileName, filePath, content) {
const contents = content.replace(/\$Name\$/g, componentName).replace(/\$name\$/g, fileName)
fs.writeFile(filePath, contents, 'utf8')
}
function getComponentPath(componentName, directory, fileName) {
const cwd = process.cwd()
const root = path.join(cwd, 'src')
mkdir.sync(root)
const dir = path.join(root, directory)
mkdir.sync(dir)
const componentPath = path.join(dir, fileName)
const exists = fs.existsSync(componentPath)
if (exists) {
console.error(`Component [${componentName}] already exists at ${componentPath}`)
return process.exit(1)
}
mkdir.sync(componentPath)
return componentPath
}
function implementTypeChecking(typeSystem, content) {
let mutatedContent
switch (typeSystem) {
case 'flow':
mutatedContent = `// @flow${EOL}${EOL}${content}`
break
default:
mutatedContent = content
break
}
return {
content: mutatedContent,
}
}
function validTransform(key, transform) {
if (!allowedNameTransforms.hasOwnProperty(transform)) {
console.log(`Invalid ${key} name transform`)
console.log(' allowed transform functions are:')
Object.keys(allowedNameTransforms).forEach(trf => {
console.log(` - ${chalk.cyan(trf)}`)
})
process.exit(1)
}
}
function transformNames(component, fileFn, compFn) {
validTransform('fileName', fileFn)
const fileName = changeCase[fileFn](component)
validTransform('component', compFn)
const componentName = changeCase[compFn](component)
return { fileName, componentName }
}
function generate(component, options) {
const {
fileName,
componentName
} = transformNames(component, options.fileFormat, options.componentFormat)
console.log(options.fileFormat, fileName, options.componentFormat, componentName)
const componentPath = getComponentPath(componentName, options.directory, fileName)
const scriptFiles =[
template('index.js'),
template(options.isFunctional ? 'stateless.js' : 'stateful.js'),
]
const styleFiles = [template('styles.css')]
scriptFiles.forEach(script => {
const { name } = path.parse(script.filePath)
const { content } = implementTypeChecking(options.typeCheck, script.content)
const filePath = path.join(componentPath,
`${name === 'index' ? 'index' : fileName}.js`
)
save(componentName, fileName, filePath, content)
})
styleFiles.forEach(style => {
const filePath = path.join(componentPath, 'styles.css')
save(componentName, fileName, filePath, style.content)
})
}
module.exports = generate