-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.ts
225 lines (192 loc) · 7.01 KB
/
build.ts
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import * as path from 'path'
import * as fs from 'fs'
import glob from 'glob'
import { Project, ts, printNode } from 'ts-morph'
const ignore = ['./node_modules/**', './test/**', './lib/**', './build.ts']
const files = glob.sync('./**/*.ts', { ignore }).filter((filename: string) => {
return ~['build.ts'].includes(filename)
&& !filename.endsWith('.test.ts')
&& !filename.startsWith('_')
}).map((filename: string) => filename.replace('.ts', ''))
const nameTable = analysisExport(files)
const OUTPUT_DIRECTORY: string = path.resolve('lib')
fs.mkdirSync(OUTPUT_DIRECTORY, { recursive: true })
build()
generateIndex(ts.ModuleKind.ESNext, OUTPUT_DIRECTORY, `.js`)
generateREADME()
function build() {
const project = new Project({
tsConfigFilePath: path.resolve('./tsconfig.json'),
addFilesFromTsConfig: false
})
files.forEach(filePath => {
// const dirName = path.basename(path.dirname(filePath))
// const fileName = path.basename(filePath, path.extname(filePath))
// const sourceFile = project.addExistingSourceFile(filePath + '.ts')
project.addExistingSourceFile(filePath + '.ts')
})
project.emit({
customTransformers: {
after: [ transformer() ]
}
})
}
function analysisExport(files: string[]) {
const project = new Project({
tsConfigFilePath: path.resolve('./tsconfig.json'),
addFilesFromTsConfig: false
})
const nameTable: Map<string, { name: string, file: string, line: number, column: number }[]> = new Map
const defaultExportFiles: Set<string> = new Set
files.forEach(filePath => {
const dirName = path.basename(path.dirname(filePath))
const fileName = path.basename(filePath, path.extname(filePath))
const sourceFile = project.addExistingSourceFile(filePath + '.ts')
const exportedDeclarations = sourceFile.getExportedDeclarations()
const exportNames: string[] = []
console.log(filePath)
exportedDeclarations.forEach((nodes, symbol) => {
const node = nodes[0]
const { line, column } = sourceFile.getLineAndColumnAtPos(node.getStart())
if('default' === symbol) {
defaultExportFiles.add(
`${dirName}/${fileName} has default export\n` +
` @ ${filePath}.ts:${line}:${column}`
)
}
if(!node) console.debug(nodes, fileName)
const leadingCommentRanges = node.getLeadingCommentRanges()
if(0 !== leadingCommentRanges.length) {
const text = leadingCommentRanges[0].getText()
if(/@noexport/.test(text)) return
}
const names = nameTable.get(symbol) || []
nameTable.set(symbol, names.concat({ name: [dirName, fileName].join('/'), file: `${filePath}.ts`, line, column }))
exportNames.push(symbol)
return
})
project.removeSourceFile(sourceFile)
})
function collectNameConflict(): Set<string> {
const errors: Set<string> = new Set
for (const [name, files] of nameTable) {
if(files.length <= 1) continue
const out = []
out.push(`"${name}" are both exports by ${files.map(({ name }) => name).join(', ')}`)
files.forEach(({ file, line, column }) => {
out.push(` @ ${file}:${line}:${column}`)
})
errors.add(out.join('\n'))
}
return errors
}
if(defaultExportFiles.size) {
const out = []
out.push('[Warning]:\n')
let index = 1
defaultExportFiles.forEach(warning => {
out.push(`${index}. ` + warning)
index++
})
console.warn(out.join('\n'))
}
const errors = collectNameConflict()
if(errors.size) {
const out = []
out.push('[Error]:\n')
let index = 1
errors.forEach(error => {
out.push(`${index}. ` + error)
index++
})
console.error(out.join('\n'))
process.exit(2)
}
return nameTable
}
function generateIndex(moduleKind: ts.ModuleKind, output: string, extname: string) {
const project = new Project({
tsConfigFilePath: path.resolve('./tsconfig.json'),
skipFileDependencyResolution: true,
addFilesFromTsConfig: false,
compilerOptions: {
outDir: output,
module: moduleKind
}
})
const nodes = files.map(filename => {
return ts.createExportDeclaration(
undefined,
undefined,
undefined,
ts.createStringLiteral(filename + '.js')
)
})
const rootIndexPath: string = path.resolve('index.ts')
const sourceFile = project.createSourceFile(rootIndexPath, nodes.map(node => printNode(node)).join('\n'))
const emitOutput = sourceFile.getEmitOutput()
for (const outputFile of emitOutput.getOutputFiles()) {
fs.writeFileSync(
outputFile.getFilePath().replace(/\.js/g, extname),
outputFile.getText(),
'utf-8'
)
}
}
function generateREADME(): void {
let out: string = `\
<br/>
<div align=left>
# util-extra
_name enough_
</div>
<br />
`
const groups: { [key: string]: string[] } = {}
nameTable.forEach((names, symbol) => {
const { file, line } = names[0]
if(!groups[file]) groups[file] = []
groups[file].push(`- [\`${symbol}\`](${file}#L${line})`)
// out.push(`- [\`${symbol}\`](${file}#L${line})`)
})
Object.keys(groups).forEach(file => {
const name = file.replace('./', '').replace('.ts', '')
out += '<details>\n'
out += `<summary>${name}</summary>\n\n`
groups[file].forEach(row => {
out += `${row}\n`
})
out += '</details>\n\n'
})
fs.writeFileSync('README.md', out, 'utf-8')
}
function transformer(): ts.TransformerFactory<ts.SourceFile> {
return (transformationContext: ts.TransformationContext) => {
return (sourceFile: ts.SourceFile) => {
function visitNode(node: ts.Node): ts.VisitResult<ts.Node> {
if (shouldMutateModuleSpecifier(node)) {
if (ts.isImportDeclaration(node)) {
const newModuleSpecifier = ts.createLiteral(`${node.moduleSpecifier.text}.js`)
return ts.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, newModuleSpecifier)
} else if (ts.isExportDeclaration(node)) {
const newModuleSpecifier = ts.createLiteral(`${node.moduleSpecifier.text}.js`)
return ts.updateExportDeclaration(node, node.decorators, node.modifiers, node.exportClause, newModuleSpecifier, node.isTypeOnly)
}
}
return ts.visitEachChild(node, visitNode, transformationContext)
}
function shouldMutateModuleSpecifier(node: ts.Node): node is (ts.ImportDeclaration | ts.ExportDeclaration) & { moduleSpecifier: ts.StringLiteral } {
if (!ts.isImportDeclaration(node) && !ts.isExportDeclaration(node)) return false
if (node.moduleSpecifier === undefined) return false
// only when module specifier is valid
if (!ts.isStringLiteral(node.moduleSpecifier)) return false
// only when path is relative
if (!node.moduleSpecifier.text.startsWith('./') && !node.moduleSpecifier.text.startsWith('../')) return false
// only when module specifier has no extension
if (path.extname(node.moduleSpecifier.text) !== '') return false
return true
}
return ts.visitNode(sourceFile, visitNode)
}
}
}