|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import fsSync from "node:fs"; |
| 3 | +import path from "node:path"; |
1 | 4 | import { build } from "esbuild";
|
2 | 5 | import { NodeModulesPolyfillPlugin } from "@esbuild-plugins/node-modules-polyfill";
|
3 | 6 | import chokidar from "chokidar";
|
4 | 7 | import { Command } from "commander";
|
5 | 8 | import glob from "fast-glob";
|
6 |
| -import fs from "node:fs/promises"; |
7 |
| -import path from "node:path"; |
8 | 9 | import SpecLogger, { Level } from "./log";
|
9 | 10 | import { setSetting } from "./settings";
|
10 | 11 |
|
@@ -33,33 +34,55 @@ async function generateIndex(outdir: string, files: string[]) {
|
33 | 34 | .concat(diffVersionedSpecNames);
|
34 | 35 | specNames.sort();
|
35 | 36 |
|
36 |
| - await fs.mkdir(outdir, { recursive: true }); |
| 37 | + const modules = files |
| 38 | + .filter((p) => fsSync.statSync(p).isFile()) |
| 39 | + .map(path.parse) |
| 40 | + .map((p) => `${p.dir}/${p.name}`.replace(/^src\//, "")); |
37 | 41 |
|
38 |
| - Promise.all([ |
39 |
| - // index.js |
40 |
| - await fs.writeFile( |
41 |
| - path.join(outdir, "index.js"), |
42 |
| - `var e=${JSON.stringify(specNames)},diffVersionedCompletions=${JSON.stringify( |
43 |
| - diffVersionedSpecNames |
44 |
| - )};export{e as default,diffVersionedCompletions};` |
45 |
| - ), |
46 |
| - // index.json |
47 |
| - fs.writeFile( |
48 |
| - path.join(outdir, "index.json"), |
49 |
| - JSON.stringify({ |
50 |
| - completions: specNames, |
51 |
| - diffVersionedCompletions: diffVersionedSpecNames, |
52 |
| - }) |
53 |
| - ), |
54 |
| - // index.d.ts |
55 |
| - fs.writeFile( |
56 |
| - path.join(outdir, "index.d.ts"), |
57 |
| - `declare const completions: string[] |
| 42 | + await fs.mkdir(outdir, { recursive: true }); |
| 43 | + await fs.mkdir(path.join(outdir, "dynamic"), { recursive: true }); |
| 44 | + |
| 45 | + // index.js |
| 46 | + await fs.writeFile( |
| 47 | + path.join(outdir, "index.js"), |
| 48 | + `var e=${JSON.stringify(specNames)},diffVersionedCompletions=${JSON.stringify( |
| 49 | + diffVersionedSpecNames |
| 50 | + )};export{e as default,diffVersionedCompletions};` |
| 51 | + ); |
| 52 | + // index.json |
| 53 | + await fs.writeFile( |
| 54 | + path.join(outdir, "index.json"), |
| 55 | + JSON.stringify({ |
| 56 | + completions: specNames, |
| 57 | + diffVersionedCompletions: diffVersionedSpecNames, |
| 58 | + }) |
| 59 | + ); |
| 60 | + // index.d.ts |
| 61 | + await fs.writeFile( |
| 62 | + path.join(outdir, "index.d.ts"), |
| 63 | + `declare const completions: string[] |
58 | 64 | declare const diffVersionedCompletions: string[]
|
59 | 65 | export { completions as default, diffVersionedCompletions }
|
60 |
| - ` |
61 |
| - ), |
62 |
| - ]); |
| 66 | +` |
| 67 | + ); |
| 68 | + // dynamic/index.js |
| 69 | + await fs.writeFile( |
| 70 | + path.join(outdir, "dynamic/index.js"), |
| 71 | + `var e={${modules |
| 72 | + .map((mod) => `${JSON.stringify(mod)}:()=>import(${JSON.stringify(`../${mod}.js`)})`) |
| 73 | + .join(",")}};export{e as default};` |
| 74 | + ); |
| 75 | + // dynamic/index.d.ts |
| 76 | + await fs.writeFile( |
| 77 | + path.join(outdir, "dynamic/index.d.ts"), |
| 78 | + `declare const completions: { |
| 79 | + [key: string]: () => Promise<{ |
| 80 | + default: any; |
| 81 | + }> |
| 82 | +} |
| 83 | +export { completions as default } |
| 84 | +` |
| 85 | + ); |
63 | 86 | }
|
64 | 87 |
|
65 | 88 | /**
|
|
0 commit comments