-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcg.js
executable file
·103 lines (94 loc) · 3.35 KB
/
cg.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
#!/usr/bin/env node
const program = require("commander")
const packageJson = require("./package.json")
const fs = require('fs')
const path = require('path')
program
.version(packageJson.version, '-v, --version')
.option('-i, --new-implementation <implementation_id>', 'create a new implementation from template')
.option('-s, --new-source <source_name>', 'create a new source module from template')
.option('-m, --include-modules [modules...]', 'include modules when generating an implementation')
.option('-n, --tag-name <tag_name>', 'specify tag name')
.parse(process.argv)
let options = program.opts()
if (options.newImplementation) {
let dest = './implementations/' + options.newImplementation;
if (!dest.endsWith('.js')) {
dest += '.js'
}
if (fs.existsSync(dest)) {
console.log(`File ${dest} already exists`)
process.exit()
}
if (!fs.existsSync('./implementations')) {
fs.mkdirSync('./implementations')
}
let content = fs.readFileSync(path.join(__dirname, 'templates/implementation.js')).toString('utf-8')
if (options.tagName) {
content = content.replaceAll('${tag_name}', options.tagName)
}
if (options.includeModules) {
let paramStr = " "
let sourceStr = "\n"
for (let i = 0; i < options.includeModules.length; i++) {
paramStr += options.includeModules[i] + ((i === options.includeModules.length - 1) ? '' : ',') + ' '
sourceStr += ` '${options.includeModules[i]}',\n`
}
sourceStr = sourceStr.trimEnd()
let paramIndex = content.indexOf('{}') + 1
const sourceMark = '/* data source module names */'
content = content.slice(0, paramIndex) + paramStr + content.slice(paramIndex)
let sourceIndex = content.indexOf(sourceMark) + sourceMark.length
content = content.slice(0, sourceIndex) + sourceStr + content.slice(sourceIndex)
fs.writeFileSync(dest, content)
}
fs.writeFileSync(dest, content)
console.log('Generated new implementation at: ' + dest)
}
if (options.newSource) {
let dest = './sources/' + options.newSource;
if (!dest.endsWith('.js')) {
dest += '.js'
}
if (fs.existsSync(dest)) {
console.log(`File ${dest} already exists`)
process.exit()
}
if (!fs.existsSync('./sources')) {
fs.mkdirSync('./sources')
}
fs.copyFileSync(path.join(__dirname, 'templates/source.js'), dest)
console.log('Generated new source module at: ' + dest)
}
if (!fs.existsSync('./implementations')) {
console.log("No 'implementation' folder found")
process.exit(1)
}
let files = fs.readdirSync('./implementations')
let modules = {}
for (let i = 0; i < files.length; i++) {
if (files[i].endsWith('.js')) {
if (files[i] === 'implementations.js') {
continue
}
let name = files[i].substring(0, files[i].length - 3)
modules[name] = "./" + files[i]
}
}
let implementations = ""
for (const moduleKey in modules) {
const name = moduleKey
const entry = ` "${name}": "${modules[moduleKey]}", \n`
implementations += entry
}
// console.log(implementations)
const conf = `
module.exports = {
config: {
sourceDir: "../sources"
},
implementations: {
${implementations} },
}
`
fs.writeFileSync('./implementations/implementations.js', conf.trim())