-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-types.js
82 lines (61 loc) · 2.01 KB
/
build-types.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
const path = require('path')
const fs = require('fs')
const glob = require('fast-glob')
const { Project } = require('ts-morph')
const { parse, compileScript } = require('@vue/compiler-sfc')
let index = 1
main()
async function main() {
const project = new Project({
compilerOptions: {
declaration: true,
emitDeclarationOnly: true,
noEmitOnError: true,
allowJs: true,
outDir: 'types'
},
tsConfigFilePath: path.resolve(__dirname, './tsconfig.json'),
skipAddingFilesFromTsConfig: true
})
const files = await glob(['src/**/*.ts', 'src/**/*.vue'])
const sourceFiles = []
await Promise.all(
files.map(async file => {
if (/\.vue$/.test(file)) {
const sfc = parse(await fs.promises.readFile(file, 'utf-8'))
const { script, scriptSetup } = sfc.descriptor
if (script || scriptSetup) {
let content = ''
let isTs = false
if (script && script.content) {
content += script.content
if (script.lang === 'ts') isTs = true
}
if (scriptSetup) {
const compiled = compileScript(sfc.descriptor, {
id: `${index++}`
})
content += compiled.content
if (scriptSetup.lang === 'ts') isTs = true
}
sourceFiles.push(
project.createSourceFile(file + (isTs ? '.ts' : '.js'), content)
)
}
} else {
sourceFiles.push(project.addSourceFileAtPath(file))
}
})
)
const diagnostics = project.getPreEmitDiagnostics()
console.log(project.formatDiagnosticsWithColorAndContext(diagnostics))
project.emitToMemory()
for (const sourceFile of sourceFiles) {
const emitOutput = sourceFile.getEmitOutput()
for (const outputFile of emitOutput.getOutputFiles()) {
const filePath = outputFile.getFilePath()
await fs.promises.mkdir(path.dirname(filePath), { recursive: true })
await fs.promises.writeFile(filePath, outputFile.getText(), 'utf8')
}
}
}