main
1#!/usr/bin/env node
2
3// Copyright 2024 Google LLC
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// https://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17import fs from 'node:fs/promises'
18import { generateDtsBundle } from 'dts-bundle-generator'
19import glob from 'fast-glob'
20
21const output = {
22 inlineDeclareExternals: true,
23 inlineDeclareGlobals: true,
24 sortNodes: false,
25 exportReferencedTypes: false, //args['export-referenced-types'],
26}
27const entries = [
28 {
29 filePath: './src/vendor-extra.ts',
30 outFile: './build/vendor-extra.d.ts',
31 libraries: {
32 allowedTypesLibraries: ['node'], // args['external-types'],
33 inlinedLibraries: [
34 '@nodelib/fs.stat',
35 '@nodelib/fs.scandir',
36 '@nodelib/fs.walk',
37 'fast-glob',
38 '@types/jsonfile',
39 'node-fetch-native',
40 // 'chalk',
41 'globby',
42 '@types/minimist',
43 // '@types/which',
44 // 'zurk',
45 // '@webpod/ps',
46 '@webpod/ingrid',
47 'depseek',
48 'envapi',
49 'maml.js',
50 ], // args['external-inlines'],
51 },
52 output,
53 },
54 {
55 filePath: './src/vendor-core.ts',
56 outFile: './build/vendor-core.d.ts',
57 libraries: {
58 allowedTypesLibraries: ['node'], // args['external-types'],
59 inlinedLibraries: [
60 '@types/which',
61 '@webpod/ps',
62 '@webpod/ingrid',
63 'chalk',
64 'zurk',
65 ], // args['external-inlines'],
66 },
67 output,
68 },
69]
70
71const compilationOptions = {
72 preferredConfigPath: './tsconfig.json', // args.project,
73 followSymlinks: true,
74}
75
76const results = generateDtsBundle(entries, compilationOptions)
77 // generateDtsBundle cannot handle the circular refs on types inlining, so we need to help it manually:
78 /*
79build/vendor.d.ts(163,7): error TS2456: Type alias 'Options' circularly references itself.
80build/vendor.d.ts(164,7): error TS2456: Type alias 'Entry' circularly references itself.
81build/vendor.d.ts(165,7): error TS2456: Type alias 'Task' circularly references itself.
82build/vendor.d.ts(166,7): error TS2456: Type alias 'Pattern' circularly references itself.
83build/vendor.d.ts(167,7): error TS2456: Type alias 'FileSystemAdapter' circularly references itself.
84build/vendor.d.ts(197,48): error TS2694: Namespace 'FastGlob' has no exported member 'FastGlobOptions
85 */
86
87 .map((r) =>
88 r
89 .replace('type Options = Options;', 'export {Options};')
90 .replace('type Task = Task;', 'export {Task};')
91 .replace('type Pattern = Pattern;', 'export {Pattern};')
92 .replace('FastGlob.FastGlobOptions', 'FastGlob.Options')
93 .replace('type Entry =', 'export type Entry =')
94 )
95
96for (const i in results) {
97 const entry = entries[i]
98 const result = results[i]
99
100 await fs.writeFile(entry.outFile, result, 'utf8')
101}
102
103// Properly formats triple-slash directives
104const pkgEntries = ['core', 'index', 'vendor']
105const prefix = `/// <reference types="node" />
106/// <reference types="fs-extra" />
107
108`
109
110for (const dts of await glob(['build/**/*.d.ts', '!build/vendor-*.d.ts'])) {
111 const contents =
112 (pkgEntries.some((e) => dts.includes(e)) ? prefix : '') +
113 (await fs.readFile(dts, 'utf8'))
114 .replaceAll(".ts';", ".js';")
115 .split('\n')
116 .filter((line) => !line.startsWith('/// <reference types'))
117 .join('\n')
118
119 await fs.writeFile(dts, contents, 'utf8')
120}
121
122process.exit(0)