@kubb/fabric-core 0.0.0-canary-20251024103955 → 0.0.0-canary-20251024144419
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.
- package/README.md +276 -0
- package/dist/{App-_vPNh477.d.ts → App-Cjd-lGfW.d.cts} +13 -1
- package/dist/{App-DVWD6TgC.d.cts → App-ztRQpZS9.d.ts} +13 -1
- package/dist/{index-DVok6g82.d.cts → index-BUERYeq7.d.cts} +2 -2
- package/dist/{index-CfV-59_M.d.ts → index-DfaD7Pj_.d.ts} +2 -2
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/parsers/typescript.d.cts +2 -2
- package/dist/parsers/typescript.d.ts +2 -2
- package/dist/parsers.d.cts +2 -2
- package/dist/parsers.d.ts +2 -2
- package/dist/plugins.cjs +133 -23
- package/dist/plugins.cjs.map +1 -1
- package/dist/plugins.d.cts +13 -12
- package/dist/plugins.d.ts +13 -12
- package/dist/plugins.js +120 -14
- package/dist/plugins.js.map +1 -1
- package/dist/types.d.cts +2 -2
- package/dist/types.d.ts +2 -2
- package/dist/{typescriptParser-BM90H8Tx.d.cts → typescriptParser-CBt00C7L.d.cts} +2 -2
- package/dist/{typescriptParser-Chjs-RhT.d.ts → typescriptParser-DDr_EXfe.d.ts} +2 -2
- package/package.json +5 -2
- package/src/App.ts +6 -0
- package/src/FileManager.ts +7 -0
- package/src/plugins/barrelPlugin.ts +10 -11
- package/src/plugins/fsPlugin.ts +1 -1
- package/src/plugins/graphPlugin.ts +133 -0
- package/src/plugins/index.ts +1 -0
- package/src/plugins/progressPlugin.ts +1 -15
- package/src/utils/TreeNode.ts +27 -0
package/dist/plugins.cjs
CHANGED
|
@@ -11,6 +11,12 @@ let cli_progress = require("cli-progress");
|
|
|
11
11
|
cli_progress = require_trimExtName.__toESM(cli_progress);
|
|
12
12
|
let node_process = require("node:process");
|
|
13
13
|
node_process = require_trimExtName.__toESM(node_process);
|
|
14
|
+
let node_http = require("node:http");
|
|
15
|
+
node_http = require_trimExtName.__toESM(node_http);
|
|
16
|
+
let tiny_open = require("tiny-open");
|
|
17
|
+
tiny_open = require_trimExtName.__toESM(tiny_open);
|
|
18
|
+
let serve_handler = require("serve-handler");
|
|
19
|
+
serve_handler = require_trimExtName.__toESM(serve_handler);
|
|
14
20
|
|
|
15
21
|
//#region src/plugins/createPlugin.ts
|
|
16
22
|
function createPlugin(plugin) {
|
|
@@ -22,33 +28,33 @@ function createPlugin(plugin) {
|
|
|
22
28
|
|
|
23
29
|
//#endregion
|
|
24
30
|
//#region src/plugins/fsPlugin.ts
|
|
25
|
-
async function write(path$
|
|
31
|
+
async function write(path$2, data, options = {}) {
|
|
26
32
|
return (0, js_runtime.switcher)({
|
|
27
|
-
node: async (path$
|
|
33
|
+
node: async (path$3, data$1, { sanity }) => {
|
|
28
34
|
if (!data$1 || (data$1 === null || data$1 === void 0 ? void 0 : data$1.trim()) === "") return;
|
|
29
35
|
try {
|
|
30
|
-
const oldContent = await fs_extra.default.readFile((0, node_path.resolve)(path$
|
|
36
|
+
const oldContent = await fs_extra.default.readFile((0, node_path.resolve)(path$3), { encoding: "utf-8" });
|
|
31
37
|
if ((oldContent === null || oldContent === void 0 ? void 0 : oldContent.toString()) === (data$1 === null || data$1 === void 0 ? void 0 : data$1.toString())) return;
|
|
32
38
|
} catch (_err) {}
|
|
33
|
-
await fs_extra.default.outputFile((0, node_path.resolve)(path$
|
|
39
|
+
await fs_extra.default.outputFile((0, node_path.resolve)(path$3), data$1.trim(), { encoding: "utf-8" });
|
|
34
40
|
if (sanity) {
|
|
35
|
-
const savedData = await fs_extra.default.readFile((0, node_path.resolve)(path$
|
|
36
|
-
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$
|
|
41
|
+
const savedData = await fs_extra.default.readFile((0, node_path.resolve)(path$3), { encoding: "utf-8" });
|
|
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$3}\n\nData[${data$1.length}]:\n${data$1}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
|
|
37
43
|
return savedData;
|
|
38
44
|
}
|
|
39
45
|
return data$1;
|
|
40
46
|
},
|
|
41
|
-
bun: async (path$
|
|
47
|
+
bun: async (path$3, data$1, { sanity }) => {
|
|
42
48
|
if (!data$1 || (data$1 === null || data$1 === void 0 ? void 0 : data$1.trim()) === "") return;
|
|
43
|
-
await Bun.write((0, node_path.resolve)(path$
|
|
49
|
+
await Bun.write((0, node_path.resolve)(path$3), data$1.trim());
|
|
44
50
|
if (sanity) {
|
|
45
|
-
const savedData = await Bun.file((0, node_path.resolve)(path$
|
|
46
|
-
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$
|
|
51
|
+
const savedData = await Bun.file((0, node_path.resolve)(path$3)).text();
|
|
52
|
+
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$3}\n\nData[${data$1.length}]:\n${data$1}\n\nSaved[${savedData.length}]:\n${savedData}\n`);
|
|
47
53
|
return savedData;
|
|
48
54
|
}
|
|
49
55
|
return data$1;
|
|
50
56
|
}
|
|
51
|
-
}, "node")(path$
|
|
57
|
+
}, "node")(path$2, data, options);
|
|
52
58
|
}
|
|
53
59
|
const fsPlugin = createPlugin({
|
|
54
60
|
name: "fs",
|
|
@@ -117,6 +123,24 @@ var TreeNode = class TreeNode {
|
|
|
117
123
|
findDeep(predicate) {
|
|
118
124
|
for (const leaf of this.leaves) if (predicate(leaf)) return leaf;
|
|
119
125
|
}
|
|
126
|
+
static toGraph(root) {
|
|
127
|
+
const nodes = [];
|
|
128
|
+
const edges = [];
|
|
129
|
+
root.forEach((node) => {
|
|
130
|
+
nodes.push({
|
|
131
|
+
id: node.data.path,
|
|
132
|
+
label: node.data.name
|
|
133
|
+
});
|
|
134
|
+
for (const child of node.children) edges.push({
|
|
135
|
+
from: node.data.path,
|
|
136
|
+
to: child.data.path
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
return {
|
|
140
|
+
nodes,
|
|
141
|
+
edges
|
|
142
|
+
};
|
|
143
|
+
}
|
|
120
144
|
static fromFiles(files, rootFolder = "") {
|
|
121
145
|
const normalizePath = (p) => p.replace(/\\/g, "/");
|
|
122
146
|
const normalizedRoot = normalizePath(rootFolder);
|
|
@@ -161,8 +185,9 @@ function getBarrelFiles({ files, root, mode }) {
|
|
|
161
185
|
if (mode === "propagate" || mode === false) return [];
|
|
162
186
|
const cachedFiles = /* @__PURE__ */ new Map();
|
|
163
187
|
const dedupe = /* @__PURE__ */ new Map();
|
|
164
|
-
const
|
|
165
|
-
|
|
188
|
+
const treeNode = TreeNode.fromFiles(files, root);
|
|
189
|
+
if (!treeNode) return [];
|
|
190
|
+
treeNode.forEach((node) => {
|
|
166
191
|
var _node$parent;
|
|
167
192
|
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
193
|
const parentPath = node.parent.data.path;
|
|
@@ -220,8 +245,7 @@ const barrelPlugin = createPlugin({
|
|
|
220
245
|
install(app, options) {
|
|
221
246
|
if (!options) throw new Error("Barrel plugin requires options.root and options.mode");
|
|
222
247
|
if (!options.mode) return;
|
|
223
|
-
app.context.events.
|
|
224
|
-
var _app$context$options;
|
|
248
|
+
app.context.events.on("write:start", async ({ files }) => {
|
|
225
249
|
const root = options.root;
|
|
226
250
|
const barrelFiles = getBarrelFiles({
|
|
227
251
|
files,
|
|
@@ -229,19 +253,14 @@ const barrelPlugin = createPlugin({
|
|
|
229
253
|
mode: options.mode
|
|
230
254
|
});
|
|
231
255
|
await app.context.fileManager.add(...barrelFiles);
|
|
232
|
-
await app.context.fileManager.write({
|
|
233
|
-
mode: (_app$context$options = app.context.options) === null || _app$context$options === void 0 ? void 0 : _app$context$options.mode,
|
|
234
|
-
dryRun: options.dryRun,
|
|
235
|
-
parsers: app.context.installedParsers
|
|
236
|
-
});
|
|
237
256
|
});
|
|
238
257
|
},
|
|
239
258
|
inject(app, options) {
|
|
240
259
|
if (!options) throw new Error("Barrel plugin requires options.root and options.mode");
|
|
241
260
|
return { async writeEntry({ root, mode }) {
|
|
242
|
-
var _app$context$
|
|
261
|
+
var _app$context$options;
|
|
243
262
|
if (!mode || mode === "propagate") return;
|
|
244
|
-
const rootPath =
|
|
263
|
+
const rootPath = node_path.default.resolve(root, "index.ts");
|
|
245
264
|
const entryFile = require_defineProperty.createFile({
|
|
246
265
|
path: rootPath,
|
|
247
266
|
baseName: "index.ts",
|
|
@@ -263,7 +282,7 @@ const barrelPlugin = createPlugin({
|
|
|
263
282
|
});
|
|
264
283
|
await app.context.fileManager.add(entryFile);
|
|
265
284
|
await app.context.fileManager.write({
|
|
266
|
-
mode: (_app$context$
|
|
285
|
+
mode: (_app$context$options = app.context.options) === null || _app$context$options === void 0 ? void 0 : _app$context$options.mode,
|
|
267
286
|
dryRun: options.dryRun,
|
|
268
287
|
parsers: app.context.installedParsers
|
|
269
288
|
});
|
|
@@ -297,9 +316,100 @@ const progressPlugin = createPlugin({
|
|
|
297
316
|
}
|
|
298
317
|
});
|
|
299
318
|
|
|
319
|
+
//#endregion
|
|
320
|
+
//#region src/plugins/graphPlugin.ts
|
|
321
|
+
function getGraph({ files, root }) {
|
|
322
|
+
const treeNode = TreeNode.fromFiles(files, root);
|
|
323
|
+
if (!treeNode) return;
|
|
324
|
+
return TreeNode.toGraph(treeNode);
|
|
325
|
+
}
|
|
326
|
+
const html = `
|
|
327
|
+
<!DOCTYPE html>
|
|
328
|
+
<html lang="en">
|
|
329
|
+
<head>
|
|
330
|
+
<meta charset="UTF-8" />
|
|
331
|
+
<title>File Graph</title>
|
|
332
|
+
<script type="module">
|
|
333
|
+
import { Network } from 'https://cdn.jsdelivr.net/npm/vis-network/standalone/esm/vis-network.min.js'
|
|
334
|
+
|
|
335
|
+
async function main() {
|
|
336
|
+
const res = await fetch('./graph.json')
|
|
337
|
+
const { nodes, edges } = await res.json()
|
|
338
|
+
const container = document.getElementById('graph')
|
|
339
|
+
|
|
340
|
+
const network = new Network(
|
|
341
|
+
container,
|
|
342
|
+
{ nodes, edges },
|
|
343
|
+
{
|
|
344
|
+
layout: { hierarchical: { direction: 'UD', sortMethod: 'directed' } },
|
|
345
|
+
nodes: { shape: 'box', font: { face: 'monospace' } },
|
|
346
|
+
edges: { arrows: 'to' },
|
|
347
|
+
physics: false,
|
|
348
|
+
},
|
|
349
|
+
)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
main()
|
|
353
|
+
<\/script>
|
|
354
|
+
<style>
|
|
355
|
+
html, body, #graph { height: 100%; margin: 0; }
|
|
356
|
+
</style>
|
|
357
|
+
</head>
|
|
358
|
+
<body>
|
|
359
|
+
<div id="graph"></div>
|
|
360
|
+
</body>
|
|
361
|
+
</html>
|
|
362
|
+
`;
|
|
363
|
+
async function serve(root) {
|
|
364
|
+
const server = node_http.default.createServer((req, res) => {
|
|
365
|
+
return (0, serve_handler.default)(req, res, {
|
|
366
|
+
public: root,
|
|
367
|
+
cleanUrls: true
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
server.listen(0, async () => {
|
|
371
|
+
const { port } = server.address();
|
|
372
|
+
console.log(`Running on http://localhost:${port}/graph.html`);
|
|
373
|
+
await (0, tiny_open.default)(`http://localhost:${port}/graph.html`);
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
const graphPlugin = createPlugin({
|
|
377
|
+
name: "graph",
|
|
378
|
+
install(app, options) {
|
|
379
|
+
if (!options) throw new Error("Graph plugin requires options.root and options.mode");
|
|
380
|
+
app.context.events.on("write:start", async ({ files }) => {
|
|
381
|
+
const root = options.root;
|
|
382
|
+
const graph = getGraph({
|
|
383
|
+
files,
|
|
384
|
+
root
|
|
385
|
+
});
|
|
386
|
+
if (!graph) return;
|
|
387
|
+
const graphFile = require_defineProperty.createFile({
|
|
388
|
+
baseName: "graph.json",
|
|
389
|
+
path: node_path.default.join(root, "graph.json"),
|
|
390
|
+
sources: [{
|
|
391
|
+
name: "graph",
|
|
392
|
+
value: JSON.stringify(graph, null, 2)
|
|
393
|
+
}]
|
|
394
|
+
});
|
|
395
|
+
const graphHtmlFile = require_defineProperty.createFile({
|
|
396
|
+
baseName: "graph.html",
|
|
397
|
+
path: node_path.default.join(root, "graph.html"),
|
|
398
|
+
sources: [{
|
|
399
|
+
name: "graph",
|
|
400
|
+
value: html
|
|
401
|
+
}]
|
|
402
|
+
});
|
|
403
|
+
await app.context.fileManager.add(graphFile, graphHtmlFile);
|
|
404
|
+
if (options.open) await serve(root);
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
|
|
300
409
|
//#endregion
|
|
301
410
|
exports.barrelPlugin = barrelPlugin;
|
|
302
411
|
exports.createPlugin = createPlugin;
|
|
303
412
|
exports.fsPlugin = fsPlugin;
|
|
413
|
+
exports.graphPlugin = graphPlugin;
|
|
304
414
|
exports.progressPlugin = progressPlugin;
|
|
305
415
|
//# sourceMappingURL=plugins.cjs.map
|
package/dist/plugins.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugins.cjs","names":["data","fs","path","stack: Array<TreeNode<TData>>","result: Array<TreeNode<TData>>","next: TreeNode<BarrelData> | undefined","path","createFile","getRelativePath","SingleBar","Presets","process"],"sources":["../src/plugins/createPlugin.ts","../src/plugins/fsPlugin.ts","../src/utils/TreeNode.ts","../src/plugins/barrelPlugin.ts","../src/plugins/progressPlugin.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}\n\ntype Options = {\n dryRun?: boolean\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 onBeforeWrite?: (path: string, data: string | undefined) => 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 | undefined, options: { sanity?: boolean } = {}): Promise<string | undefined> {\n return switcher(\n {\n node: async (path, data: string | undefined, { sanity }: { sanity?: boolean }) => {\n if (!data || data?.trim() === '') {\n return undefined\n }\n\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.trim(), { 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 | undefined, { sanity }: { sanity?: boolean }) => {\n if (!data || data?.trim() === '') {\n return undefined\n }\n\n await Bun.write(resolve(path), data.trim())\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 },\n },\n 'node',\n )(path, data, 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.onBeforeWrite) {\n await options.onBeforeWrite(file.path, source)\n }\n await write(file.path, source, { sanity: false })\n })\n },\n inject(app, { dryRun } = {}) {\n return {\n async write(\n options = {\n extension: { '.ts': '.ts' },\n },\n ) {\n await app.context.fileManager.write({\n mode: app.context.options?.mode,\n extension: options.extension,\n 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 dryRun?: boolean\n}\n\ntype WriteEntryOptions = {\n root: string\n mode: Mode\n}\n\ntype ExtendOptions = {\n writeEntry(options: WriteEntryOptions): 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: WriteEntryOptions): Promise<void>\n }\n}\n\ndeclare global {\n namespace Kubb {\n interface App {\n writeEntry(options: WriteEntryOptions): 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 mode: app.context.options?.mode,\n dryRun: options.dryRun,\n parsers: app.context.installedParsers,\n })\n })\n },\n inject(app, options) {\n if (!options) {\n throw new Error('Barrel plugin requires options.root and options.mode')\n }\n\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 entryFile = 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(entryFile)\n\n await app.context.fileManager.write({\n mode: app.context.options?.mode,\n dryRun: options.dryRun,\n parsers: app.context.installedParsers,\n })\n },\n }\n },\n})\n","import { Presets, SingleBar } from 'cli-progress'\nimport { createPlugin } from './createPlugin.ts'\nimport { relative } from 'node:path'\nimport process from 'node:process'\n\ntype Options = {}\n\n// biome-ignore lint/suspicious/noTsIgnore: production ready\n// @ts-ignore\ndeclare module '@kubb/fabric-core' {\n interface App {}\n}\n\ndeclare global {\n namespace Kubb {\n interface App {}\n }\n}\n\nexport const progressPlugin = createPlugin<Options>({\n name: 'progress',\n install(app) {\n const progressBar = new SingleBar(\n {\n format: '{bar} {percentage}% | {value}/{total} | {message}',\n barCompleteChar: '█',\n barIncompleteChar: '░',\n hideCursor: true,\n clearOnComplete: true,\n },\n Presets.shades_grey,\n )\n\n app.context.events.on('process:start', async ({ files }) => {\n progressBar.start(files.length, 0, { message: 'Starting...' })\n })\n\n app.context.events.on('process:progress', async ({ file }) => {\n const message = `Writing ${relative(process.cwd(), file.path)}`\n progressBar.increment(1, { message })\n })\n\n app.context.events.on('process:end', async ({ files }) => {\n progressBar.update(files.length, { message: 'Done ✅' })\n progressBar.stop()\n })\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;AAEA,SAAgB,aACd,QACgC;AAChC,QAAO;EACL,MAAM;EACN,GAAG;EACJ;;;;;ACkBH,eAAsB,MAAM,QAAc,MAA0B,UAAgC,EAAE,EAA+B;AACnI,iCACE;EACE,MAAM,OAAO,QAAM,QAA0B,EAAE,aAAmC;AAChF,OAAI,CAACA,2DAAQA,OAAM,MAAM,MAAK,GAC5B;AAGF,OAAI;IACF,MAAM,aAAa,MAAMC,iBAAG,gCAAiBC,OAAK,EAAE,EAClD,UAAU,SACX,CAAC;AACF,iEAAI,WAAY,UAAU,uDAAKF,OAAM,UAAU,EAC7C;YAEK,MAAM;AAIf,SAAMC,iBAAG,kCAAmBC,OAAK,EAAEF,OAAK,MAAM,EAAE,EAAE,UAAU,SAAS,CAAC;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MAAM;CACN,QAAQ,KAAK;EACX,MAAM,cAAc,IAAIC,uBACtB;GACE,QAAQ;GACR,iBAAiB;GACjB,mBAAmB;GACnB,YAAY;GACZ,iBAAiB;GAClB,EACDC,qBAAQ,YACT;AAED,MAAI,QAAQ,OAAO,GAAG,iBAAiB,OAAO,EAAE,YAAY;AAC1D,eAAY,MAAM,MAAM,QAAQ,GAAG,EAAE,SAAS,eAAe,CAAC;IAC9D;AAEF,MAAI,QAAQ,OAAO,GAAG,oBAAoB,OAAO,EAAE,WAAW;GAC5D,MAAM,UAAU,mCAAoBC,qBAAQ,KAAK,EAAE,KAAK,KAAK;AAC7D,eAAY,UAAU,GAAG,EAAE,SAAS,CAAC;IACrC;AAEF,MAAI,QAAQ,OAAO,GAAG,eAAe,OAAO,EAAE,YAAY;AACxD,eAAY,OAAO,MAAM,QAAQ,EAAE,SAAS,UAAU,CAAC;AACvD,eAAY,MAAM;IAClB;;CAEL,CAAC"}
|
|
1
|
+
{"version":3,"file":"plugins.cjs","names":["data","fs","path","stack: Array<TreeNode<TData>>","result: Array<TreeNode<TData>>","nodes: Array<{ id: string; label: string }>","edges: Array<{ from: string; to: string }>","next: TreeNode<BarrelData> | undefined","path","createFile","getRelativePath","SingleBar","Presets","process","http","createFile","path"],"sources":["../src/plugins/createPlugin.ts","../src/plugins/fsPlugin.ts","../src/utils/TreeNode.ts","../src/plugins/barrelPlugin.ts","../src/plugins/progressPlugin.ts","../src/plugins/graphPlugin.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}\n\ntype Options = {\n dryRun?: boolean\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 onBeforeWrite?: (path: string, data: string | undefined) => 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 | undefined, options: { sanity?: boolean } = {}): Promise<string | undefined> {\n return switcher(\n {\n node: async (path, data: string | undefined, { sanity }: { sanity?: boolean }) => {\n if (!data || data?.trim() === '') {\n return undefined\n }\n\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.trim(), { 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 | undefined, { sanity }: { sanity?: boolean }) => {\n if (!data || data?.trim() === '') {\n return undefined\n }\n\n await Bun.write(resolve(path), data.trim())\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[${data.length}]:\\n${data}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n\n return savedData\n }\n\n return data\n },\n },\n 'node',\n )(path, data, 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.onBeforeWrite) {\n await options.onBeforeWrite(file.path, source)\n }\n await write(file.path, source, { sanity: false })\n })\n },\n inject(app, { dryRun } = {}) {\n return {\n async write(\n options = {\n extension: { '.ts': '.ts' },\n },\n ) {\n await app.context.fileManager.write({\n mode: app.context.options?.mode,\n extension: options.extension,\n 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 type Graph = {\n nodes: Array<{ id: string; label: string }>\n edges: Array<{ from: string; to: 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 toGraph(root: TreeNode<BarrelData>): Graph {\n const nodes: Array<{ id: string; label: string }> = []\n const edges: Array<{ from: string; to: string }> = []\n\n root.forEach((node) => {\n nodes.push({\n id: node.data.path,\n label: node.data.name,\n })\n\n for (const child of node.children) {\n edges.push({\n from: node.data.path,\n to: child.data.path,\n })\n }\n })\n\n return { nodes, edges }\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\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 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 dryRun?: boolean\n}\n\ntype WriteEntryOptions = {\n root: string\n mode: Mode\n}\n\ntype ExtendOptions = {\n writeEntry(options: WriteEntryOptions): 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: WriteEntryOptions): Promise<void>\n }\n}\n\ndeclare global {\n namespace Kubb {\n interface App {\n writeEntry(options: WriteEntryOptions): 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 treeNode = TreeNode.fromFiles(files, root)\n\n if (!treeNode) {\n return []\n }\n\n treeNode.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.on('write:start', 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 },\n inject(app, options) {\n if (!options) {\n throw new Error('Barrel plugin requires options.root and options.mode')\n }\n\n return {\n async writeEntry({ root, mode }) {\n if (!mode || mode === 'propagate') {\n return undefined\n }\n\n const rootPath = path.resolve(root, 'index.ts')\n\n const barrelFiles = app.files.filter((file) => {\n return file.sources.some((source) => source.isIndexable)\n })\n\n const entryFile = 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(entryFile)\n\n await app.context.fileManager.write({\n mode: app.context.options?.mode,\n dryRun: options.dryRun,\n parsers: app.context.installedParsers,\n })\n },\n }\n },\n})\n","import { Presets, SingleBar } from 'cli-progress'\nimport { createPlugin } from './createPlugin.ts'\nimport { relative } from 'node:path'\nimport process from 'node:process'\n\nexport const progressPlugin = createPlugin({\n name: 'progress',\n install(app) {\n const progressBar = new SingleBar(\n {\n format: '{bar} {percentage}% | {value}/{total} | {message}',\n barCompleteChar: '█',\n barIncompleteChar: '░',\n hideCursor: true,\n clearOnComplete: true,\n },\n Presets.shades_grey,\n )\n\n app.context.events.on('process:start', async ({ files }) => {\n progressBar.start(files.length, 0, { message: 'Starting...' })\n })\n\n app.context.events.on('process:progress', async ({ file }) => {\n const message = `Writing ${relative(process.cwd(), file.path)}`\n progressBar.increment(1, { message })\n })\n\n app.context.events.on('process:end', async ({ files }) => {\n progressBar.update(files.length, { message: 'Done ✅' })\n progressBar.stop()\n })\n },\n})\n","import { createPlugin } from './createPlugin.ts'\nimport type * as KubbFile from '../KubbFile.ts'\nimport { type Graph, TreeNode } from '../utils/TreeNode.ts'\nimport path from 'node:path'\nimport http from 'node:http'\nimport open from 'tiny-open'\nimport type { AddressInfo } from 'node:net'\nimport handler from 'serve-handler'\n\nimport { createFile } from '../createFile.ts'\n\ntype Options = {\n root: string\n /**\n * @default false\n */\n open?: boolean\n}\n\ntype GetGraphOptions = {\n files: KubbFile.File[]\n root: string\n}\n\nexport function getGraph({ files, root }: GetGraphOptions): Graph | undefined {\n const treeNode = TreeNode.fromFiles(files, root)\n\n if (!treeNode) {\n return undefined\n }\n\n return TreeNode.toGraph(treeNode)\n}\nconst html = `\n <!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"UTF-8\" />\n <title>File Graph</title>\n <script type=\"module\">\n import { Network } from 'https://cdn.jsdelivr.net/npm/vis-network/standalone/esm/vis-network.min.js'\n\n async function main() {\n const res = await fetch('./graph.json')\n const { nodes, edges } = await res.json()\n const container = document.getElementById('graph')\n\n const network = new Network(\n container,\n { nodes, edges },\n {\n layout: { hierarchical: { direction: 'UD', sortMethod: 'directed' } },\n nodes: { shape: 'box', font: { face: 'monospace' } },\n edges: { arrows: 'to' },\n physics: false,\n },\n )\n }\n\n main()\n </script>\n <style>\n html, body, #graph { height: 100%; margin: 0; }\n </style>\n </head>\n <body>\n <div id=\"graph\"></div>\n </body>\n</html>\n`\n\nasync function serve(root: string) {\n const server = http.createServer((req, res) => {\n return handler(req, res, {\n public: root,\n cleanUrls: true,\n })\n })\n\n server.listen(0, async () => {\n const { port } = server.address() as AddressInfo\n console.log(`Running on http://localhost:${port}/graph.html`)\n\n await open(`http://localhost:${port}/graph.html`)\n })\n}\n\nexport const graphPlugin = createPlugin<Options>({\n name: 'graph',\n install(app, options) {\n if (!options) {\n throw new Error('Graph plugin requires options.root and options.mode')\n }\n\n app.context.events.on('write:start', async ({ files }) => {\n const root = options.root\n\n const graph = getGraph({ files, root })\n\n if (!graph) {\n return undefined\n }\n\n const graphFile = createFile({\n baseName: 'graph.json',\n path: path.join(root, 'graph.json'),\n sources: [\n {\n name: 'graph',\n value: JSON.stringify(graph, null, 2),\n },\n ],\n })\n\n const graphHtmlFile = createFile({\n baseName: 'graph.html',\n path: path.join(root, 'graph.html'),\n sources: [\n {\n name: 'graph',\n value: html,\n },\n ],\n })\n\n await app.context.fileManager.add(graphFile, graphHtmlFile)\n\n if (options.open) {\n await serve(root)\n }\n })\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAEA,SAAgB,aACd,QACgC;AAChC,QAAO;EACL,MAAM;EACN,GAAG;EACJ;;;;;ACkBH,eAAsB,MAAM,QAAc,MAA0B,UAAgC,EAAE,EAA+B;AACnI,iCACE;EACE,MAAM,OAAO,QAAM,QAA0B,EAAE,aAAmC;AAChF,OAAI,CAACA,2DAAQA,OAAM,MAAM,MAAK,GAC5B;AAGF,OAAI;IACF,MAAM,aAAa,MAAMC,iBAAG,gCAAiBC,OAAK,EAAE,EAClD,UAAU,SACX,CAAC;AACF,iEAAI,WAAY,UAAU,uDAAKF,OAAM,UAAU,EAC7C;YAEK,MAAM;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,WAAW;GACZ,CAAC;GACF;AAEF,QAAO,OAAO,GAAG,YAAY;EAC3B,MAAM,EAAE,SAAS,OAAO,SAAS;AACjC,UAAQ,IAAI,+BAA+B,KAAK,aAAa;AAE7D,+BAAW,oBAAoB,KAAK,aAAa;GACjD;;AAGJ,MAAa,cAAc,aAAsB;CAC/C,MAAM;CACN,QAAQ,KAAK,SAAS;AACpB,MAAI,CAAC,QACH,OAAM,IAAI,MAAM,sDAAsD;AAGxE,MAAI,QAAQ,OAAO,GAAG,eAAe,OAAO,EAAE,YAAY;GACxD,MAAM,OAAO,QAAQ;GAErB,MAAM,QAAQ,SAAS;IAAE;IAAO;IAAM,CAAC;AAEvC,OAAI,CAAC,MACH;GAGF,MAAM,YAAYC,kCAAW;IAC3B,UAAU;IACV,MAAMC,kBAAK,KAAK,MAAM,aAAa;IACnC,SAAS,CACP;KACE,MAAM;KACN,OAAO,KAAK,UAAU,OAAO,MAAM,EAAE;KACtC,CACF;IACF,CAAC;GAEF,MAAM,gBAAgBD,kCAAW;IAC/B,UAAU;IACV,MAAMC,kBAAK,KAAK,MAAM,aAAa;IACnC,SAAS,CACP;KACE,MAAM;KACN,OAAO;KACR,CACF;IACF,CAAC;AAEF,SAAM,IAAI,QAAQ,YAAY,IAAI,WAAW,cAAc;AAE3D,OAAI,QAAQ,KACV,OAAM,MAAM,KAAK;IAEnB;;CAEL,CAAC"}
|
package/dist/plugins.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as Plugin, l as UserPlugin, u as Extname } from "./App-
|
|
1
|
+
import { c as Plugin, l as UserPlugin, u as Extname } from "./App-Cjd-lGfW.cjs";
|
|
2
2
|
|
|
3
3
|
//#region src/plugins/createPlugin.d.ts
|
|
4
4
|
declare function createPlugin<Options$3 = unknown, TAppExtension extends Record<string, any> = {}>(plugin: UserPlugin<Options$3, TAppExtension>): Plugin<Options$3, TAppExtension>;
|
|
@@ -64,16 +64,17 @@ declare global {
|
|
|
64
64
|
declare const barrelPlugin: Plugin<Options$1, ExtendOptions>;
|
|
65
65
|
//#endregion
|
|
66
66
|
//#region src/plugins/progressPlugin.d.ts
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
67
|
+
declare const progressPlugin: Plugin<[], {}>;
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/plugins/graphPlugin.d.ts
|
|
70
|
+
type Options = {
|
|
71
|
+
root: string;
|
|
72
|
+
/**
|
|
73
|
+
* @default false
|
|
74
|
+
*/
|
|
75
|
+
open?: boolean;
|
|
76
|
+
};
|
|
77
|
+
declare const graphPlugin: Plugin<Options, {}>;
|
|
77
78
|
//#endregion
|
|
78
|
-
export { barrelPlugin, createPlugin, fsPlugin, progressPlugin };
|
|
79
|
+
export { barrelPlugin, createPlugin, fsPlugin, graphPlugin, progressPlugin };
|
|
79
80
|
//# sourceMappingURL=plugins.d.cts.map
|
package/dist/plugins.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as Plugin, l as UserPlugin, u as Extname } from "./App-
|
|
1
|
+
import { c as Plugin, l as UserPlugin, u as Extname } from "./App-ztRQpZS9.js";
|
|
2
2
|
|
|
3
3
|
//#region src/plugins/createPlugin.d.ts
|
|
4
4
|
declare function createPlugin<Options$3 = unknown, TAppExtension extends Record<string, any> = {}>(plugin: UserPlugin<Options$3, TAppExtension>): Plugin<Options$3, TAppExtension>;
|
|
@@ -64,16 +64,17 @@ declare global {
|
|
|
64
64
|
declare const barrelPlugin: Plugin<Options$1, ExtendOptions>;
|
|
65
65
|
//#endregion
|
|
66
66
|
//#region src/plugins/progressPlugin.d.ts
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
67
|
+
declare const progressPlugin: Plugin<[], {}>;
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/plugins/graphPlugin.d.ts
|
|
70
|
+
type Options = {
|
|
71
|
+
root: string;
|
|
72
|
+
/**
|
|
73
|
+
* @default false
|
|
74
|
+
*/
|
|
75
|
+
open?: boolean;
|
|
76
|
+
};
|
|
77
|
+
declare const graphPlugin: Plugin<Options, {}>;
|
|
77
78
|
//#endregion
|
|
78
|
-
export { barrelPlugin, createPlugin, fsPlugin, progressPlugin };
|
|
79
|
+
export { barrelPlugin, createPlugin, fsPlugin, graphPlugin, progressPlugin };
|
|
79
80
|
//# sourceMappingURL=plugins.d.ts.map
|
package/dist/plugins.js
CHANGED
|
@@ -6,6 +6,9 @@ import { switcher } from "js-runtime";
|
|
|
6
6
|
import fs from "fs-extra";
|
|
7
7
|
import { Presets, SingleBar } from "cli-progress";
|
|
8
8
|
import process from "node:process";
|
|
9
|
+
import http from "node:http";
|
|
10
|
+
import open from "tiny-open";
|
|
11
|
+
import handler from "serve-handler";
|
|
9
12
|
|
|
10
13
|
//#region src/plugins/createPlugin.ts
|
|
11
14
|
function createPlugin(plugin) {
|
|
@@ -38,7 +41,7 @@ async function write(path$1, data, options = {}) {
|
|
|
38
41
|
await Bun.write(resolve(path$2), data$1.trim());
|
|
39
42
|
if (sanity) {
|
|
40
43
|
const savedData = await Bun.file(resolve(path$2)).text();
|
|
41
|
-
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[${
|
|
44
|
+
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`);
|
|
42
45
|
return savedData;
|
|
43
46
|
}
|
|
44
47
|
return data$1;
|
|
@@ -112,6 +115,24 @@ var TreeNode = class TreeNode {
|
|
|
112
115
|
findDeep(predicate) {
|
|
113
116
|
for (const leaf of this.leaves) if (predicate(leaf)) return leaf;
|
|
114
117
|
}
|
|
118
|
+
static toGraph(root) {
|
|
119
|
+
const nodes = [];
|
|
120
|
+
const edges = [];
|
|
121
|
+
root.forEach((node) => {
|
|
122
|
+
nodes.push({
|
|
123
|
+
id: node.data.path,
|
|
124
|
+
label: node.data.name
|
|
125
|
+
});
|
|
126
|
+
for (const child of node.children) edges.push({
|
|
127
|
+
from: node.data.path,
|
|
128
|
+
to: child.data.path
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
return {
|
|
132
|
+
nodes,
|
|
133
|
+
edges
|
|
134
|
+
};
|
|
135
|
+
}
|
|
115
136
|
static fromFiles(files, rootFolder = "") {
|
|
116
137
|
const normalizePath = (p) => p.replace(/\\/g, "/");
|
|
117
138
|
const normalizedRoot = normalizePath(rootFolder);
|
|
@@ -156,8 +177,9 @@ function getBarrelFiles({ files, root, mode }) {
|
|
|
156
177
|
if (mode === "propagate" || mode === false) return [];
|
|
157
178
|
const cachedFiles = /* @__PURE__ */ new Map();
|
|
158
179
|
const dedupe = /* @__PURE__ */ new Map();
|
|
159
|
-
const
|
|
160
|
-
|
|
180
|
+
const treeNode = TreeNode.fromFiles(files, root);
|
|
181
|
+
if (!treeNode) return [];
|
|
182
|
+
treeNode.forEach((node) => {
|
|
161
183
|
var _node$parent;
|
|
162
184
|
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;
|
|
163
185
|
const parentPath = node.parent.data.path;
|
|
@@ -215,8 +237,7 @@ const barrelPlugin = createPlugin({
|
|
|
215
237
|
install(app, options) {
|
|
216
238
|
if (!options) throw new Error("Barrel plugin requires options.root and options.mode");
|
|
217
239
|
if (!options.mode) return;
|
|
218
|
-
app.context.events.
|
|
219
|
-
var _app$context$options;
|
|
240
|
+
app.context.events.on("write:start", async ({ files }) => {
|
|
220
241
|
const root = options.root;
|
|
221
242
|
const barrelFiles = getBarrelFiles({
|
|
222
243
|
files,
|
|
@@ -224,19 +245,14 @@ const barrelPlugin = createPlugin({
|
|
|
224
245
|
mode: options.mode
|
|
225
246
|
});
|
|
226
247
|
await app.context.fileManager.add(...barrelFiles);
|
|
227
|
-
await app.context.fileManager.write({
|
|
228
|
-
mode: (_app$context$options = app.context.options) === null || _app$context$options === void 0 ? void 0 : _app$context$options.mode,
|
|
229
|
-
dryRun: options.dryRun,
|
|
230
|
-
parsers: app.context.installedParsers
|
|
231
|
-
});
|
|
232
248
|
});
|
|
233
249
|
},
|
|
234
250
|
inject(app, options) {
|
|
235
251
|
if (!options) throw new Error("Barrel plugin requires options.root and options.mode");
|
|
236
252
|
return { async writeEntry({ root, mode }) {
|
|
237
|
-
var _app$context$
|
|
253
|
+
var _app$context$options;
|
|
238
254
|
if (!mode || mode === "propagate") return;
|
|
239
|
-
const rootPath = resolve(root, "index.ts");
|
|
255
|
+
const rootPath = path.resolve(root, "index.ts");
|
|
240
256
|
const entryFile = createFile({
|
|
241
257
|
path: rootPath,
|
|
242
258
|
baseName: "index.ts",
|
|
@@ -258,7 +274,7 @@ const barrelPlugin = createPlugin({
|
|
|
258
274
|
});
|
|
259
275
|
await app.context.fileManager.add(entryFile);
|
|
260
276
|
await app.context.fileManager.write({
|
|
261
|
-
mode: (_app$context$
|
|
277
|
+
mode: (_app$context$options = app.context.options) === null || _app$context$options === void 0 ? void 0 : _app$context$options.mode,
|
|
262
278
|
dryRun: options.dryRun,
|
|
263
279
|
parsers: app.context.installedParsers
|
|
264
280
|
});
|
|
@@ -293,5 +309,95 @@ const progressPlugin = createPlugin({
|
|
|
293
309
|
});
|
|
294
310
|
|
|
295
311
|
//#endregion
|
|
296
|
-
|
|
312
|
+
//#region src/plugins/graphPlugin.ts
|
|
313
|
+
function getGraph({ files, root }) {
|
|
314
|
+
const treeNode = TreeNode.fromFiles(files, root);
|
|
315
|
+
if (!treeNode) return;
|
|
316
|
+
return TreeNode.toGraph(treeNode);
|
|
317
|
+
}
|
|
318
|
+
const html = `
|
|
319
|
+
<!DOCTYPE html>
|
|
320
|
+
<html lang="en">
|
|
321
|
+
<head>
|
|
322
|
+
<meta charset="UTF-8" />
|
|
323
|
+
<title>File Graph</title>
|
|
324
|
+
<script type="module">
|
|
325
|
+
import { Network } from 'https://cdn.jsdelivr.net/npm/vis-network/standalone/esm/vis-network.min.js'
|
|
326
|
+
|
|
327
|
+
async function main() {
|
|
328
|
+
const res = await fetch('./graph.json')
|
|
329
|
+
const { nodes, edges } = await res.json()
|
|
330
|
+
const container = document.getElementById('graph')
|
|
331
|
+
|
|
332
|
+
const network = new Network(
|
|
333
|
+
container,
|
|
334
|
+
{ nodes, edges },
|
|
335
|
+
{
|
|
336
|
+
layout: { hierarchical: { direction: 'UD', sortMethod: 'directed' } },
|
|
337
|
+
nodes: { shape: 'box', font: { face: 'monospace' } },
|
|
338
|
+
edges: { arrows: 'to' },
|
|
339
|
+
physics: false,
|
|
340
|
+
},
|
|
341
|
+
)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
main()
|
|
345
|
+
<\/script>
|
|
346
|
+
<style>
|
|
347
|
+
html, body, #graph { height: 100%; margin: 0; }
|
|
348
|
+
</style>
|
|
349
|
+
</head>
|
|
350
|
+
<body>
|
|
351
|
+
<div id="graph"></div>
|
|
352
|
+
</body>
|
|
353
|
+
</html>
|
|
354
|
+
`;
|
|
355
|
+
async function serve(root) {
|
|
356
|
+
const server = http.createServer((req, res) => {
|
|
357
|
+
return handler(req, res, {
|
|
358
|
+
public: root,
|
|
359
|
+
cleanUrls: true
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
server.listen(0, async () => {
|
|
363
|
+
const { port } = server.address();
|
|
364
|
+
console.log(`Running on http://localhost:${port}/graph.html`);
|
|
365
|
+
await open(`http://localhost:${port}/graph.html`);
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
const graphPlugin = createPlugin({
|
|
369
|
+
name: "graph",
|
|
370
|
+
install(app, options) {
|
|
371
|
+
if (!options) throw new Error("Graph plugin requires options.root and options.mode");
|
|
372
|
+
app.context.events.on("write:start", async ({ files }) => {
|
|
373
|
+
const root = options.root;
|
|
374
|
+
const graph = getGraph({
|
|
375
|
+
files,
|
|
376
|
+
root
|
|
377
|
+
});
|
|
378
|
+
if (!graph) return;
|
|
379
|
+
const graphFile = createFile({
|
|
380
|
+
baseName: "graph.json",
|
|
381
|
+
path: path.join(root, "graph.json"),
|
|
382
|
+
sources: [{
|
|
383
|
+
name: "graph",
|
|
384
|
+
value: JSON.stringify(graph, null, 2)
|
|
385
|
+
}]
|
|
386
|
+
});
|
|
387
|
+
const graphHtmlFile = createFile({
|
|
388
|
+
baseName: "graph.html",
|
|
389
|
+
path: path.join(root, "graph.html"),
|
|
390
|
+
sources: [{
|
|
391
|
+
name: "graph",
|
|
392
|
+
value: html
|
|
393
|
+
}]
|
|
394
|
+
});
|
|
395
|
+
await app.context.fileManager.add(graphFile, graphHtmlFile);
|
|
396
|
+
if (options.open) await serve(root);
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
//#endregion
|
|
402
|
+
export { barrelPlugin, createPlugin, fsPlugin, graphPlugin, progressPlugin };
|
|
297
403
|
//# sourceMappingURL=plugins.js.map
|