@kubb/fabric-core 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/dist/{App-D3DHa4Il.d.cts → App-Cplfh8QA.d.cts} +21 -18
  2. package/dist/{App-BxAl3dNP.d.ts → App-Dvetv2V_.d.ts} +21 -18
  3. package/dist/createParser-17uGjfu3.js +11 -0
  4. package/dist/createParser-17uGjfu3.js.map +1 -0
  5. package/dist/createParser-C4IkyTs5.cjs +17 -0
  6. package/dist/createParser-C4IkyTs5.cjs.map +1 -0
  7. package/dist/{defaultParser-vwyTb1XT.js → defaultParser--HzU9LPa.js} +2 -2
  8. package/dist/{defaultParser-vwyTb1XT.js.map → defaultParser--HzU9LPa.js.map} +1 -1
  9. package/dist/{defaultParser-Dl-OrbH1.cjs → defaultParser-n9VW2iVf.cjs} +2 -2
  10. package/dist/{defaultParser-Dl-OrbH1.cjs.map → defaultParser-n9VW2iVf.cjs.map} +1 -1
  11. package/dist/defineProperty-3OJdpith.js +168 -0
  12. package/dist/defineProperty-3OJdpith.js.map +1 -0
  13. package/dist/defineProperty-CjCLDutJ.cjs +201 -0
  14. package/dist/defineProperty-CjCLDutJ.cjs.map +1 -0
  15. package/dist/getRelativePath-C6lvNCs7.cjs +26 -0
  16. package/dist/getRelativePath-C6lvNCs7.cjs.map +1 -0
  17. package/dist/getRelativePath-CERJmYkp.js +19 -0
  18. package/dist/getRelativePath-CERJmYkp.js.map +1 -0
  19. package/dist/index-Agz-2M75.d.ts +18 -0
  20. package/dist/index-C3GyFwE1.d.cts +18 -0
  21. package/dist/index.cjs +56 -205
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.d.cts +3 -14
  24. package/dist/index.d.ts +3 -14
  25. package/dist/index.js +28 -174
  26. package/dist/index.js.map +1 -1
  27. package/dist/parsers/typescript.cjs +4 -2
  28. package/dist/parsers/typescript.d.cts +2 -2
  29. package/dist/parsers/typescript.d.ts +2 -2
  30. package/dist/parsers/typescript.js +4 -2
  31. package/dist/parsers.cjs +5 -3
  32. package/dist/parsers.cjs.map +1 -1
  33. package/dist/parsers.d.cts +3 -3
  34. package/dist/parsers.d.ts +3 -3
  35. package/dist/parsers.js +5 -3
  36. package/dist/parsers.js.map +1 -1
  37. package/dist/plugins.cjs +207 -17
  38. package/dist/plugins.cjs.map +1 -1
  39. package/dist/plugins.d.cts +40 -7
  40. package/dist/plugins.d.ts +40 -7
  41. package/dist/plugins.js +194 -4
  42. package/dist/plugins.js.map +1 -1
  43. package/dist/{chunk-CUT6urMc.cjs → trimExtName-Bb4zGVF1.cjs} +14 -1
  44. package/dist/trimExtName-Bb4zGVF1.cjs.map +1 -0
  45. package/dist/trimExtName-CeOVQVbu.js +8 -0
  46. package/dist/trimExtName-CeOVQVbu.js.map +1 -0
  47. package/dist/types.d.cts +2 -2
  48. package/dist/types.d.ts +2 -2
  49. package/dist/{typescriptParser-CctRhsng.d.ts → typescriptParser-B49WHoGL.d.ts} +2 -2
  50. package/dist/{typescriptParser-JawJ8wET.cjs → typescriptParser-CNHO6H2_.cjs} +10 -24
  51. package/dist/typescriptParser-CNHO6H2_.cjs.map +1 -0
  52. package/dist/{typescriptParser-CrzOv_Aw.js → typescriptParser-CWT7zCJy.js} +5 -18
  53. package/dist/typescriptParser-CWT7zCJy.js.map +1 -0
  54. package/dist/{typescriptParser--N0n8KFn.d.cts → typescriptParser-CxGhFQXh.d.cts} +2 -2
  55. package/package.json +1 -1
  56. package/src/App.ts +28 -19
  57. package/src/FileManager.ts +8 -1
  58. package/src/FileProcessor.ts +7 -1
  59. package/src/defineApp.ts +7 -7
  60. package/src/parsers/createParser.ts +1 -1
  61. package/src/parsers/types.ts +2 -2
  62. package/src/plugins/barrelPlugin.ts +189 -0
  63. package/src/plugins/createPlugin.ts +1 -1
  64. package/src/plugins/fsPlugin.ts +19 -3
  65. package/src/plugins/index.ts +1 -0
  66. package/src/plugins/types.ts +4 -4
  67. package/src/utils/AsyncEventEmitter.ts +8 -0
  68. package/src/utils/EventEmitter.ts +8 -0
  69. package/src/utils/TreeNode.ts +118 -0
  70. package/dist/createParser-B_RpW6sx.js +0 -17
  71. package/dist/createParser-B_RpW6sx.js.map +0 -1
  72. package/dist/createParser-DZB5qExa.cjs +0 -29
  73. package/dist/createParser-DZB5qExa.cjs.map +0 -1
  74. package/dist/defineApp-B9W1A5SV.d.ts +0 -9
  75. package/dist/defineApp-BP97CT5p.d.cts +0 -9
  76. package/dist/typescriptParser-CrzOv_Aw.js.map +0 -1
  77. package/dist/typescriptParser-JawJ8wET.cjs.map +0 -1
package/dist/plugins.cjs CHANGED
@@ -1,10 +1,12 @@
1
- const require_chunk = require('./chunk-CUT6urMc.cjs');
1
+ const require_trimExtName = require('./trimExtName-Bb4zGVF1.cjs');
2
+ const require_defineProperty = require('./defineProperty-CjCLDutJ.cjs');
3
+ const require_getRelativePath = require('./getRelativePath-C6lvNCs7.cjs');
2
4
  let node_path = require("node:path");
3
- node_path = require_chunk.__toESM(node_path);
5
+ node_path = require_trimExtName.__toESM(node_path);
4
6
  let js_runtime = require("js-runtime");
5
- js_runtime = require_chunk.__toESM(js_runtime);
7
+ js_runtime = require_trimExtName.__toESM(js_runtime);
6
8
  let fs_extra = require("fs-extra");
7
- fs_extra = require_chunk.__toESM(fs_extra);
9
+ fs_extra = require_trimExtName.__toESM(fs_extra);
8
10
 
9
11
  //#region src/plugins/createPlugin.ts
10
12
  function createPlugin(plugin) {
@@ -16,28 +18,28 @@ function createPlugin(plugin) {
16
18
 
17
19
  //#endregion
18
20
  //#region src/plugins/fsPlugin.ts
19
- async function write(path, data, options = {}) {
21
+ async function write(path$1, data, options = {}) {
20
22
  if (data.trim() === "") return;
21
23
  return (0, js_runtime.switcher)({
22
- node: async (path$1, data$1, { sanity }) => {
24
+ node: async (path$2, data$1, { sanity }) => {
23
25
  try {
24
- const oldContent = await fs_extra.default.readFile((0, node_path.resolve)(path$1), { encoding: "utf-8" });
26
+ const oldContent = await fs_extra.default.readFile((0, node_path.resolve)(path$2), { encoding: "utf-8" });
25
27
  if ((oldContent === null || oldContent === void 0 ? void 0 : oldContent.toString()) === (data$1 === null || data$1 === void 0 ? void 0 : data$1.toString())) return;
26
28
  } catch (_err) {}
27
- await fs_extra.default.outputFile((0, node_path.resolve)(path$1), data$1, { encoding: "utf-8" });
29
+ await fs_extra.default.outputFile((0, node_path.resolve)(path$2), data$1, { encoding: "utf-8" });
28
30
  if (sanity) {
29
- const savedData = await fs_extra.default.readFile((0, node_path.resolve)(path$1), { encoding: "utf-8" });
30
- if ((savedData === null || savedData === void 0 ? void 0 : savedData.toString()) !== (data$1 === null || data$1 === void 0 ? void 0 : data$1.toString())) throw new Error(`Sanity check failed for ${path$1}\n\nData[${data$1.length}]:\n${data$1}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
31
+ const savedData = await fs_extra.default.readFile((0, node_path.resolve)(path$2), { encoding: "utf-8" });
32
+ if ((savedData === null || savedData === void 0 ? void 0 : savedData.toString()) !== (data$1 === null || data$1 === void 0 ? void 0 : data$1.toString())) throw new Error(`Sanity check failed for ${path$2}\n\nData[${data$1.length}]:\n${data$1}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
31
33
  return savedData;
32
34
  }
33
35
  return data$1;
34
36
  },
35
- bun: async (path$1, data$1, { sanity }) => {
37
+ bun: async (path$2, data$1, { sanity }) => {
36
38
  try {
37
- await Bun.write((0, node_path.resolve)(path$1), data$1);
39
+ await Bun.write((0, node_path.resolve)(path$2), data$1);
38
40
  if (sanity) {
39
- const savedData = await Bun.file((0, node_path.resolve)(path$1)).text();
40
- if ((savedData === null || savedData === void 0 ? void 0 : savedData.toString()) !== (data$1 === null || data$1 === void 0 ? void 0 : data$1.toString())) throw new Error(`Sanity check failed for ${path$1}\n\nData[${path$1.length}]:\n${path$1}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
41
+ const savedData = await Bun.file((0, node_path.resolve)(path$2)).text();
42
+ if ((savedData === null || savedData === void 0 ? void 0 : savedData.toString()) !== (data$1 === null || data$1 === void 0 ? void 0 : data$1.toString())) throw new Error(`Sanity check failed for ${path$2}\n\nData[${path$2.length}]:\n${path$2}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
41
43
  return savedData;
42
44
  }
43
45
  return data$1;
@@ -45,12 +47,12 @@ async function write(path, data, options = {}) {
45
47
  console.error(e);
46
48
  }
47
49
  }
48
- }, "node")(path, data.trim(), options);
50
+ }, "node")(path$1, data.trim(), options);
49
51
  }
50
52
  const fsPlugin = createPlugin({
51
53
  name: "fs",
52
- scope: "write",
53
- async install(app, options) {
54
+ install(app, options) {
55
+ if (options === null || options === void 0 ? void 0 : options.clean) fs_extra.default.removeSync(options.clean.path);
54
56
  app.context.events.on("process:progress", async ({ file, source }) => {
55
57
  if (options === null || options === void 0 ? void 0 : options.onWrite) await options.onWrite(file.path, source);
56
58
  await write(file.path, source, { sanity: false });
@@ -71,6 +73,194 @@ const fsPlugin = createPlugin({
71
73
  });
72
74
 
73
75
  //#endregion
76
+ //#region \0@oxc-project+runtime@0.95.0/helpers/classPrivateFieldSet2.js
77
+ function _classPrivateFieldSet2(s, a, r) {
78
+ return s.set(require_defineProperty._assertClassBrand(s, a), r), r;
79
+ }
80
+
81
+ //#endregion
82
+ //#region src/utils/TreeNode.ts
83
+ var _cachedLeaves = /* @__PURE__ */ new WeakMap();
84
+ var TreeNode = class TreeNode {
85
+ constructor(data, parent) {
86
+ require_defineProperty._defineProperty(this, "data", void 0);
87
+ require_defineProperty._defineProperty(this, "parent", void 0);
88
+ require_defineProperty._defineProperty(this, "children", []);
89
+ require_defineProperty._classPrivateFieldInitSpec(this, _cachedLeaves, void 0);
90
+ this.data = data;
91
+ this.parent = parent;
92
+ }
93
+ addChild(data) {
94
+ const child = new TreeNode(data, this);
95
+ this.children.push(child);
96
+ _classPrivateFieldSet2(_cachedLeaves, this, void 0);
97
+ return child;
98
+ }
99
+ get leaves() {
100
+ if (require_defineProperty._classPrivateFieldGet2(_cachedLeaves, this)) return require_defineProperty._classPrivateFieldGet2(_cachedLeaves, this);
101
+ if (this.children.length === 0) return [this];
102
+ const stack = [...this.children];
103
+ const result = [];
104
+ for (const node of stack) if (node.children.length) for (const child of node.children) stack.push(child);
105
+ else result.push(node);
106
+ _classPrivateFieldSet2(_cachedLeaves, this, result);
107
+ return result;
108
+ }
109
+ forEach(callback) {
110
+ const stack = [this];
111
+ for (const node of stack) {
112
+ callback(node);
113
+ if (node.children.length) for (const child of node.children) stack.push(child);
114
+ }
115
+ return this;
116
+ }
117
+ findDeep(predicate) {
118
+ for (const leaf of this.leaves) if (predicate(leaf)) return leaf;
119
+ }
120
+ static fromFiles(files, rootFolder = "") {
121
+ const normalizePath = (p) => p.replace(/\\/g, "/");
122
+ const normalizedRoot = normalizePath(rootFolder);
123
+ const rootPrefix = normalizedRoot.endsWith("/") ? normalizedRoot : `${normalizedRoot}/`;
124
+ const filteredFiles = files.filter((file) => {
125
+ const filePath = normalizePath(file.path);
126
+ return !filePath.endsWith(".json") && (!rootFolder || filePath.startsWith(rootPrefix));
127
+ });
128
+ if (filteredFiles.length === 0) return null;
129
+ const treeNode = new TreeNode({
130
+ name: rootFolder || "",
131
+ path: rootFolder || "",
132
+ file: void 0
133
+ });
134
+ for (const file of filteredFiles) {
135
+ const parts = normalizePath(file.path).slice(rootPrefix.length).split("/");
136
+ let current = treeNode;
137
+ let currentPath = rootFolder;
138
+ for (const [index, part] of parts.entries()) {
139
+ const isLast = index === parts.length - 1;
140
+ currentPath += (currentPath.endsWith("/") ? "" : "/") + part;
141
+ let next;
142
+ for (const child of current.children) if (child.data.name === part) {
143
+ next = child;
144
+ break;
145
+ }
146
+ if (!next) next = current.addChild({
147
+ name: part,
148
+ path: currentPath,
149
+ file: isLast ? file : void 0
150
+ });
151
+ current = next;
152
+ }
153
+ }
154
+ return treeNode;
155
+ }
156
+ };
157
+
158
+ //#endregion
159
+ //#region src/plugins/barrelPlugin.ts
160
+ function getBarrelFiles({ files, root, mode }) {
161
+ if (mode === "propagate" || mode === false) return [];
162
+ const cachedFiles = /* @__PURE__ */ new Map();
163
+ const dedupe = /* @__PURE__ */ new Map();
164
+ const tree = TreeNode.fromFiles(files, root);
165
+ tree === null || tree === void 0 || tree.forEach((node) => {
166
+ var _node$parent;
167
+ if (!(node === null || node === void 0 ? void 0 : node.children) || !((_node$parent = node.parent) === null || _node$parent === void 0 ? void 0 : _node$parent.data.path)) return;
168
+ const parentPath = node.parent.data.path;
169
+ const barrelPath = node_path.default.join(parentPath, "index.ts");
170
+ let barrelFile = cachedFiles.get(barrelPath);
171
+ if (!barrelFile) {
172
+ barrelFile = require_defineProperty.createFile({
173
+ path: barrelPath,
174
+ baseName: "index.ts",
175
+ exports: [],
176
+ sources: []
177
+ });
178
+ cachedFiles.set(barrelPath, barrelFile);
179
+ dedupe.set(barrelPath, /* @__PURE__ */ new Set());
180
+ }
181
+ const seen = dedupe.get(barrelPath);
182
+ node.leaves.forEach((leaf) => {
183
+ const file = leaf.data.file;
184
+ if (!file) return;
185
+ (file.sources || []).forEach((source) => {
186
+ if (!file.path || !source.isIndexable || !source.name) return;
187
+ const key = `${source.name}|${source.isTypeOnly ? "1" : "0"}`;
188
+ if (seen.has(key)) return;
189
+ seen.add(key);
190
+ barrelFile.exports.push({
191
+ name: [source.name],
192
+ path: require_getRelativePath.getRelativePath(parentPath, file.path),
193
+ isTypeOnly: source.isTypeOnly
194
+ });
195
+ barrelFile.sources.push({
196
+ name: source.name,
197
+ isTypeOnly: source.isTypeOnly,
198
+ value: "",
199
+ isExportable: mode === "all" || mode === "named",
200
+ isIndexable: mode === "all" || mode === "named"
201
+ });
202
+ });
203
+ });
204
+ });
205
+ const result = [...cachedFiles.values()];
206
+ if (mode === "all") return result.map((file) => {
207
+ var _file$exports;
208
+ return {
209
+ ...file,
210
+ exports: (_file$exports = file.exports) === null || _file$exports === void 0 ? void 0 : _file$exports.map((e) => ({
211
+ ...e,
212
+ name: void 0
213
+ }))
214
+ };
215
+ });
216
+ return result;
217
+ }
218
+ const barrelPlugin = createPlugin({
219
+ name: "barrel",
220
+ install(app, options) {
221
+ if (!options) throw new Error("Barrel plugin requires options.root and options.mode");
222
+ if (!options.mode) return;
223
+ app.context.events.onOnce("process:end", async ({ files }) => {
224
+ const root = options.root;
225
+ const barrelFiles = getBarrelFiles({
226
+ files,
227
+ root,
228
+ mode: options.mode
229
+ });
230
+ await app.context.fileManager.add(...barrelFiles);
231
+ await app.context.fileManager.write({ parsers: app.context.installedParsers });
232
+ });
233
+ },
234
+ inject(app) {
235
+ return { async writeEntry({ root, mode }) {
236
+ if (!mode || mode === "propagate") return;
237
+ const rootPath = (0, node_path.resolve)(root, "index.ts");
238
+ const rootFile = require_defineProperty.createFile({
239
+ path: rootPath,
240
+ baseName: "index.ts",
241
+ exports: app.files.filter((file) => {
242
+ return file.sources.some((source) => source.isIndexable);
243
+ }).flatMap((file) => {
244
+ var _file$sources;
245
+ const containsOnlyTypes = file.sources.every((source) => source.isTypeOnly);
246
+ return (_file$sources = file.sources) === null || _file$sources === void 0 ? void 0 : _file$sources.map((source) => {
247
+ if (!file.path || !source.isIndexable) return;
248
+ return {
249
+ name: mode === "all" ? void 0 : [source.name],
250
+ path: require_getRelativePath.getRelativePath(rootPath, file.path),
251
+ isTypeOnly: mode === "all" ? containsOnlyTypes : source.isTypeOnly
252
+ };
253
+ }).filter(Boolean);
254
+ }).filter(Boolean),
255
+ sources: []
256
+ });
257
+ await app.context.fileManager.add(rootFile);
258
+ } };
259
+ }
260
+ });
261
+
262
+ //#endregion
263
+ exports.barrelPlugin = barrelPlugin;
74
264
  exports.createPlugin = createPlugin;
75
265
  exports.fsPlugin = fsPlugin;
76
266
  //# sourceMappingURL=plugins.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"plugins.cjs","names":["fs","path","data"],"sources":["../src/plugins/createPlugin.ts","../src/plugins/fsPlugin.ts"],"sourcesContent":["import type { Plugin, UserPlugin } from './types.ts'\n\nexport function createPlugin<Options = any[], TAppExtension extends Record<string, any> = {}>(\n plugin: UserPlugin<Options, TAppExtension>,\n): Plugin<Options, TAppExtension> {\n return {\n type: 'plugin',\n ...plugin,\n }\n}\n","import { createPlugin } from './createPlugin.ts'\nimport { switcher } from 'js-runtime'\nimport fs from 'fs-extra'\nimport { resolve } from 'node:path'\nimport type * as KubbFile from '../KubbFile.ts'\n\ntype WriteOptions = {\n extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>\n dryRun?: boolean\n}\n\ntype Options = {\n /**\n * Optional callback that is invoked whenever a file is written by the plugin.\n * Useful for tests to observe write operations without spying on internal functions.\n */\n onWrite?: (path: string, data: string) => void | Promise<void>\n}\n\ntype ExtendOptions = {\n write(options?: WriteOptions): Promise<void>\n}\n\nexport async function write(path: string, data: string, options: { sanity?: boolean } = {}): Promise<string | undefined> {\n if (data.trim() === '') {\n return undefined\n }\n return switcher(\n {\n node: async (path: string, data: string, { sanity }: { sanity?: boolean }) => {\n try {\n const oldContent = await fs.readFile(resolve(path), {\n encoding: 'utf-8',\n })\n if (oldContent?.toString() === data?.toString()) {\n return\n }\n } catch (_err) {\n /* empty */\n }\n\n await fs.outputFile(resolve(path), data, { encoding: 'utf-8' })\n\n if (sanity) {\n const savedData = await fs.readFile(resolve(path), {\n encoding: 'utf-8',\n })\n\n if (savedData?.toString() !== data?.toString()) {\n throw new Error(`Sanity check failed for ${path}\\n\\nData[${data.length}]:\\n${data}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n\n return savedData\n }\n\n return data\n },\n bun: async (path: string, data: string, { sanity }: { sanity?: boolean }) => {\n try {\n await Bun.write(resolve(path), data)\n\n if (sanity) {\n const file = Bun.file(resolve(path))\n const savedData = await file.text()\n\n if (savedData?.toString() !== data?.toString()) {\n throw new Error(`Sanity check failed for ${path}\\n\\nData[${path.length}]:\\n${path}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n\n return savedData\n }\n\n return data\n } catch (e) {\n console.error(e)\n }\n },\n },\n 'node',\n )(path, data.trim(), options)\n}\n\ndeclare module '../index.ts' {\n interface App {\n write(options?: WriteOptions): Promise<void>\n }\n}\n\nexport const fsPlugin = createPlugin<Options, ExtendOptions>({\n name: 'fs',\n scope: 'write',\n async install(app, options) {\n app.context.events.on('process:progress', async ({ file, source }) => {\n if (options?.onWrite) {\n await options.onWrite(file.path, source)\n }\n await write(file.path, source, { sanity: false })\n })\n },\n inject(app) {\n return {\n async write(\n options = {\n extension: { '.ts': '.ts' },\n dryRun: false,\n },\n ) {\n await app.context.fileManager.write({\n extension: options.extension,\n dryRun: options.dryRun,\n parsers: app.context.installedParsers,\n })\n },\n }\n },\n})\n"],"mappings":";;;;;;;;;AAEA,SAAgB,aACd,QACgC;AAChC,QAAO;EACL,MAAM;EACN,GAAG;EACJ;;;;;ACeH,eAAsB,MAAM,MAAc,MAAc,UAAgC,EAAE,EAA+B;AACvH,KAAI,KAAK,MAAM,KAAK,GAClB;AAEF,iCACE;EACE,MAAM,OAAO,QAAc,QAAc,EAAE,aAAmC;AAC5E,OAAI;IACF,MAAM,aAAa,MAAMA,iBAAG,gCAAiBC,OAAK,EAAE,EAClD,UAAU,SACX,CAAC;AACF,iEAAI,WAAY,UAAU,uDAAKC,OAAM,UAAU,EAC7C;YAEK,MAAM;AAIf,SAAMF,iBAAG,kCAAmBC,OAAK,EAAEC,QAAM,EAAE,UAAU,SAAS,CAAC;AAE/D,OAAI,QAAQ;IACV,MAAM,YAAY,MAAMF,iBAAG,gCAAiBC,OAAK,EAAE,EACjD,UAAU,SACX,CAAC;AAEF,+DAAI,UAAW,UAAU,uDAAKC,OAAM,UAAU,EAC5C,OAAM,IAAI,MAAM,2BAA2BD,OAAK,WAAWC,OAAK,OAAO,MAAMA,OAAK,YAAY,UAAU,OAAO,MAAM,UAAU,IAAI;AAGrI,WAAO;;AAGT,UAAOA;;EAET,KAAK,OAAO,QAAc,QAAc,EAAE,aAAmC;AAC3E,OAAI;AACF,UAAM,IAAI,6BAAcD,OAAK,EAAEC,OAAK;AAEpC,QAAI,QAAQ;KAEV,MAAM,YAAY,MADL,IAAI,4BAAaD,OAAK,CAAC,CACP,MAAM;AAEnC,gEAAI,UAAW,UAAU,uDAAKC,OAAM,UAAU,EAC5C,OAAM,IAAI,MAAM,2BAA2BD,OAAK,WAAWA,OAAK,OAAO,MAAMA,OAAK,YAAY,UAAU,OAAO,MAAM,UAAU,IAAI;AAGrI,YAAO;;AAGT,WAAOC;YACA,GAAG;AACV,YAAQ,MAAM,EAAE;;;EAGrB,EACD,OACD,CAAC,MAAM,KAAK,MAAM,EAAE,QAAQ;;AAS/B,MAAa,WAAW,aAAqC;CAC3D,MAAM;CACN,OAAO;CACP,MAAM,QAAQ,KAAK,SAAS;AAC1B,MAAI,QAAQ,OAAO,GAAG,oBAAoB,OAAO,EAAE,MAAM,aAAa;AACpE,yDAAI,QAAS,QACX,OAAM,QAAQ,QAAQ,KAAK,MAAM,OAAO;AAE1C,SAAM,MAAM,KAAK,MAAM,QAAQ,EAAE,QAAQ,OAAO,CAAC;IACjD;;CAEJ,OAAO,KAAK;AACV,SAAO,EACL,MAAM,MACJ,UAAU;GACR,WAAW,EAAE,OAAO,OAAO;GAC3B,QAAQ;GACT,EACD;AACA,SAAM,IAAI,QAAQ,YAAY,MAAM;IAClC,WAAW,QAAQ;IACnB,QAAQ,QAAQ;IAChB,SAAS,IAAI,QAAQ;IACtB,CAAC;KAEL;;CAEJ,CAAC"}
1
+ {"version":3,"file":"plugins.cjs","names":["fs","path","data","stack: Array<TreeNode<TData>>","result: Array<TreeNode<TData>>","next: TreeNode<BarrelData> | undefined","path","createFile","getRelativePath"],"sources":["../src/plugins/createPlugin.ts","../src/plugins/fsPlugin.ts","../src/utils/TreeNode.ts","../src/plugins/barrelPlugin.ts"],"sourcesContent":["import type { Plugin, UserPlugin } from './types.ts'\n\nexport function createPlugin<Options = unknown, TAppExtension extends Record<string, any> = {}>(\n plugin: UserPlugin<Options, TAppExtension>,\n): Plugin<Options, TAppExtension> {\n return {\n type: 'plugin',\n ...plugin,\n }\n}\n","import { createPlugin } from './createPlugin.ts'\nimport { switcher } from 'js-runtime'\nimport fs from 'fs-extra'\nimport { resolve } from 'node:path'\nimport type * as KubbFile from '../KubbFile.ts'\n\ntype WriteOptions = {\n extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>\n dryRun?: boolean\n}\n\ntype Options = {\n /**\n * Optional callback that is invoked whenever a file is written by the plugin.\n * Useful for tests to observe write operations without spying on internal functions.\n */\n onWrite?: (path: string, data: string) => void | Promise<void>\n clean?: {\n path: string\n }\n}\n\ntype ExtendOptions = {\n write(options?: WriteOptions): Promise<void>\n}\n\nexport async function write(path: string, data: string, options: { sanity?: boolean } = {}): Promise<string | undefined> {\n if (data.trim() === '') {\n return undefined\n }\n return switcher(\n {\n node: async (path: string, data: string, { sanity }: { sanity?: boolean }) => {\n try {\n const oldContent = await fs.readFile(resolve(path), {\n encoding: 'utf-8',\n })\n if (oldContent?.toString() === data?.toString()) {\n return\n }\n } catch (_err) {\n /* empty */\n }\n\n await fs.outputFile(resolve(path), data, { encoding: 'utf-8' })\n\n if (sanity) {\n const savedData = await fs.readFile(resolve(path), {\n encoding: 'utf-8',\n })\n\n if (savedData?.toString() !== data?.toString()) {\n throw new Error(`Sanity check failed for ${path}\\n\\nData[${data.length}]:\\n${data}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n\n return savedData\n }\n\n return data\n },\n bun: async (path: string, data: string, { sanity }: { sanity?: boolean }) => {\n try {\n await Bun.write(resolve(path), data)\n\n if (sanity) {\n const file = Bun.file(resolve(path))\n const savedData = await file.text()\n\n if (savedData?.toString() !== data?.toString()) {\n throw new Error(`Sanity check failed for ${path}\\n\\nData[${path.length}]:\\n${path}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n\n return savedData\n }\n\n return data\n } catch (e) {\n console.error(e)\n }\n },\n },\n 'node',\n )(path, data.trim(), options)\n}\n\n// biome-ignore lint/suspicious/noTsIgnore: production ready\n// @ts-ignore\ndeclare module '@kubb/fabric-core' {\n interface App {\n write(options?: WriteOptions): Promise<void>\n }\n}\n\ndeclare global {\n namespace Kubb {\n interface App {\n write(options?: WriteOptions): Promise<void>\n }\n }\n}\n\nexport const fsPlugin = createPlugin<Options, ExtendOptions>({\n name: 'fs',\n install(app, options) {\n if (options?.clean) {\n fs.removeSync(options.clean.path)\n }\n\n app.context.events.on('process:progress', async ({ file, source }) => {\n if (options?.onWrite) {\n await options.onWrite(file.path, source)\n }\n await write(file.path, source, { sanity: false })\n })\n },\n inject(app) {\n return {\n async write(\n options = {\n extension: { '.ts': '.ts' },\n dryRun: false,\n },\n ) {\n await app.context.fileManager.write({\n extension: options.extension,\n dryRun: options.dryRun,\n parsers: app.context.installedParsers,\n })\n },\n }\n },\n})\n","import type * as KubbFile from '../KubbFile.ts'\n\ntype BarrelData = {\n file?: KubbFile.File\n path: string\n name: string\n}\n\nexport class TreeNode<TData = unknown> {\n data: TData\n parent?: TreeNode<TData>\n children: Array<TreeNode<TData>> = []\n #cachedLeaves?: Array<TreeNode<TData>>\n\n constructor(data: TData, parent?: TreeNode<TData>) {\n this.data = data\n this.parent = parent\n }\n\n addChild(data: TData): TreeNode<TData> {\n const child = new TreeNode(data, this)\n this.children.push(child)\n this.#cachedLeaves = undefined // invalidate cached leaves\n return child\n }\n\n get leaves(): Array<TreeNode<TData>> {\n if (this.#cachedLeaves) return this.#cachedLeaves\n if (this.children.length === 0) return [this]\n\n const stack: Array<TreeNode<TData>> = [...this.children]\n const result: Array<TreeNode<TData>> = []\n\n for (const node of stack) {\n if (node.children.length) {\n for (const child of node.children) stack.push(child)\n } else {\n result.push(node)\n }\n }\n\n this.#cachedLeaves = result\n return result\n }\n\n forEach(callback: (node: TreeNode<TData>) => void): this {\n const stack: Array<TreeNode<TData>> = [this]\n\n for (const node of stack) {\n callback(node)\n if (node.children.length) {\n for (const child of node.children) stack.push(child)\n }\n }\n return this\n }\n\n findDeep(predicate: (node: TreeNode<TData>) => boolean): TreeNode<TData> | undefined {\n for (const leaf of this.leaves) {\n if (predicate(leaf)) return leaf\n }\n return undefined\n }\n\n static fromFiles(files: Array<KubbFile.File>, rootFolder = ''): TreeNode<BarrelData> | null {\n const normalizePath = (p: string): string => p.replace(/\\\\/g, '/')\n const normalizedRoot = normalizePath(rootFolder)\n const rootPrefix = normalizedRoot.endsWith('/') ? normalizedRoot : `${normalizedRoot}/`\n\n const filteredFiles = files.filter((file) => {\n const filePath = normalizePath(file.path)\n return !filePath.endsWith('.json') && (!rootFolder || filePath.startsWith(rootPrefix))\n })\n\n if (filteredFiles.length === 0) {\n return null\n }\n\n const treeNode = new TreeNode<BarrelData>({\n name: rootFolder || '',\n path: rootFolder || '',\n file: undefined,\n })\n\n for (const file of filteredFiles) {\n const relPath = normalizePath(file.path).slice(rootPrefix.length)\n const parts = relPath.split('/')\n\n let current = treeNode\n let currentPath = rootFolder\n\n for (const [index, part] of parts.entries()) {\n const isLast = index === parts.length - 1\n currentPath += (currentPath.endsWith('/') ? '' : '/') + part\n\n let next: TreeNode<BarrelData> | undefined\n for (const child of current.children) {\n if ((child.data as BarrelData).name === part) {\n next = child\n break\n }\n }\n\n if (!next) {\n next = current.addChild({\n name: part,\n path: currentPath,\n file: isLast ? file : undefined,\n })\n }\n\n current = next\n }\n }\n\n return treeNode\n }\n}\n","/** biome-ignore-all lint/suspicious/useIterableCallbackReturn: not needed */\n\nimport { createPlugin } from './createPlugin.ts'\nimport type * as KubbFile from '../KubbFile.ts'\nimport { TreeNode } from '../utils/TreeNode.ts'\nimport path, { resolve } from 'node:path'\nimport { getRelativePath } from '../utils/getRelativePath.ts'\nimport { createFile } from '../createFile.ts'\n\ntype Mode = 'all' | 'named' | 'propagate' | false\n\ntype Options = {\n root: string\n mode: Mode\n}\n\ntype ExtendOptions = {\n writeEntry(options: Options): Promise<void>\n}\n\n// biome-ignore lint/suspicious/noTsIgnore: production ready\n// @ts-ignore\ndeclare module '@kubb/fabric-core' {\n interface App {\n writeEntry(options: Options): Promise<void>\n }\n}\n\ndeclare global {\n namespace Kubb {\n interface App {\n writeEntry(options: Options): Promise<void>\n }\n }\n}\n\ntype GetBarrelFilesOptions = {\n files: KubbFile.File[]\n root: string\n mode: Mode\n}\n\nexport function getBarrelFiles({ files, root, mode }: GetBarrelFilesOptions): Array<KubbFile.File> {\n // Do not generate when propagating or disabled\n if (mode === 'propagate' || mode === false) {\n return []\n }\n\n const cachedFiles = new Map<KubbFile.Path, KubbFile.File>()\n const dedupe = new Map<KubbFile.Path, Set<string>>()\n\n const tree = TreeNode.fromFiles(files, root)\n tree?.forEach((node) => {\n // Only create a barrel for directory-like nodes that have a parent with a path\n if (!node?.children || !node.parent?.data.path) {\n return\n }\n\n const parentPath = node.parent.data.path as KubbFile.Path\n const barrelPath = path.join(parentPath, 'index.ts') as KubbFile.Path\n\n let barrelFile = cachedFiles.get(barrelPath)\n if (!barrelFile) {\n barrelFile = createFile({\n path: barrelPath,\n baseName: 'index.ts',\n exports: [],\n sources: [],\n })\n cachedFiles.set(barrelPath, barrelFile)\n dedupe.set(barrelPath, new Set<string>())\n }\n\n const seen = dedupe.get(barrelPath)!\n\n // Collect all leaves under the current directory node\n node.leaves.forEach((leaf) => {\n const file = leaf.data.file\n if (!file) {\n return\n }\n\n const sources = file.sources || []\n sources.forEach((source) => {\n if (!file.path || !source.isIndexable || !source.name) {\n return\n }\n\n const key = `${source.name}|${source.isTypeOnly ? '1' : '0'}`\n if (seen.has(key)) {\n return\n }\n seen.add(key)\n\n // Always compute relative path from the parent directory to the file path\n barrelFile!.exports!.push({\n name: [source.name],\n path: getRelativePath(parentPath, file.path),\n isTypeOnly: source.isTypeOnly,\n })\n\n barrelFile!.sources.push({\n name: source.name,\n isTypeOnly: source.isTypeOnly,\n value: '', // TODO use parser to generate import\n isExportable: mode === 'all' || mode === 'named',\n isIndexable: mode === 'all' || mode === 'named',\n })\n })\n })\n })\n\n const result = [...cachedFiles.values()]\n\n if (mode === 'all') {\n return result.map((file) => ({\n ...file,\n exports: file.exports?.map((e) => ({ ...e, name: undefined })),\n }))\n }\n\n return result\n}\n\nexport const barrelPlugin = createPlugin<Options, ExtendOptions>({\n name: 'barrel',\n install(app, options) {\n if (!options) {\n throw new Error('Barrel plugin requires options.root and options.mode')\n }\n\n if (!options.mode) {\n return undefined\n }\n\n app.context.events.onOnce('process:end', async ({ files }) => {\n const root = options.root\n const barrelFiles = getBarrelFiles({ files, root, mode: options.mode })\n\n await app.context.fileManager.add(...barrelFiles)\n\n await app.context.fileManager.write({\n parsers: app.context.installedParsers,\n })\n })\n },\n inject(app) {\n return {\n async writeEntry({ root, mode }) {\n if (!mode || mode === 'propagate') {\n return undefined\n }\n\n const rootPath = resolve(root, 'index.ts')\n\n const barrelFiles = app.files.filter((file) => {\n return file.sources.some((source) => source.isIndexable)\n })\n\n const rootFile = createFile({\n path: rootPath,\n baseName: 'index.ts',\n exports: barrelFiles\n .flatMap((file) => {\n const containsOnlyTypes = file.sources.every((source) => source.isTypeOnly)\n\n return file.sources\n ?.map((source) => {\n if (!file.path || !source.isIndexable) {\n return undefined\n }\n\n return {\n name: mode === 'all' ? undefined : [source.name],\n path: getRelativePath(rootPath, file.path),\n isTypeOnly: mode === 'all' ? containsOnlyTypes : source.isTypeOnly,\n } as KubbFile.Export\n })\n .filter(Boolean)\n })\n .filter(Boolean),\n sources: [],\n })\n\n await app.context.fileManager.add(rootFile)\n },\n }\n },\n})\n"],"mappings":";;;;;;;;;;;AAEA,SAAgB,aACd,QACgC;AAChC,QAAO;EACL,MAAM;EACN,GAAG;EACJ;;;;;ACkBH,eAAsB,MAAM,QAAc,MAAc,UAAgC,EAAE,EAA+B;AACvH,KAAI,KAAK,MAAM,KAAK,GAClB;AAEF,iCACE;EACE,MAAM,OAAO,QAAc,QAAc,EAAE,aAAmC;AAC5E,OAAI;IACF,MAAM,aAAa,MAAMA,iBAAG,gCAAiBC,OAAK,EAAE,EAClD,UAAU,SACX,CAAC;AACF,iEAAI,WAA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;AAGF,MAAI,QAAQ,OAAO,OAAO,eAAe,OAAO,EAAE,YAAY;GAC5D,MAAM,OAAO,QAAQ;GACrB,MAAM,cAAc,eAAe;IAAE;IAAO;IAAM,MAAM,QAAQ;IAAM,CAAC;AAEvE,SAAM,IAAI,QAAQ,YAAY,IAAI,GAAG,YAAY;AAEjD,SAAM,IAAI,QAAQ,YAAY,MAAM,EAClC,SAAS,IAAI,QAAQ,kBACtB,CAAC;IACF;;CAEJ,OAAO,KAAK;AACV,SAAO,EACL,MAAM,WAAW,EAAE,MAAM,QAAQ;AAC/B,OAAI,CAAC,QAAQ,SAAS,YACpB;GAGF,MAAM,kCAAmB,MAAM,WAAW;GAM1C,MAAM,WAAWD,kCAAW;IAC1B,MAAM;IACN,UAAU;IACV,SAPkB,IAAI,MAAM,QAAQ,SAAS;AAC7C,YAAO,KAAK,QAAQ,MAAM,WAAW,OAAO,YAAY;MACxD,CAMG,SAAS,SAAS;;KACjB,MAAM,oBAAoB,KAAK,QAAQ,OAAO,WAAW,OAAO,WAAW;AAE3E,6BAAO,KAAK,uEACR,KAAK,WAAW;AAChB,UAAI,CAAC,KAAK,QAAQ,CAAC,OAAO,YACxB;AAGF,aAAO;OACL,MAAM,SAAS,QAAQ,SAAY,CAAC,OAAO,KAAK;OAChD,MAAMC,wCAAgB,UAAU,KAAK,KAAK;OAC1C,YAAY,SAAS,QAAQ,oBAAoB,OAAO;OACzD;OACD,CACD,OAAO,QAAQ;MAClB,CACD,OAAO,QAAQ;IAClB,SAAS,EAAE;IACZ,CAAC;AAEF,SAAM,IAAI,QAAQ,YAAY,IAAI,SAAS;KAE9C;;CAEJ,CAAC"}
@@ -1,29 +1,62 @@
1
- import { c as Plugin, l as UserPlugin, u as Extname } from "./App-D3DHa4Il.cjs";
1
+ import { c as Plugin, l as UserPlugin, u as Extname } from "./App-Cplfh8QA.cjs";
2
2
 
3
3
  //#region src/plugins/createPlugin.d.ts
4
- declare function createPlugin<Options$1 = any[], TAppExtension extends Record<string, any> = {}>(plugin: UserPlugin<Options$1, TAppExtension>): Plugin<Options$1, TAppExtension>;
4
+ declare function createPlugin<Options$2 = unknown, TAppExtension extends Record<string, any> = {}>(plugin: UserPlugin<Options$2, TAppExtension>): Plugin<Options$2, TAppExtension>;
5
5
  //#endregion
6
6
  //#region src/plugins/fsPlugin.d.ts
7
7
  type WriteOptions = {
8
8
  extension?: Record<Extname, Extname | ''>;
9
9
  dryRun?: boolean;
10
10
  };
11
- type Options = {
11
+ type Options$1 = {
12
12
  /**
13
13
  * Optional callback that is invoked whenever a file is written by the plugin.
14
14
  * Useful for tests to observe write operations without spying on internal functions.
15
15
  */
16
16
  onWrite?: (path: string, data: string) => void | Promise<void>;
17
+ clean?: {
18
+ path: string;
19
+ };
17
20
  };
18
- type ExtendOptions = {
21
+ type ExtendOptions$1 = {
19
22
  write(options?: WriteOptions): Promise<void>;
20
23
  };
21
- declare module '../index.ts' {
24
+ declare module '@kubb/fabric-core' {
22
25
  interface App {
23
26
  write(options?: WriteOptions): Promise<void>;
24
27
  }
25
28
  }
26
- declare const fsPlugin: Plugin<Options, ExtendOptions>;
29
+ declare global {
30
+ namespace Kubb {
31
+ interface App {
32
+ write(options?: WriteOptions): Promise<void>;
33
+ }
34
+ }
35
+ }
36
+ declare const fsPlugin: Plugin<Options$1, ExtendOptions$1>;
37
+ //#endregion
38
+ //#region src/plugins/barrelPlugin.d.ts
39
+ type Mode = 'all' | 'named' | 'propagate' | false;
40
+ type Options = {
41
+ root: string;
42
+ mode: Mode;
43
+ };
44
+ type ExtendOptions = {
45
+ writeEntry(options: Options): Promise<void>;
46
+ };
47
+ declare module '@kubb/fabric-core' {
48
+ interface App {
49
+ writeEntry(options: Options): Promise<void>;
50
+ }
51
+ }
52
+ declare global {
53
+ namespace Kubb {
54
+ interface App {
55
+ writeEntry(options: Options): Promise<void>;
56
+ }
57
+ }
58
+ }
59
+ declare const barrelPlugin: Plugin<Options, ExtendOptions>;
27
60
  //#endregion
28
- export { createPlugin, fsPlugin };
61
+ export { barrelPlugin, createPlugin, fsPlugin };
29
62
  //# sourceMappingURL=plugins.d.cts.map
package/dist/plugins.d.ts CHANGED
@@ -1,29 +1,62 @@
1
- import { c as Plugin, l as UserPlugin, u as Extname } from "./App-BxAl3dNP.js";
1
+ import { c as Plugin, l as UserPlugin, u as Extname } from "./App-Dvetv2V_.js";
2
2
 
3
3
  //#region src/plugins/createPlugin.d.ts
4
- declare function createPlugin<Options$1 = any[], TAppExtension extends Record<string, any> = {}>(plugin: UserPlugin<Options$1, TAppExtension>): Plugin<Options$1, TAppExtension>;
4
+ declare function createPlugin<Options$2 = unknown, TAppExtension extends Record<string, any> = {}>(plugin: UserPlugin<Options$2, TAppExtension>): Plugin<Options$2, TAppExtension>;
5
5
  //#endregion
6
6
  //#region src/plugins/fsPlugin.d.ts
7
7
  type WriteOptions = {
8
8
  extension?: Record<Extname, Extname | ''>;
9
9
  dryRun?: boolean;
10
10
  };
11
- type Options = {
11
+ type Options$1 = {
12
12
  /**
13
13
  * Optional callback that is invoked whenever a file is written by the plugin.
14
14
  * Useful for tests to observe write operations without spying on internal functions.
15
15
  */
16
16
  onWrite?: (path: string, data: string) => void | Promise<void>;
17
+ clean?: {
18
+ path: string;
19
+ };
17
20
  };
18
- type ExtendOptions = {
21
+ type ExtendOptions$1 = {
19
22
  write(options?: WriteOptions): Promise<void>;
20
23
  };
21
- declare module '../index.ts' {
24
+ declare module '@kubb/fabric-core' {
22
25
  interface App {
23
26
  write(options?: WriteOptions): Promise<void>;
24
27
  }
25
28
  }
26
- declare const fsPlugin: Plugin<Options, ExtendOptions>;
29
+ declare global {
30
+ namespace Kubb {
31
+ interface App {
32
+ write(options?: WriteOptions): Promise<void>;
33
+ }
34
+ }
35
+ }
36
+ declare const fsPlugin: Plugin<Options$1, ExtendOptions$1>;
37
+ //#endregion
38
+ //#region src/plugins/barrelPlugin.d.ts
39
+ type Mode = 'all' | 'named' | 'propagate' | false;
40
+ type Options = {
41
+ root: string;
42
+ mode: Mode;
43
+ };
44
+ type ExtendOptions = {
45
+ writeEntry(options: Options): Promise<void>;
46
+ };
47
+ declare module '@kubb/fabric-core' {
48
+ interface App {
49
+ writeEntry(options: Options): Promise<void>;
50
+ }
51
+ }
52
+ declare global {
53
+ namespace Kubb {
54
+ interface App {
55
+ writeEntry(options: Options): Promise<void>;
56
+ }
57
+ }
58
+ }
59
+ declare const barrelPlugin: Plugin<Options, ExtendOptions>;
27
60
  //#endregion
28
- export { createPlugin, fsPlugin };
61
+ export { barrelPlugin, createPlugin, fsPlugin };
29
62
  //# sourceMappingURL=plugins.d.ts.map
package/dist/plugins.js CHANGED
@@ -1,4 +1,7 @@
1
- import { resolve } from "node:path";
1
+ import { a as _classPrivateFieldInitSpec, i as _assertClassBrand, n as createFile, r as _classPrivateFieldGet2, t as _defineProperty } from "./defineProperty-3OJdpith.js";
2
+ import "./trimExtName-CeOVQVbu.js";
3
+ import { t as getRelativePath } from "./getRelativePath-CERJmYkp.js";
4
+ import path, { resolve } from "node:path";
2
5
  import { switcher } from "js-runtime";
3
6
  import fs from "fs-extra";
4
7
 
@@ -45,8 +48,8 @@ async function write(path$1, data, options = {}) {
45
48
  }
46
49
  const fsPlugin = createPlugin({
47
50
  name: "fs",
48
- scope: "write",
49
- async install(app, options) {
51
+ install(app, options) {
52
+ if (options === null || options === void 0 ? void 0 : options.clean) fs.removeSync(options.clean.path);
50
53
  app.context.events.on("process:progress", async ({ file, source }) => {
51
54
  if (options === null || options === void 0 ? void 0 : options.onWrite) await options.onWrite(file.path, source);
52
55
  await write(file.path, source, { sanity: false });
@@ -67,5 +70,192 @@ const fsPlugin = createPlugin({
67
70
  });
68
71
 
69
72
  //#endregion
70
- export { createPlugin, fsPlugin };
73
+ //#region \0@oxc-project+runtime@0.95.0/helpers/classPrivateFieldSet2.js
74
+ function _classPrivateFieldSet2(s, a, r) {
75
+ return s.set(_assertClassBrand(s, a), r), r;
76
+ }
77
+
78
+ //#endregion
79
+ //#region src/utils/TreeNode.ts
80
+ var _cachedLeaves = /* @__PURE__ */ new WeakMap();
81
+ var TreeNode = class TreeNode {
82
+ constructor(data, parent) {
83
+ _defineProperty(this, "data", void 0);
84
+ _defineProperty(this, "parent", void 0);
85
+ _defineProperty(this, "children", []);
86
+ _classPrivateFieldInitSpec(this, _cachedLeaves, void 0);
87
+ this.data = data;
88
+ this.parent = parent;
89
+ }
90
+ addChild(data) {
91
+ const child = new TreeNode(data, this);
92
+ this.children.push(child);
93
+ _classPrivateFieldSet2(_cachedLeaves, this, void 0);
94
+ return child;
95
+ }
96
+ get leaves() {
97
+ if (_classPrivateFieldGet2(_cachedLeaves, this)) return _classPrivateFieldGet2(_cachedLeaves, this);
98
+ if (this.children.length === 0) return [this];
99
+ const stack = [...this.children];
100
+ const result = [];
101
+ for (const node of stack) if (node.children.length) for (const child of node.children) stack.push(child);
102
+ else result.push(node);
103
+ _classPrivateFieldSet2(_cachedLeaves, this, result);
104
+ return result;
105
+ }
106
+ forEach(callback) {
107
+ const stack = [this];
108
+ for (const node of stack) {
109
+ callback(node);
110
+ if (node.children.length) for (const child of node.children) stack.push(child);
111
+ }
112
+ return this;
113
+ }
114
+ findDeep(predicate) {
115
+ for (const leaf of this.leaves) if (predicate(leaf)) return leaf;
116
+ }
117
+ static fromFiles(files, rootFolder = "") {
118
+ const normalizePath = (p) => p.replace(/\\/g, "/");
119
+ const normalizedRoot = normalizePath(rootFolder);
120
+ const rootPrefix = normalizedRoot.endsWith("/") ? normalizedRoot : `${normalizedRoot}/`;
121
+ const filteredFiles = files.filter((file) => {
122
+ const filePath = normalizePath(file.path);
123
+ return !filePath.endsWith(".json") && (!rootFolder || filePath.startsWith(rootPrefix));
124
+ });
125
+ if (filteredFiles.length === 0) return null;
126
+ const treeNode = new TreeNode({
127
+ name: rootFolder || "",
128
+ path: rootFolder || "",
129
+ file: void 0
130
+ });
131
+ for (const file of filteredFiles) {
132
+ const parts = normalizePath(file.path).slice(rootPrefix.length).split("/");
133
+ let current = treeNode;
134
+ let currentPath = rootFolder;
135
+ for (const [index, part] of parts.entries()) {
136
+ const isLast = index === parts.length - 1;
137
+ currentPath += (currentPath.endsWith("/") ? "" : "/") + part;
138
+ let next;
139
+ for (const child of current.children) if (child.data.name === part) {
140
+ next = child;
141
+ break;
142
+ }
143
+ if (!next) next = current.addChild({
144
+ name: part,
145
+ path: currentPath,
146
+ file: isLast ? file : void 0
147
+ });
148
+ current = next;
149
+ }
150
+ }
151
+ return treeNode;
152
+ }
153
+ };
154
+
155
+ //#endregion
156
+ //#region src/plugins/barrelPlugin.ts
157
+ function getBarrelFiles({ files, root, mode }) {
158
+ if (mode === "propagate" || mode === false) return [];
159
+ const cachedFiles = /* @__PURE__ */ new Map();
160
+ const dedupe = /* @__PURE__ */ new Map();
161
+ const tree = TreeNode.fromFiles(files, root);
162
+ tree === null || tree === void 0 || tree.forEach((node) => {
163
+ var _node$parent;
164
+ if (!(node === null || node === void 0 ? void 0 : node.children) || !((_node$parent = node.parent) === null || _node$parent === void 0 ? void 0 : _node$parent.data.path)) return;
165
+ const parentPath = node.parent.data.path;
166
+ const barrelPath = path.join(parentPath, "index.ts");
167
+ let barrelFile = cachedFiles.get(barrelPath);
168
+ if (!barrelFile) {
169
+ barrelFile = createFile({
170
+ path: barrelPath,
171
+ baseName: "index.ts",
172
+ exports: [],
173
+ sources: []
174
+ });
175
+ cachedFiles.set(barrelPath, barrelFile);
176
+ dedupe.set(barrelPath, /* @__PURE__ */ new Set());
177
+ }
178
+ const seen = dedupe.get(barrelPath);
179
+ node.leaves.forEach((leaf) => {
180
+ const file = leaf.data.file;
181
+ if (!file) return;
182
+ (file.sources || []).forEach((source) => {
183
+ if (!file.path || !source.isIndexable || !source.name) return;
184
+ const key = `${source.name}|${source.isTypeOnly ? "1" : "0"}`;
185
+ if (seen.has(key)) return;
186
+ seen.add(key);
187
+ barrelFile.exports.push({
188
+ name: [source.name],
189
+ path: getRelativePath(parentPath, file.path),
190
+ isTypeOnly: source.isTypeOnly
191
+ });
192
+ barrelFile.sources.push({
193
+ name: source.name,
194
+ isTypeOnly: source.isTypeOnly,
195
+ value: "",
196
+ isExportable: mode === "all" || mode === "named",
197
+ isIndexable: mode === "all" || mode === "named"
198
+ });
199
+ });
200
+ });
201
+ });
202
+ const result = [...cachedFiles.values()];
203
+ if (mode === "all") return result.map((file) => {
204
+ var _file$exports;
205
+ return {
206
+ ...file,
207
+ exports: (_file$exports = file.exports) === null || _file$exports === void 0 ? void 0 : _file$exports.map((e) => ({
208
+ ...e,
209
+ name: void 0
210
+ }))
211
+ };
212
+ });
213
+ return result;
214
+ }
215
+ const barrelPlugin = createPlugin({
216
+ name: "barrel",
217
+ install(app, options) {
218
+ if (!options) throw new Error("Barrel plugin requires options.root and options.mode");
219
+ if (!options.mode) return;
220
+ app.context.events.onOnce("process:end", async ({ files }) => {
221
+ const root = options.root;
222
+ const barrelFiles = getBarrelFiles({
223
+ files,
224
+ root,
225
+ mode: options.mode
226
+ });
227
+ await app.context.fileManager.add(...barrelFiles);
228
+ await app.context.fileManager.write({ parsers: app.context.installedParsers });
229
+ });
230
+ },
231
+ inject(app) {
232
+ return { async writeEntry({ root, mode }) {
233
+ if (!mode || mode === "propagate") return;
234
+ const rootPath = resolve(root, "index.ts");
235
+ const rootFile = createFile({
236
+ path: rootPath,
237
+ baseName: "index.ts",
238
+ exports: app.files.filter((file) => {
239
+ return file.sources.some((source) => source.isIndexable);
240
+ }).flatMap((file) => {
241
+ var _file$sources;
242
+ const containsOnlyTypes = file.sources.every((source) => source.isTypeOnly);
243
+ return (_file$sources = file.sources) === null || _file$sources === void 0 ? void 0 : _file$sources.map((source) => {
244
+ if (!file.path || !source.isIndexable) return;
245
+ return {
246
+ name: mode === "all" ? void 0 : [source.name],
247
+ path: getRelativePath(rootPath, file.path),
248
+ isTypeOnly: mode === "all" ? containsOnlyTypes : source.isTypeOnly
249
+ };
250
+ }).filter(Boolean);
251
+ }).filter(Boolean),
252
+ sources: []
253
+ });
254
+ await app.context.fileManager.add(rootFile);
255
+ } };
256
+ }
257
+ });
258
+
259
+ //#endregion
260
+ export { barrelPlugin, createPlugin, fsPlugin };
71
261
  //# sourceMappingURL=plugins.js.map