-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathscanner.js
145 lines (121 loc) · 4.89 KB
/
scanner.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
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
'use strict'
const path = require('path')
const walk = require('walk')
const fs = require('fs-extra')
const {generate, getClassNameWithNameSpace, loadExtraTypeDef, writeOutput} = require('./generator.js')
const getSeperatedOutputFilePathWithRightExtension = (filePath, extensionName) => {
const fileNameSegments = filePath.split('.')
fileNameSegments.pop()
fileNameSegments.push(extensionName)
return fileNameSegments.join('.')
}
const fileWalker = ({dir, outPath, seperate, ignores, extensionName}, {verbose, dryrun, watch}, cb) => {
const dest = path.resolve(outPath)
if (verbose || dryrun) console.log('[V] Output full path:', outPath)
const files = new Map()
const walker = walk.walk(dir, {
followLinks: true,
filters: ['node_modules', ...ignores]
})
walker.on('file', (root, fileStats, next) => {
if (['.ef', '.eft', '.efml'].includes(path.extname(fileStats.name))) {
const fileOrigPath = path.join(root, fileStats.name)
const filePath = path.relative(dir, fileOrigPath)
if (verbose || dryrun) console.log('[V] Reading file:', fileOrigPath)
fs.readFile(fileOrigPath, 'utf8', (err, source) => {
if (err) return console.error(err)
const fileName = path.basename(fileStats.name, path.extname(fileStats.name))
const dirName = path.relative(dir, root)
const [className, nameSpace] = getClassNameWithNameSpace(fileName, dirName)
if (verbose || dryrun) {
console.log('[V] Input file:', fileName)
console.log('[V] Relative dir:', dirName)
console.log('[V] Relative input path:', filePath)
console.log('[V] Generated class name:', className)
console.log('[V] Generated namesace:', nameSpace)
}
files.set(filePath, {className, nameSpace, source})
next()
})
} else return next()
})
const writeResults = (e, {$results, dest, currentVersion, needsUpdate}) => {
if (e) {
if (cb) return cb(e)
return console.error(e)
}
if (needsUpdate) return writeOutput({$results, dest, currentVersion}, {verbose, dryrun}, cb)
return cb(null, {$results, dest, currentVersion})
}
if (seperate) walker.on('end', () => {
let doneCount = 0
const checkEnd = (e) => {
if (e) {
if (cb) return cb(e)
return console.error(e)
}
doneCount += 1
if (doneCount >= files.size) {
if (cb) return cb()
return
}
}
const writeAndCheckEnd = (e, {$results, dest, currentVersion}) => {
if (e) {
if (cb) return cb(e)
return console.error(e)
}
doneCount += 1
return writeOutput({$results, dest, currentVersion}, {verbose, dryrun}, checkEnd)
}
for (let [filePath, file] of files) {
const outFilePath = getSeperatedOutputFilePathWithRightExtension(filePath, extensionName)
generate({files: new Map([[filePath, file]]), dest: path.join(dest, outFilePath)}, {verbose, dryrun}, writeAndCheckEnd)
}
})
else walker.on('end', () => generate({files, dest}, {verbose, dryrun, watch}, writeResults))
}
const scanEntry = ({dir = '.', outPath = 'ef.hpp', seperate = false, ignores = [], extensionName = 'hpp', extraTypeDef = '.eftypedef'}, {verbose, dryrun, watch}, cb) => {
if (seperate && outPath === 'ef.hpp') outPath = '.efgenerated/ef'
if (seperate) ignores.push(outPath)
if (verbose || dryrun) {
console.log('[V] Scan dir:', dir)
console.log('[V] Output path:', outPath)
console.log('[V] Seperate headers:', seperate)
console.log('[V] Ignored folder(s):', ignores)
console.log('[V] Extra param type def:', extraTypeDef)
}
const walkFiles = () => fileWalker({dir, outPath, seperate, ignores, extensionName}, {verbose, dryrun, watch}, cb)
if (extraTypeDef) return loadExtraTypeDef({extraTypeDef}, {verbose, dryrun}, walkFiles)
else return walkFiles()
}
const compileSingleFile = ({input, output, base, extraTypeDef}, {verbose, dryrun}, cb) => {
const fileName = path.basename(input, path.extname(input))
const dirName = path.relative(base, path.dirname(input))
const filePath = path.relative(base, input)
const [className, nameSpace] = getClassNameWithNameSpace(fileName, dirName)
if (verbose || dryrun) {
console.log('[V] Input file:', input)
console.log('[V] Output file:', output)
console.log('[V] Relative input path:', filePath)
console.log('[V] Generated class name:', className)
console.log('[V] Generated namesace:', nameSpace)
}
const writeResults = (e, {$results, dest, currentVersion}) => {
if (e) {
if (cb) return cb(e)
return console.error(e)
}
return writeOutput({$results, dest, currentVersion}, {verbose, dryrun}, cb)
}
const compileFile = () => {
fs.readFile(input, 'utf8', (err, source) => {
if (err) return console.error(err)
const files = new Map([[filePath, {className, nameSpace, source}]])
generate({files, dest: output}, {verbose, dryrun}, writeResults)
})
}
if (extraTypeDef) return loadExtraTypeDef({extraTypeDef}, {verbose, dryrun}, compileFile)
else return compileFile()
}
module.exports = {scanEntry, compileSingleFile, getSeperatedOutputFilePathWithRightExtension}