forked from abalone0204/Clairvoyance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (48 loc) · 1.55 KB
/
index.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
const target = process.argv[2]
const actionMap = require("./actionMap.json")
const fs = require('fs')
main(target, actionMap)
function main(target, actionMap) {
actionMap.forEach(actionObject => {
actionGenerator(target, actionObject)
})
}
function actionGenerator(target, obj) {
const fileName = obj.fileName
const actions = obj.actions
const constants = actions.map(action => getConstantName(action.name))
const resultFile = `./${target}/${obj.fileName}.js`
createFile(resultFile)
const constantString = genConstantString(constants)
const actionCreatorString = genActionCreators(actions)
fs.appendFile(resultFile, constantString + '\n' + actionCreatorString, (err) => {
if (err) throw err
})
}
function genConstantString(constants) {
return constants.map(constant => `export const ${constant} = '${constant}'`).join('\n')
}
function genActionCreators(actions) {
const data = actions.map((action) => {
const args = action.args ? action.args : []
return `
export function ${action.name} (${args.join(', ')}) {
return {
type: ${getConstantName(action.name)}${args.length > 0 ? ',' : ''}
${args.join(',\n')}
}
}`.trim()
}).join('\n\n')
return '\n' + data
}
function createFile(resultFile) {
fs.writeFile(resultFile, '\n', {
flag: 'wx'
}, (err) => {
if (err) throw err
})
}
function getConstantName(camelCaseName) {
return camelCaseName.replace(/([A-Z])/g, '_$1')
.toUpperCase()
}