-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathbuild-dts.mjs
More file actions
122 lines (109 loc) · 3.72 KB
/
build-dts.mjs
File metadata and controls
122 lines (109 loc) · 3.72 KB
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
#!/usr/bin/env node
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import fs from 'node:fs/promises'
import { generateDtsBundle } from 'dts-bundle-generator'
import glob from 'fast-glob'
const output = {
inlineDeclareExternals: true,
inlineDeclareGlobals: true,
sortNodes: false,
exportReferencedTypes: false, //args['export-referenced-types'],
}
const entries = [
{
filePath: './src/vendor-extra.ts',
outFile: './build/vendor-extra.d.ts',
libraries: {
allowedTypesLibraries: ['node'], // args['external-types'],
inlinedLibraries: [
'@nodelib/fs.stat',
'@nodelib/fs.scandir',
'@nodelib/fs.walk',
'fast-glob',
'@types/jsonfile',
'node-fetch-native',
// 'chalk',
'globby',
'@types/minimist',
// '@types/which',
// 'zurk',
// '@webpod/ps',
'@webpod/ingrid',
'depseek',
'envapi',
'maml',
], // args['external-inlines'],
},
output,
},
{
filePath: './src/vendor-core.ts',
outFile: './build/vendor-core.d.ts',
libraries: {
allowedTypesLibraries: ['node'], // args['external-types'],
inlinedLibraries: [
'@types/which',
'@webpod/ps',
'@webpod/ingrid',
'chalk',
'zurk',
], // args['external-inlines'],
},
output,
},
]
const compilationOptions = {
preferredConfigPath: './tsconfig.json', // args.project,
followSymlinks: true,
}
const results = generateDtsBundle(entries, compilationOptions)
// generateDtsBundle cannot handle the circular refs on types inlining, so we need to help it manually:
/*
build/vendor.d.ts(163,7): error TS2456: Type alias 'Options' circularly references itself.
build/vendor.d.ts(164,7): error TS2456: Type alias 'Entry' circularly references itself.
build/vendor.d.ts(165,7): error TS2456: Type alias 'Task' circularly references itself.
build/vendor.d.ts(166,7): error TS2456: Type alias 'Pattern' circularly references itself.
build/vendor.d.ts(167,7): error TS2456: Type alias 'FileSystemAdapter' circularly references itself.
build/vendor.d.ts(197,48): error TS2694: Namespace 'FastGlob' has no exported member 'FastGlobOptions
*/
.map((r) =>
r
.replace('type Options = Options;', 'export {Options};')
.replace('type Task = Task;', 'export {Task};')
.replace('type Pattern = Pattern;', 'export {Pattern};')
.replace('FastGlob.FastGlobOptions', 'FastGlob.Options')
.replace('type Entry =', 'export type Entry =')
)
for (const i in results) {
const entry = entries[i]
const result = results[i]
await fs.writeFile(entry.outFile, result, 'utf8')
}
// Properly formats triple-slash directives
const pkgEntries = ['core', 'index', 'vendor']
const prefix = `/// <reference types="node" />
/// <reference types="fs-extra" />
`
for (const dts of await glob(['build/**/*.d.ts', '!build/vendor-*.d.ts'])) {
const contents =
(pkgEntries.some((e) => dts.includes(e)) ? prefix : '') +
(await fs.readFile(dts, 'utf8'))
.replaceAll(".ts';", ".js';")
.split('\n')
.filter((line) => !line.startsWith('/// <reference types'))
.join('\n')
await fs.writeFile(dts, contents, 'utf8')
}
process.exit(0)