@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/README.md
CHANGED
|
@@ -22,6 +22,282 @@
|
|
|
22
22
|
|
|
23
23
|
<br />
|
|
24
24
|
|
|
25
|
+
Kubb Fabric is a language-agnostic toolkit for generating code and files using JSX and TypeScript.
|
|
26
|
+
It offers a lightweight layer for file generation while orchestrating the overall process of creating and managing files.
|
|
27
|
+
|
|
28
|
+
> [!WARNING]
|
|
29
|
+
> Fabric is under active development. Until a stable 1.0 release, minor versions may occasionally include breaking changes. Please check release notes and PR titles for breaking changes.
|
|
30
|
+
|
|
31
|
+
# Features
|
|
32
|
+
|
|
33
|
+
- 🎨 Declarative file generation — Create files effortlessly using JSX or JavaScript syntax.
|
|
34
|
+
- 📦 Cross-runtime support — Works seamlessly with Node.js and Bun.
|
|
35
|
+
- 🧩 Built-in debugging utilities — Simplify development and inspect generation flows with ease.
|
|
36
|
+
- ⚡ Fast and lightweight — Minimal overhead, maximum performance.
|
|
37
|
+
|
|
38
|
+
## Write a TypeScript file
|
|
39
|
+
|
|
40
|
+
Below is a minimal example showing how `createApp` works together with plugins and parsers via `app.use`.
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { createApp } from '@kubb/fabric-core'
|
|
44
|
+
import { fsPlugin } from '@kubb/fabric-core/plugins'
|
|
45
|
+
import { typescriptParser, createParser } from '@kubb/fabric-core/parsers'
|
|
46
|
+
|
|
47
|
+
const app = createApp()
|
|
48
|
+
|
|
49
|
+
app.use(fsPlugin, {
|
|
50
|
+
dryRun: false,
|
|
51
|
+
onBeforeWrite: (path, data) => {
|
|
52
|
+
console.log('About to write:', path)
|
|
53
|
+
},
|
|
54
|
+
clean: { path: './generated' },
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
app.use(typescriptParser)
|
|
58
|
+
|
|
59
|
+
await app.addFile({
|
|
60
|
+
baseName: 'index.ts',
|
|
61
|
+
path: './generated/index.ts',
|
|
62
|
+
sources: [
|
|
63
|
+
{ value: 'export const x = 1', isExportable: true },
|
|
64
|
+
],
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
await app.write()
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
# API Reference
|
|
72
|
+
|
|
73
|
+
## Core
|
|
74
|
+
### `createApp(options?): App`
|
|
75
|
+
Returns an app instance with:
|
|
76
|
+
- `app.use(pluginOrParser, ...options) => App` — register plugins and parsers.
|
|
77
|
+
- `app.addFile(...files)` — queue in-memory files to generate.
|
|
78
|
+
- `app.files` — getter with all queued files.
|
|
79
|
+
- `app.context` — internal context holding events, options, FileManager, installed plugins/parsers.
|
|
80
|
+
|
|
81
|
+
### `defineApp(instance?): () => App`
|
|
82
|
+
Factory to create your own `createApp` with an optional bootstrap `instance(app)` called on creation.
|
|
83
|
+
|
|
84
|
+
### App events (emitted by the core during processing)
|
|
85
|
+
- `start`
|
|
86
|
+
- `end`
|
|
87
|
+
- `render { app }`
|
|
88
|
+
- `file:add { files }`
|
|
89
|
+
- `write:start { files }`
|
|
90
|
+
- `write:end { files }`
|
|
91
|
+
- `file:start { file, index, total }`
|
|
92
|
+
- `file:end { file, index, total }`
|
|
93
|
+
- `process:start { files }`
|
|
94
|
+
- `process:progress { file, source, processed, percentage, total }`
|
|
95
|
+
- `process:end { files }`
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
## Plugins
|
|
99
|
+
#### `fsPlugin`
|
|
100
|
+
Writes files to disk on `process:progress`, supports dry runs and cleaning an output folder before writing.
|
|
101
|
+
|
|
102
|
+
```
|
|
103
|
+
import { fsPlugin } from '@kubb/fabric-core/plugins'
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
| Option | Type | Default | Description |
|
|
107
|
+
|---|----------------------------------------------------------------------|---|-----------------------------------------------------------------------|
|
|
108
|
+
| dryRun | `boolean` | `false` | If true, do not write files to disk. |
|
|
109
|
+
| onBeforeWrite | `(path: string, data: string \| undefined) => void \| Promise<void>` | — | Called right before each file write on `process:progress`. |
|
|
110
|
+
| clean | `{ path: string }` | — | If provided, removes the directory at `path` before writing any files. |
|
|
111
|
+
|
|
112
|
+
Injected `app.write` options (via `fsPlugin`):
|
|
113
|
+
|
|
114
|
+
| Option | Type | Default | Description |
|
|
115
|
+
|---|----------------------------------|---|---|
|
|
116
|
+
| extension | `Record<Extname, Extname \| ''>` | — | Maps input file extensions to output extensions. When set, the matching parser (by extNames) is used. |
|
|
117
|
+
|
|
118
|
+
#### `barrelPlugin`
|
|
119
|
+
Generates `index.ts` barrel files per folder at `process:end`. `writeEntry` creates a single entry barrel at `root`.
|
|
120
|
+
|
|
121
|
+
```
|
|
122
|
+
import { barrelPlugin } from '@kubb/fabric-core/plugins'
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
| Option | Type | Default | Description |
|
|
126
|
+
|---|--------------------------------------------|---|---|
|
|
127
|
+
| root | `string` | — | Root directory to generate barrel files for. |
|
|
128
|
+
| mode | `'all' \| 'named' \| 'propagate' \| false` | — | Controls how exports are generated: all exports, only named exports, propagate (skip barrels), or disabled. |
|
|
129
|
+
| dryRun | `boolean` | `false` | If true, computes barrels but skips writing. |
|
|
130
|
+
|
|
131
|
+
Injected `app.writeEntry` parameters (via `barrelPlugin`):
|
|
132
|
+
|
|
133
|
+
| Param | Type | Description |
|
|
134
|
+
|---|--------------------------------------------|---|
|
|
135
|
+
| root | `string` | Root directory where the entry `index.ts` should be created. |
|
|
136
|
+
| mode | `'all' \| 'named' \| 'propagate' \| false` | Controls which export style to use for the entry barrel. |
|
|
137
|
+
|
|
138
|
+
#### `progressPlugin`
|
|
139
|
+
Shows a CLI progress bar by listening to core events.
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
import { progressPlugin } from '@kubb/fabric-core/plugins'
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
| Option | Type | Default | Description |
|
|
146
|
+
|---|---|---|-----------------------------------------------------------------------------------------|
|
|
147
|
+
| — | — | — | This plugin has no options, it displays a CLI progress bar by listening to core events. |
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
#### `graphPlugin`
|
|
151
|
+
Shows a graph of all files
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
import { graphPlugin } from '@kubb/fabric-core/plugins'
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
| Option | Type | Default | Description |
|
|
158
|
+
|--------|-----------|---------|-----------------------------------------------|
|
|
159
|
+
| root | `string` | | Root directory where to start searching from. |
|
|
160
|
+
| open | `boolean` | false | Open a webpage with the generated graph |
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
#### `reactPlugin`
|
|
164
|
+
Enables rendering React components to the terminal or to a string. Useful for CLI UIs and templating.
|
|
165
|
+
|
|
166
|
+
```
|
|
167
|
+
import { reactPlugin } from '@kubb/react-fabric/plugins'
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
| Option | Type | Default | Description |
|
|
171
|
+
|---|---|---|---|
|
|
172
|
+
| stdout | `NodeJS.WriteStream` | — | Optional output stream used to print the rendered content while the app is running. If set, the output is written progressively. |
|
|
173
|
+
| stdin | `NodeJS.ReadStream` | — | Optional input stream for interactive components. |
|
|
174
|
+
| stderr | `NodeJS.WriteStream` | — | Optional error output stream. |
|
|
175
|
+
| debug | `boolean` | — | When true, logs render/unmount information to the console to aid debugging. |
|
|
176
|
+
|
|
177
|
+
Injected methods (via `reactPlugin`):
|
|
178
|
+
|
|
179
|
+
| Method | Signature | Description |
|
|
180
|
+
|---|---|----------------------------------------------------------------------------------------------------|
|
|
181
|
+
| `render` | `(App: React.ElementType) => Promise<void> \| void` | Render a React component tree to the terminal and emit the core `start` event. |
|
|
182
|
+
| `renderToString` | `(App: React.ElementType) => Promise<string> \| string` | Render a React component tree and return the final output as a string (without writing to stdout). |
|
|
183
|
+
| `waitUntilExit` | `() => Promise<void>` | Wait until the rendered app exits, resolves when unmounted and emits the core `end` event. |
|
|
184
|
+
|
|
185
|
+
#### `createPlugin`
|
|
186
|
+
|
|
187
|
+
Factory to declare a plugin that can be registered via `app.use`.
|
|
188
|
+
|
|
189
|
+
| Field | Required | Description |
|
|
190
|
+
|---|---|---------------------------------------------------------------------------------------------------------------------------|
|
|
191
|
+
| `name` | Yes | String identifier of your plugin. |
|
|
192
|
+
| `install(app, options)` | Yes | Called when the plugin is registered. You can subscribe to core events and perform side effects here. |
|
|
193
|
+
| `inject?(app, options)` | No | Return synchronously the runtime methods/properties to merge into `app` (e.g. `write`, `render`). This must not be async. |
|
|
194
|
+
|
|
195
|
+
Example:
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
import { createApp } from '@kubb/fabric-core'
|
|
199
|
+
import { createPlugin } from '@kubb/fabric-core/plugins'
|
|
200
|
+
|
|
201
|
+
const helloPlugin = createPlugin<{ name?: string }, { sayHello: (msg?: string) => void }>({
|
|
202
|
+
name: 'helloPlugin',
|
|
203
|
+
install(app, options) {
|
|
204
|
+
app.context.events.on('start', () => {
|
|
205
|
+
console.log('App started')
|
|
206
|
+
})
|
|
207
|
+
},
|
|
208
|
+
inject(app, options) {
|
|
209
|
+
return {
|
|
210
|
+
sayHello(msg = options?.name ?? 'world') {
|
|
211
|
+
console.log(`Hello ${msg}!`)
|
|
212
|
+
},
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
const app = createApp()
|
|
218
|
+
await app.use(helloPlugin, { name: 'Fabric' })
|
|
219
|
+
app.sayHello() // -> Hello Fabric!
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Parsers
|
|
223
|
+
#### `typescriptParser`
|
|
224
|
+
|
|
225
|
+
Prints TS/JS imports/exports and sources, supports extname mapping for generated import/export paths.
|
|
226
|
+
|
|
227
|
+
```
|
|
228
|
+
import { typescriptParser } from '@kubb/fabric-core/parsers'
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
| Option | Type | Default | Description |
|
|
232
|
+
|---|---|---|---------------------------------------------------------------------------------------------|
|
|
233
|
+
| file | `KubbFile.File` | -| File that will be used to be parsed. |
|
|
234
|
+
| extname | `string` | `'.ts'` | Extension to use when emitting import/export paths (e.g., rewrite `./file` to `./file.ts`). |
|
|
235
|
+
|
|
236
|
+
#### `tsxParser`
|
|
237
|
+
|
|
238
|
+
Delegates to `typescriptParser` with TSX printing settings.
|
|
239
|
+
|
|
240
|
+
```
|
|
241
|
+
import { tsxParser } from '@kubb/fabric-core/parsers'
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
| Option | Type | Default | Description |
|
|
245
|
+
|---|---|---|---|
|
|
246
|
+
| file | `KubbFile.File` | -| File that will be used to be parsed. |
|
|
247
|
+
| extname | `string` | `'.tsx'` | Extension to use when emitting import/export paths for TSX/JSX files. |
|
|
248
|
+
|
|
249
|
+
#### `defaultParser`
|
|
250
|
+
|
|
251
|
+
Fallback parser used when no extension mapping is provided to `app.write`.
|
|
252
|
+
|
|
253
|
+
```
|
|
254
|
+
import { defaultParser } @kubb/fabric-core/parsers`
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
| Option | Type | Default | Description |
|
|
258
|
+
|---|---|---|--------------------------------------------------------------------------|
|
|
259
|
+
| file | `KubbFile.File` | -| File that will be used to be parsed. |
|
|
260
|
+
|
|
261
|
+
#### `createParser`
|
|
262
|
+
Factory to declare a parser that can be registered via `app.use` and selected by `extNames` during `app.write`.
|
|
263
|
+
|
|
264
|
+
| Field | Required | Description |
|
|
265
|
+
|---|---|-----------------------------------------------------------------------------------------------------------------|
|
|
266
|
+
| `name` | Yes | String identifier of your parser. |
|
|
267
|
+
| `extNames` | Yes | List of file extensions this parser can handle (e.g. ['.ts']). Use `undefined` for the default parser fallback. |
|
|
268
|
+
| `install(app, options)` | No | Optional setup when the parser is registered (subscribe to events, set state, etc.). |
|
|
269
|
+
| `parse(file, { extname })` | Yes | Must return the final string that will be written for the given file. |
|
|
270
|
+
|
|
271
|
+
Example:
|
|
272
|
+
|
|
273
|
+
```ts
|
|
274
|
+
import { createApp } from '@kubb/fabric-core'
|
|
275
|
+
import { createParser } from '@kubb/fabric-core/parsers'
|
|
276
|
+
|
|
277
|
+
const vueParser = createParser<{ banner?: string }>({
|
|
278
|
+
name: 'vueParser',
|
|
279
|
+
extNames: ['.vue'],
|
|
280
|
+
async install(app, options) {
|
|
281
|
+
// Optional setup
|
|
282
|
+
},
|
|
283
|
+
async parse(file, { extname }) {
|
|
284
|
+
const banner = file.options?.banner ?? ''
|
|
285
|
+
const sources = file.sources.map(s => s.value).join('\n')
|
|
286
|
+
return `${banner}\n${sources}`
|
|
287
|
+
},
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
const app = createApp()
|
|
291
|
+
app.use(vueParser)
|
|
292
|
+
app.use(fsPlugin); // make it possible to write to the filesystem
|
|
293
|
+
|
|
294
|
+
app.write({ extension: { '.vue': '.ts' } })
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
> [!NOTE]
|
|
298
|
+
> - `app.use` accepts both plugins and parsers. The `fsPlugin` handles I/O and adds `app.write`. Parsers decide how files are converted to strings for specific extensions.
|
|
299
|
+
> - When extension mapping is provided to `app.write`, Fabric picks a parser whose `extNames` include the file’s extension. Otherwise, the default parser is used.
|
|
300
|
+
|
|
25
301
|
## Supporting Kubb
|
|
26
302
|
|
|
27
303
|
Kubb uses an MIT-licensed open source project with its ongoing development made possible entirely by the support of Sponsors. If you would like to become a sponsor, please consider:
|
|
@@ -247,6 +247,18 @@ type AppEvents = {
|
|
|
247
247
|
'process:start': [{
|
|
248
248
|
files: ResolvedFile[];
|
|
249
249
|
}];
|
|
250
|
+
/**
|
|
251
|
+
* Called when FileManager is adding files to its cache
|
|
252
|
+
*/
|
|
253
|
+
'file:add': [{
|
|
254
|
+
files: ResolvedFile[];
|
|
255
|
+
}];
|
|
256
|
+
'write:start': [{
|
|
257
|
+
files: ResolvedFile[];
|
|
258
|
+
}];
|
|
259
|
+
'write:end': [{
|
|
260
|
+
files: ResolvedFile[];
|
|
261
|
+
}];
|
|
250
262
|
/**
|
|
251
263
|
* Called for each file when processing begins.
|
|
252
264
|
*/
|
|
@@ -300,4 +312,4 @@ interface App<TOptions extends AppOptions = AppOptions> extends Kubb.App {
|
|
|
300
312
|
}
|
|
301
313
|
//#endregion
|
|
302
314
|
export { FileProcessor as a, Plugin as c, File as d, KubbFile_d_exports as f, FileManager as i, UserPlugin as l, AppContext as n, Parser as o, ResolvedFile as p, AppOptions as r, UserParser as s, App as t, Extname as u };
|
|
303
|
-
//# sourceMappingURL=App-
|
|
315
|
+
//# sourceMappingURL=App-Cjd-lGfW.d.cts.map
|
|
@@ -247,6 +247,18 @@ type AppEvents = {
|
|
|
247
247
|
'process:start': [{
|
|
248
248
|
files: ResolvedFile[];
|
|
249
249
|
}];
|
|
250
|
+
/**
|
|
251
|
+
* Called when FileManager is adding files to its cache
|
|
252
|
+
*/
|
|
253
|
+
'file:add': [{
|
|
254
|
+
files: ResolvedFile[];
|
|
255
|
+
}];
|
|
256
|
+
'write:start': [{
|
|
257
|
+
files: ResolvedFile[];
|
|
258
|
+
}];
|
|
259
|
+
'write:end': [{
|
|
260
|
+
files: ResolvedFile[];
|
|
261
|
+
}];
|
|
250
262
|
/**
|
|
251
263
|
* Called for each file when processing begins.
|
|
252
264
|
*/
|
|
@@ -300,4 +312,4 @@ interface App<TOptions extends AppOptions = AppOptions> extends Kubb.App {
|
|
|
300
312
|
}
|
|
301
313
|
//#endregion
|
|
302
314
|
export { FileProcessor as a, Plugin as c, File as d, KubbFile_d_exports as f, FileManager as i, UserPlugin as l, AppContext as n, Parser as o, ResolvedFile as p, AppOptions as r, UserParser as s, App as t, Extname as u };
|
|
303
|
-
//# sourceMappingURL=App-
|
|
315
|
+
//# sourceMappingURL=App-ztRQpZS9.d.ts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { d as File, p as ResolvedFile, r as AppOptions, t as App } from "./App-
|
|
1
|
+
import { d as File, p as ResolvedFile, r as AppOptions, t as App } from "./App-Cjd-lGfW.cjs";
|
|
2
2
|
|
|
3
3
|
//#region src/defineApp.d.ts
|
|
4
4
|
type RootRenderFunction<TApp extends App> = (app: TApp) => void | Promise<void>;
|
|
@@ -15,4 +15,4 @@ declare const createApp: DefineApp<AppOptions>;
|
|
|
15
15
|
declare function createFile<TMeta extends object = object>(file: File<TMeta>): ResolvedFile<TMeta>;
|
|
16
16
|
//#endregion
|
|
17
17
|
export { defineApp as i, createApp as n, DefineApp as r, createFile as t };
|
|
18
|
-
//# sourceMappingURL=index-
|
|
18
|
+
//# sourceMappingURL=index-BUERYeq7.d.cts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { d as File, p as ResolvedFile, r as AppOptions, t as App } from "./App-
|
|
1
|
+
import { d as File, p as ResolvedFile, r as AppOptions, t as App } from "./App-ztRQpZS9.js";
|
|
2
2
|
|
|
3
3
|
//#region src/defineApp.d.ts
|
|
4
4
|
type RootRenderFunction<TApp extends App> = (app: TApp) => void | Promise<void>;
|
|
@@ -15,4 +15,4 @@ declare const createApp: DefineApp<AppOptions>;
|
|
|
15
15
|
declare function createFile<TMeta extends object = object>(file: File<TMeta>): ResolvedFile<TMeta>;
|
|
16
16
|
//#endregion
|
|
17
17
|
export { defineApp as i, createApp as n, DefineApp as r, createFile as t };
|
|
18
|
-
//# sourceMappingURL=index-
|
|
18
|
+
//# sourceMappingURL=index-DfaD7Pj_.d.ts.map
|
package/dist/index.cjs
CHANGED
|
@@ -184,6 +184,7 @@ var FileManager = class {
|
|
|
184
184
|
this.flush();
|
|
185
185
|
resolvedFiles.push(resolvedFile);
|
|
186
186
|
}
|
|
187
|
+
await this.events.emit("file:add", { files: resolvedFiles });
|
|
187
188
|
return resolvedFiles;
|
|
188
189
|
}
|
|
189
190
|
flush() {
|
|
@@ -202,8 +203,10 @@ var FileManager = class {
|
|
|
202
203
|
return (0, natural_orderby.orderBy)(require_defineProperty._classPrivateFieldGet2(_cache, this).keys(), [(v) => v.length, (v) => require_trimExtName.trimExtName(v).endsWith("index")]).map((key) => require_defineProperty._classPrivateFieldGet2(_cache, this).get(key)).filter(Boolean);
|
|
203
204
|
}
|
|
204
205
|
async write(options) {
|
|
206
|
+
await this.events.emit("write:start", { files: this.files });
|
|
205
207
|
const resolvedFiles = await this.processor.run(this.files, options);
|
|
206
208
|
this.clear();
|
|
209
|
+
await this.events.emit("write:end", { files: resolvedFiles });
|
|
207
210
|
return resolvedFiles;
|
|
208
211
|
}
|
|
209
212
|
};
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["e","NodeEventEmitter","defaultParser","parser: Parser | undefined","resolvedFiles: Array<KubbFile.ResolvedFile>","createFile","trimExtName","createApp","options","isFunction"],"sources":["../src/utils/Cache.ts","../../../node_modules/.pnpm/remeda@2.32.0/node_modules/remeda/dist/isFunction-BJjFuZR7.js","../src/utils/AsyncEventEmitter.ts","../src/FileProcessor.ts","../src/FileManager.ts","../src/defineApp.ts","../src/createApp.ts"],"sourcesContent":["export class Cache<T> {\n #buffer = new Map<string, T>()\n\n get(key: string): T | null {\n return this.#buffer.get(key) ?? null\n }\n\n set(key: string, value: T): void {\n this.#buffer.set(key, value)\n }\n\n delete(key: string): void {\n this.#buffer.delete(key)\n }\n\n clear(): void {\n this.#buffer.clear()\n }\n\n keys(): string[] {\n return [...this.#buffer.keys()]\n }\n\n values(): Array<T> {\n return [...this.#buffer.values()]\n }\n\n flush(): void {\n // No-op for base cache\n }\n}\n","const e=e=>typeof e==`function`;export{e as isFunction};\n//# sourceMappingURL=isFunction-BJjFuZR7.js.map","import { EventEmitter as NodeEventEmitter } from 'node:events'\n\nexport class AsyncEventEmitter<TEvents extends Record<string, any>> {\n constructor(maxListener = 100) {\n this.#emitter.setMaxListeners(maxListener)\n }\n #emitter = new NodeEventEmitter()\n\n async emit<TEventName extends keyof TEvents & string>(eventName: TEventName, ...eventArgs: TEvents[TEventName]): Promise<void> {\n const listeners = this.#emitter.listeners(eventName) as Array<(...args: TEvents[TEventName]) => any>\n\n if (listeners.length === 0) {\n return undefined\n }\n\n await Promise.all(\n listeners.map(async (listener) => {\n try {\n return await listener(...eventArgs)\n } catch (err) {\n console.error(`Error in async listener for \"${eventName}\":`, err)\n }\n }),\n )\n }\n\n on<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void {\n this.#emitter.on(eventName, handler as any)\n }\n\n onOnce<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArgs: TEvents[TEventName]) => void): void {\n const wrapper = (...args: TEvents[TEventName]) => {\n this.off(eventName, wrapper)\n handler(...args)\n }\n this.on(eventName, wrapper)\n }\n\n off<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void {\n this.#emitter.off(eventName, handler as any)\n }\n removeAll(): void {\n this.#emitter.removeAllListeners()\n }\n}\n","import type * as KubbFile from './KubbFile.ts'\nimport pLimit from 'p-limit'\n\nimport type { Parser } from './parsers/types.ts'\nimport { defaultParser } from './parsers/defaultParser.ts'\nimport { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'\nimport type { AppEvents, AppMode } from './App.ts'\n\nexport type ProcessFilesProps = {\n parsers?: Set<Parser>\n extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>\n dryRun?: boolean\n /**\n * @default 'sequential'\n */\n mode?: AppMode\n}\n\ntype GetParseOptions = {\n parsers?: Set<Parser>\n extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>\n}\n\ntype Options = {\n events?: AsyncEventEmitter<AppEvents>\n}\n\nexport class FileProcessor {\n #limit = pLimit(100)\n events: AsyncEventEmitter<AppEvents>\n\n constructor({ events = new AsyncEventEmitter<AppEvents>() }: Options = {}) {\n this.events = events\n\n return this\n }\n\n async parse(file: KubbFile.ResolvedFile, { parsers, extension }: GetParseOptions = {}): Promise<string> {\n const parseExtName = extension?.[file.extname] || undefined\n\n if (!parsers) {\n console.warn('No parsers provided, using default parser. If you want to use a specific parser, please provide it in the options.')\n\n return defaultParser.parse(file, { extname: parseExtName })\n }\n\n if (!file.extname) {\n return defaultParser.parse(file, { extname: parseExtName })\n }\n\n let parser: Parser | undefined\n for (const item of parsers) {\n if (item.extNames?.includes(file.extname)) {\n parser = item\n break\n }\n }\n\n if (!parser) {\n return defaultParser.parse(file, { extname: parseExtName })\n }\n\n return parser.parse(file, { extname: parseExtName })\n }\n\n async run(\n files: Array<KubbFile.ResolvedFile>,\n { parsers, mode = 'sequential', dryRun, extension }: ProcessFilesProps = {},\n ): Promise<KubbFile.ResolvedFile[]> {\n await this.events.emit('process:start', { files })\n\n let processed = 0\n const total = files.length\n\n const processOne = async (resolvedFile: KubbFile.ResolvedFile, index: number) => {\n const percentage = (processed / total) * 100\n\n await this.events.emit('file:start', { file: resolvedFile, index, total })\n\n const source = dryRun ? undefined : await this.parse(resolvedFile, { extension, parsers })\n\n await this.events.emit('process:progress', {\n file: resolvedFile,\n source,\n processed,\n percentage,\n total,\n })\n\n processed++\n\n await this.events.emit('file:end', { file: resolvedFile, index, total })\n }\n\n if (mode === 'sequential') {\n async function* asyncFiles() {\n for (let index = 0; index < files.length; index++) {\n yield [files[index], index] as const\n }\n }\n\n for await (const [file, index] of asyncFiles()) {\n if (file) {\n await processOne(file, index)\n }\n }\n } else {\n const promises = files.map((resolvedFile, index) => this.#limit(() => processOne(resolvedFile, index)))\n await Promise.all(promises)\n }\n\n await this.events.emit('process:end', { files })\n\n return files\n }\n}\n","import type * as KubbFile from './KubbFile.ts'\nimport { Cache } from './utils/Cache.ts'\nimport { trimExtName } from './utils/trimExtName.ts'\nimport { orderBy } from 'natural-orderby'\nimport { createFile } from './createFile.ts'\nimport { FileProcessor, type ProcessFilesProps } from './FileProcessor.ts'\nimport { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'\nimport type { AppEvents } from './App.ts'\n\nfunction mergeFile<TMeta extends object = object>(a: KubbFile.File<TMeta>, b: KubbFile.File<TMeta>): KubbFile.File<TMeta> {\n return {\n ...a,\n sources: [...(a.sources || []), ...(b.sources || [])],\n imports: [...(a.imports || []), ...(b.imports || [])],\n exports: [...(a.exports || []), ...(b.exports || [])],\n }\n}\n\ntype Options = {\n events?: AsyncEventEmitter<AppEvents>\n}\n\nexport class FileManager {\n #cache = new Cache<KubbFile.ResolvedFile>()\n events: AsyncEventEmitter<AppEvents>\n processor: FileProcessor\n\n constructor({ events = new AsyncEventEmitter<AppEvents>() }: Options = {}) {\n this.processor = new FileProcessor({ events })\n\n this.events = events\n return this\n }\n\n async add(...files: Array<KubbFile.File>) {\n const resolvedFiles: Array<KubbFile.ResolvedFile> = []\n\n const mergedFiles = new Map<string, KubbFile.File>()\n\n files.forEach((file) => {\n const existing = mergedFiles.get(file.path)\n if (existing) {\n mergedFiles.set(file.path, mergeFile(existing, file))\n } else {\n mergedFiles.set(file.path, file)\n }\n })\n\n for (const file of mergedFiles.values()) {\n const existing = this.#cache.get(file.path)\n\n const merged = existing ? mergeFile(existing, file) : file\n const resolvedFile = createFile(merged)\n\n this.#cache.set(resolvedFile.path, resolvedFile)\n this.flush()\n\n resolvedFiles.push(resolvedFile)\n }\n\n return resolvedFiles\n }\n\n flush() {\n this.#cache.flush()\n }\n\n getByPath(path: KubbFile.Path): KubbFile.ResolvedFile | null {\n return this.#cache.get(path)\n }\n\n deleteByPath(path: KubbFile.Path): void {\n this.#cache.delete(path)\n }\n\n clear(): void {\n this.#cache.clear()\n }\n\n get files(): Array<KubbFile.ResolvedFile> {\n const cachedKeys = this.#cache.keys()\n\n // order by path length and if file is a barrel file\n const keys = orderBy(cachedKeys, [(v) => v.length, (v) => trimExtName(v).endsWith('index')])\n\n const files = keys.map((key) => this.#cache.get(key))\n\n return files.filter(Boolean)\n }\n\n async write(options: ProcessFilesProps): Promise<KubbFile.ResolvedFile[]> {\n const resolvedFiles = await this.processor.run(this.files, options)\n\n this.clear()\n\n return resolvedFiles\n }\n}\n","import { FileManager } from './FileManager.ts'\nimport { isFunction } from 'remeda'\nimport type { Plugin } from './plugins/types.ts'\nimport type { Parser } from './parsers/types.ts'\nimport { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'\nimport type { AppContext, AppEvents, AppOptions } from './App.ts'\n\nimport type { App } from './index.ts'\n\ntype RootRenderFunction<TApp extends App> = (app: TApp) => void | Promise<void>\n\nexport type DefineApp<TOptions> = (options?: TOptions) => App\n\nexport function defineApp<TOptions extends AppOptions>(instance?: RootRenderFunction<App<TOptions>>): DefineApp<TOptions> {\n function createApp(options?: TOptions): App {\n const events = new AsyncEventEmitter<AppEvents>()\n const installedPlugins = new Set<Plugin<any>>()\n const installedParsers = new Set<Parser<any>>()\n const fileManager = new FileManager({ events })\n const context = {\n events,\n options,\n fileManager,\n installedPlugins,\n installedParsers,\n } as AppContext<TOptions>\n\n const app = {\n context,\n get files() {\n return fileManager.files\n },\n async addFile(...newFiles) {\n await fileManager.add(...newFiles)\n },\n async use(pluginOrParser, ...options) {\n const args = options\n\n if (pluginOrParser.type === 'plugin') {\n if (installedPlugins.has(pluginOrParser)) {\n console.warn('Plugin has already been applied to target app.')\n } else {\n installedPlugins.add(pluginOrParser)\n }\n\n if (pluginOrParser.inject && isFunction(pluginOrParser.inject)) {\n const injecter = pluginOrParser.inject\n\n const extraApp = (injecter as any)(app, ...args)\n Object.assign(app, extraApp)\n }\n }\n if (pluginOrParser.type === 'parser') {\n if (installedParsers.has(pluginOrParser)) {\n console.warn('Parser has already been applied to target app.')\n } else {\n installedParsers.add(pluginOrParser)\n }\n }\n\n if (pluginOrParser && isFunction(pluginOrParser.install)) {\n const installer = pluginOrParser.install\n\n await (installer as any)(app, ...args)\n }\n\n return app\n },\n } as App<TOptions>\n\n if (instance) {\n instance(app)\n }\n\n return app\n }\n\n return createApp\n}\n","import { defineApp } from './defineApp.ts'\n\nexport const createApp = defineApp()\n"],"x_google_ignoreList":[1],"mappings":";;;;;;;;;;;;;AAAA,IAAa,QAAb,MAAsB;;mFACV,IAAI,KAAgB;;CAE9B,IAAI,KAAuB;;AACzB,oFAAO,KAAY,CAAC,IAAI,IAAI,+DAAI;;CAGlC,IAAI,KAAa,OAAgB;AAC/B,8DAAY,CAAC,IAAI,KAAK,MAAM;;CAG9B,OAAO,KAAmB;AACxB,8DAAY,CAAC,OAAO,IAAI;;CAG1B,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CACtC,SAAQ,KAAK,iDAAiD;QAE9D,kBAAiB,IAAI,eAAe;AAIxC,QAAI,kBAAkBA,EAAW,eAAe,QAAQ,EAAE;KACxD,MAAM,YAAY,eAAe;AAEjC,WAAO,UAAkB,KAAK,GAAG,KAAK;;AAGxC,WAAO;;GAEV;AAED,MAAI,SACF,UAAS,IAAI;AAGf,SAAO;;AAGT,QAAOF;;;;;AC3ET,MAAa,YAAY,WAAW"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["e","NodeEventEmitter","defaultParser","parser: Parser | undefined","resolvedFiles: Array<KubbFile.ResolvedFile>","createFile","trimExtName","createApp","options","isFunction"],"sources":["../src/utils/Cache.ts","../../../node_modules/.pnpm/remeda@2.32.0/node_modules/remeda/dist/isFunction-BJjFuZR7.js","../src/utils/AsyncEventEmitter.ts","../src/FileProcessor.ts","../src/FileManager.ts","../src/defineApp.ts","../src/createApp.ts"],"sourcesContent":["export class Cache<T> {\n #buffer = new Map<string, T>()\n\n get(key: string): T | null {\n return this.#buffer.get(key) ?? null\n }\n\n set(key: string, value: T): void {\n this.#buffer.set(key, value)\n }\n\n delete(key: string): void {\n this.#buffer.delete(key)\n }\n\n clear(): void {\n this.#buffer.clear()\n }\n\n keys(): string[] {\n return [...this.#buffer.keys()]\n }\n\n values(): Array<T> {\n return [...this.#buffer.values()]\n }\n\n flush(): void {\n // No-op for base cache\n }\n}\n","const e=e=>typeof e==`function`;export{e as isFunction};\n//# sourceMappingURL=isFunction-BJjFuZR7.js.map","import { EventEmitter as NodeEventEmitter } from 'node:events'\n\nexport class AsyncEventEmitter<TEvents extends Record<string, any>> {\n constructor(maxListener = 100) {\n this.#emitter.setMaxListeners(maxListener)\n }\n #emitter = new NodeEventEmitter()\n\n async emit<TEventName extends keyof TEvents & string>(eventName: TEventName, ...eventArgs: TEvents[TEventName]): Promise<void> {\n const listeners = this.#emitter.listeners(eventName) as Array<(...args: TEvents[TEventName]) => any>\n\n if (listeners.length === 0) {\n return undefined\n }\n\n await Promise.all(\n listeners.map(async (listener) => {\n try {\n return await listener(...eventArgs)\n } catch (err) {\n console.error(`Error in async listener for \"${eventName}\":`, err)\n }\n }),\n )\n }\n\n on<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void {\n this.#emitter.on(eventName, handler as any)\n }\n\n onOnce<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArgs: TEvents[TEventName]) => void): void {\n const wrapper = (...args: TEvents[TEventName]) => {\n this.off(eventName, wrapper)\n handler(...args)\n }\n this.on(eventName, wrapper)\n }\n\n off<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void {\n this.#emitter.off(eventName, handler as any)\n }\n removeAll(): void {\n this.#emitter.removeAllListeners()\n }\n}\n","import type * as KubbFile from './KubbFile.ts'\nimport pLimit from 'p-limit'\n\nimport type { Parser } from './parsers/types.ts'\nimport { defaultParser } from './parsers/defaultParser.ts'\nimport { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'\nimport type { AppEvents, AppMode } from './App.ts'\n\nexport type ProcessFilesProps = {\n parsers?: Set<Parser>\n extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>\n dryRun?: boolean\n /**\n * @default 'sequential'\n */\n mode?: AppMode\n}\n\ntype GetParseOptions = {\n parsers?: Set<Parser>\n extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>\n}\n\ntype Options = {\n events?: AsyncEventEmitter<AppEvents>\n}\n\nexport class FileProcessor {\n #limit = pLimit(100)\n events: AsyncEventEmitter<AppEvents>\n\n constructor({ events = new AsyncEventEmitter<AppEvents>() }: Options = {}) {\n this.events = events\n\n return this\n }\n\n async parse(file: KubbFile.ResolvedFile, { parsers, extension }: GetParseOptions = {}): Promise<string> {\n const parseExtName = extension?.[file.extname] || undefined\n\n if (!parsers) {\n console.warn('No parsers provided, using default parser. If you want to use a specific parser, please provide it in the options.')\n\n return defaultParser.parse(file, { extname: parseExtName })\n }\n\n if (!file.extname) {\n return defaultParser.parse(file, { extname: parseExtName })\n }\n\n let parser: Parser | undefined\n for (const item of parsers) {\n if (item.extNames?.includes(file.extname)) {\n parser = item\n break\n }\n }\n\n if (!parser) {\n return defaultParser.parse(file, { extname: parseExtName })\n }\n\n return parser.parse(file, { extname: parseExtName })\n }\n\n async run(\n files: Array<KubbFile.ResolvedFile>,\n { parsers, mode = 'sequential', dryRun, extension }: ProcessFilesProps = {},\n ): Promise<KubbFile.ResolvedFile[]> {\n await this.events.emit('process:start', { files })\n\n let processed = 0\n const total = files.length\n\n const processOne = async (resolvedFile: KubbFile.ResolvedFile, index: number) => {\n const percentage = (processed / total) * 100\n\n await this.events.emit('file:start', { file: resolvedFile, index, total })\n\n const source = dryRun ? undefined : await this.parse(resolvedFile, { extension, parsers })\n\n await this.events.emit('process:progress', {\n file: resolvedFile,\n source,\n processed,\n percentage,\n total,\n })\n\n processed++\n\n await this.events.emit('file:end', { file: resolvedFile, index, total })\n }\n\n if (mode === 'sequential') {\n async function* asyncFiles() {\n for (let index = 0; index < files.length; index++) {\n yield [files[index], index] as const\n }\n }\n\n for await (const [file, index] of asyncFiles()) {\n if (file) {\n await processOne(file, index)\n }\n }\n } else {\n const promises = files.map((resolvedFile, index) => this.#limit(() => processOne(resolvedFile, index)))\n await Promise.all(promises)\n }\n\n await this.events.emit('process:end', { files })\n\n return files\n }\n}\n","import type * as KubbFile from './KubbFile.ts'\nimport { Cache } from './utils/Cache.ts'\nimport { trimExtName } from './utils/trimExtName.ts'\nimport { orderBy } from 'natural-orderby'\nimport { createFile } from './createFile.ts'\nimport { FileProcessor, type ProcessFilesProps } from './FileProcessor.ts'\nimport { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'\nimport type { AppEvents } from './App.ts'\n\nfunction mergeFile<TMeta extends object = object>(a: KubbFile.File<TMeta>, b: KubbFile.File<TMeta>): KubbFile.File<TMeta> {\n return {\n ...a,\n sources: [...(a.sources || []), ...(b.sources || [])],\n imports: [...(a.imports || []), ...(b.imports || [])],\n exports: [...(a.exports || []), ...(b.exports || [])],\n }\n}\n\ntype Options = {\n events?: AsyncEventEmitter<AppEvents>\n}\n\nexport class FileManager {\n #cache = new Cache<KubbFile.ResolvedFile>()\n events: AsyncEventEmitter<AppEvents>\n processor: FileProcessor\n\n constructor({ events = new AsyncEventEmitter<AppEvents>() }: Options = {}) {\n this.processor = new FileProcessor({ events })\n\n this.events = events\n return this\n }\n\n async add(...files: Array<KubbFile.File>) {\n const resolvedFiles: Array<KubbFile.ResolvedFile> = []\n\n const mergedFiles = new Map<string, KubbFile.File>()\n\n files.forEach((file) => {\n const existing = mergedFiles.get(file.path)\n if (existing) {\n mergedFiles.set(file.path, mergeFile(existing, file))\n } else {\n mergedFiles.set(file.path, file)\n }\n })\n\n for (const file of mergedFiles.values()) {\n const existing = this.#cache.get(file.path)\n\n const merged = existing ? mergeFile(existing, file) : file\n const resolvedFile = createFile(merged)\n\n this.#cache.set(resolvedFile.path, resolvedFile)\n this.flush()\n\n resolvedFiles.push(resolvedFile)\n }\n\n await this.events.emit('file:add', { files: resolvedFiles })\n\n return resolvedFiles\n }\n\n flush() {\n this.#cache.flush()\n }\n\n getByPath(path: KubbFile.Path): KubbFile.ResolvedFile | null {\n return this.#cache.get(path)\n }\n\n deleteByPath(path: KubbFile.Path): void {\n this.#cache.delete(path)\n }\n\n clear(): void {\n this.#cache.clear()\n }\n\n get files(): Array<KubbFile.ResolvedFile> {\n const cachedKeys = this.#cache.keys()\n\n // order by path length and if file is a barrel file\n const keys = orderBy(cachedKeys, [(v) => v.length, (v) => trimExtName(v).endsWith('index')])\n\n const files = keys.map((key) => this.#cache.get(key))\n\n return files.filter(Boolean)\n }\n\n //TODO add test and check if write of FileManager contains the newly added file\n async write(options: ProcessFilesProps): Promise<KubbFile.ResolvedFile[]> {\n await this.events.emit('write:start', { files: this.files })\n\n const resolvedFiles = await this.processor.run(this.files, options)\n\n this.clear()\n\n await this.events.emit('write:end', { files: resolvedFiles })\n\n return resolvedFiles\n }\n}\n","import { FileManager } from './FileManager.ts'\nimport { isFunction } from 'remeda'\nimport type { Plugin } from './plugins/types.ts'\nimport type { Parser } from './parsers/types.ts'\nimport { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'\nimport type { AppContext, AppEvents, AppOptions } from './App.ts'\n\nimport type { App } from './index.ts'\n\ntype RootRenderFunction<TApp extends App> = (app: TApp) => void | Promise<void>\n\nexport type DefineApp<TOptions> = (options?: TOptions) => App\n\nexport function defineApp<TOptions extends AppOptions>(instance?: RootRenderFunction<App<TOptions>>): DefineApp<TOptions> {\n function createApp(options?: TOptions): App {\n const events = new AsyncEventEmitter<AppEvents>()\n const installedPlugins = new Set<Plugin<any>>()\n const installedParsers = new Set<Parser<any>>()\n const fileManager = new FileManager({ events })\n const context = {\n events,\n options,\n fileManager,\n installedPlugins,\n installedParsers,\n } as AppContext<TOptions>\n\n const app = {\n context,\n get files() {\n return fileManager.files\n },\n async addFile(...newFiles) {\n await fileManager.add(...newFiles)\n },\n async use(pluginOrParser, ...options) {\n const args = options\n\n if (pluginOrParser.type === 'plugin') {\n if (installedPlugins.has(pluginOrParser)) {\n console.warn('Plugin has already been applied to target app.')\n } else {\n installedPlugins.add(pluginOrParser)\n }\n\n if (pluginOrParser.inject && isFunction(pluginOrParser.inject)) {\n const injecter = pluginOrParser.inject\n\n const extraApp = (injecter as any)(app, ...args)\n Object.assign(app, extraApp)\n }\n }\n if (pluginOrParser.type === 'parser') {\n if (installedParsers.has(pluginOrParser)) {\n console.warn('Parser has already been applied to target app.')\n } else {\n installedParsers.add(pluginOrParser)\n }\n }\n\n if (pluginOrParser && isFunction(pluginOrParser.install)) {\n const installer = pluginOrParser.install\n\n await (installer as any)(app, ...args)\n }\n\n return app\n },\n } as App<TOptions>\n\n if (instance) {\n instance(app)\n }\n\n return app\n }\n\n return createApp\n}\n","import { defineApp } from './defineApp.ts'\n\nexport const createApp = defineApp()\n"],"x_google_ignoreList":[1],"mappings":";;;;;;;;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AAG,UAAU;AACzB,UAAM,YAAY,IAAI,GAAG,SAAS;;GAEpC,MAAM,IAAI,gBAAgB,GAAGC,WAAS;IACpC,MAAM,OAAOA;AAEb,QAAI,eAAe,SAAS,UAAU;AACpC,SAAI,iBAAiB,IAAI,eAAe,CACtC,SAAQ,KAAK,iDAAiD;SAE9D,kBAAiB,IAAI,eAAe;AAGtC,SAAI,eAAe,UAAUC,EAAW,eAAe,OAAO,EAAE;MAC9D,MAAM,WAAW,eAAe;MAEhC,MAAM,WAAY,SAAiB,KAAK,GAAG,KAAK;AAChD,aAAO,OAAO,KAAK,SAAS;;;AAGhC,QAAI,eAAe,SAAS,SAC1B,KAAI,iBAAiB,IAAI,eAAe,CACtC,SAAQ,KAAK,iDAAiD;QAE9D,kBAAiB,IAAI,eAAe;AAIxC,QAAI,kBAAkBA,EAAW,eAAe,QAAQ,EAAE;KACxD,MAAM,YAAY,eAAe;AAEjC,WAAO,UAAkB,KAAK,GAAG,KAAK;;AAGxC,WAAO;;GAEV;AAED,MAAI,SACF,UAAS,IAAI;AAGf,SAAO;;AAGT,QAAOF;;;;;AC3ET,MAAa,YAAY,WAAW"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as FileProcessor, i as FileManager, t as App } from "./App-
|
|
2
|
-
import { i as defineApp, n as createApp, t as createFile } from "./index-
|
|
1
|
+
import { a as FileProcessor, i as FileManager, t as App } from "./App-Cjd-lGfW.cjs";
|
|
2
|
+
import { i as defineApp, n as createApp, t as createFile } from "./index-BUERYeq7.cjs";
|
|
3
3
|
export { App, FileManager, FileProcessor, createApp, createFile, defineApp };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as FileProcessor, i as FileManager, t as App } from "./App-
|
|
2
|
-
import { i as defineApp, n as createApp, t as createFile } from "./index-
|
|
1
|
+
import { a as FileProcessor, i as FileManager, t as App } from "./App-ztRQpZS9.js";
|
|
2
|
+
import { i as defineApp, n as createApp, t as createFile } from "./index-DfaD7Pj_.js";
|
|
3
3
|
export { App, FileManager, FileProcessor, createApp, createFile, defineApp };
|
package/dist/index.js
CHANGED
|
@@ -181,6 +181,7 @@ var FileManager = class {
|
|
|
181
181
|
this.flush();
|
|
182
182
|
resolvedFiles.push(resolvedFile);
|
|
183
183
|
}
|
|
184
|
+
await this.events.emit("file:add", { files: resolvedFiles });
|
|
184
185
|
return resolvedFiles;
|
|
185
186
|
}
|
|
186
187
|
flush() {
|
|
@@ -199,8 +200,10 @@ var FileManager = class {
|
|
|
199
200
|
return orderBy(_classPrivateFieldGet2(_cache, this).keys(), [(v) => v.length, (v) => trimExtName(v).endsWith("index")]).map((key) => _classPrivateFieldGet2(_cache, this).get(key)).filter(Boolean);
|
|
200
201
|
}
|
|
201
202
|
async write(options) {
|
|
203
|
+
await this.events.emit("write:start", { files: this.files });
|
|
202
204
|
const resolvedFiles = await this.processor.run(this.files, options);
|
|
203
205
|
this.clear();
|
|
206
|
+
await this.events.emit("write:end", { files: resolvedFiles });
|
|
204
207
|
return resolvedFiles;
|
|
205
208
|
}
|
|
206
209
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["e","NodeEventEmitter","parser: Parser | undefined","resolvedFiles: Array<KubbFile.ResolvedFile>","createApp","options","isFunction"],"sources":["../src/utils/Cache.ts","../../../node_modules/.pnpm/remeda@2.32.0/node_modules/remeda/dist/isFunction-BJjFuZR7.js","../src/utils/AsyncEventEmitter.ts","../src/FileProcessor.ts","../src/FileManager.ts","../src/defineApp.ts","../src/createApp.ts"],"sourcesContent":["export class Cache<T> {\n #buffer = new Map<string, T>()\n\n get(key: string): T | null {\n return this.#buffer.get(key) ?? null\n }\n\n set(key: string, value: T): void {\n this.#buffer.set(key, value)\n }\n\n delete(key: string): void {\n this.#buffer.delete(key)\n }\n\n clear(): void {\n this.#buffer.clear()\n }\n\n keys(): string[] {\n return [...this.#buffer.keys()]\n }\n\n values(): Array<T> {\n return [...this.#buffer.values()]\n }\n\n flush(): void {\n // No-op for base cache\n }\n}\n","const e=e=>typeof e==`function`;export{e as isFunction};\n//# sourceMappingURL=isFunction-BJjFuZR7.js.map","import { EventEmitter as NodeEventEmitter } from 'node:events'\n\nexport class AsyncEventEmitter<TEvents extends Record<string, any>> {\n constructor(maxListener = 100) {\n this.#emitter.setMaxListeners(maxListener)\n }\n #emitter = new NodeEventEmitter()\n\n async emit<TEventName extends keyof TEvents & string>(eventName: TEventName, ...eventArgs: TEvents[TEventName]): Promise<void> {\n const listeners = this.#emitter.listeners(eventName) as Array<(...args: TEvents[TEventName]) => any>\n\n if (listeners.length === 0) {\n return undefined\n }\n\n await Promise.all(\n listeners.map(async (listener) => {\n try {\n return await listener(...eventArgs)\n } catch (err) {\n console.error(`Error in async listener for \"${eventName}\":`, err)\n }\n }),\n )\n }\n\n on<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void {\n this.#emitter.on(eventName, handler as any)\n }\n\n onOnce<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArgs: TEvents[TEventName]) => void): void {\n const wrapper = (...args: TEvents[TEventName]) => {\n this.off(eventName, wrapper)\n handler(...args)\n }\n this.on(eventName, wrapper)\n }\n\n off<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void {\n this.#emitter.off(eventName, handler as any)\n }\n removeAll(): void {\n this.#emitter.removeAllListeners()\n }\n}\n","import type * as KubbFile from './KubbFile.ts'\nimport pLimit from 'p-limit'\n\nimport type { Parser } from './parsers/types.ts'\nimport { defaultParser } from './parsers/defaultParser.ts'\nimport { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'\nimport type { AppEvents, AppMode } from './App.ts'\n\nexport type ProcessFilesProps = {\n parsers?: Set<Parser>\n extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>\n dryRun?: boolean\n /**\n * @default 'sequential'\n */\n mode?: AppMode\n}\n\ntype GetParseOptions = {\n parsers?: Set<Parser>\n extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>\n}\n\ntype Options = {\n events?: AsyncEventEmitter<AppEvents>\n}\n\nexport class FileProcessor {\n #limit = pLimit(100)\n events: AsyncEventEmitter<AppEvents>\n\n constructor({ events = new AsyncEventEmitter<AppEvents>() }: Options = {}) {\n this.events = events\n\n return this\n }\n\n async parse(file: KubbFile.ResolvedFile, { parsers, extension }: GetParseOptions = {}): Promise<string> {\n const parseExtName = extension?.[file.extname] || undefined\n\n if (!parsers) {\n console.warn('No parsers provided, using default parser. If you want to use a specific parser, please provide it in the options.')\n\n return defaultParser.parse(file, { extname: parseExtName })\n }\n\n if (!file.extname) {\n return defaultParser.parse(file, { extname: parseExtName })\n }\n\n let parser: Parser | undefined\n for (const item of parsers) {\n if (item.extNames?.includes(file.extname)) {\n parser = item\n break\n }\n }\n\n if (!parser) {\n return defaultParser.parse(file, { extname: parseExtName })\n }\n\n return parser.parse(file, { extname: parseExtName })\n }\n\n async run(\n files: Array<KubbFile.ResolvedFile>,\n { parsers, mode = 'sequential', dryRun, extension }: ProcessFilesProps = {},\n ): Promise<KubbFile.ResolvedFile[]> {\n await this.events.emit('process:start', { files })\n\n let processed = 0\n const total = files.length\n\n const processOne = async (resolvedFile: KubbFile.ResolvedFile, index: number) => {\n const percentage = (processed / total) * 100\n\n await this.events.emit('file:start', { file: resolvedFile, index, total })\n\n const source = dryRun ? undefined : await this.parse(resolvedFile, { extension, parsers })\n\n await this.events.emit('process:progress', {\n file: resolvedFile,\n source,\n processed,\n percentage,\n total,\n })\n\n processed++\n\n await this.events.emit('file:end', { file: resolvedFile, index, total })\n }\n\n if (mode === 'sequential') {\n async function* asyncFiles() {\n for (let index = 0; index < files.length; index++) {\n yield [files[index], index] as const\n }\n }\n\n for await (const [file, index] of asyncFiles()) {\n if (file) {\n await processOne(file, index)\n }\n }\n } else {\n const promises = files.map((resolvedFile, index) => this.#limit(() => processOne(resolvedFile, index)))\n await Promise.all(promises)\n }\n\n await this.events.emit('process:end', { files })\n\n return files\n }\n}\n","import type * as KubbFile from './KubbFile.ts'\nimport { Cache } from './utils/Cache.ts'\nimport { trimExtName } from './utils/trimExtName.ts'\nimport { orderBy } from 'natural-orderby'\nimport { createFile } from './createFile.ts'\nimport { FileProcessor, type ProcessFilesProps } from './FileProcessor.ts'\nimport { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'\nimport type { AppEvents } from './App.ts'\n\nfunction mergeFile<TMeta extends object = object>(a: KubbFile.File<TMeta>, b: KubbFile.File<TMeta>): KubbFile.File<TMeta> {\n return {\n ...a,\n sources: [...(a.sources || []), ...(b.sources || [])],\n imports: [...(a.imports || []), ...(b.imports || [])],\n exports: [...(a.exports || []), ...(b.exports || [])],\n }\n}\n\ntype Options = {\n events?: AsyncEventEmitter<AppEvents>\n}\n\nexport class FileManager {\n #cache = new Cache<KubbFile.ResolvedFile>()\n events: AsyncEventEmitter<AppEvents>\n processor: FileProcessor\n\n constructor({ events = new AsyncEventEmitter<AppEvents>() }: Options = {}) {\n this.processor = new FileProcessor({ events })\n\n this.events = events\n return this\n }\n\n async add(...files: Array<KubbFile.File>) {\n const resolvedFiles: Array<KubbFile.ResolvedFile> = []\n\n const mergedFiles = new Map<string, KubbFile.File>()\n\n files.forEach((file) => {\n const existing = mergedFiles.get(file.path)\n if (existing) {\n mergedFiles.set(file.path, mergeFile(existing, file))\n } else {\n mergedFiles.set(file.path, file)\n }\n })\n\n for (const file of mergedFiles.values()) {\n const existing = this.#cache.get(file.path)\n\n const merged = existing ? mergeFile(existing, file) : file\n const resolvedFile = createFile(merged)\n\n this.#cache.set(resolvedFile.path, resolvedFile)\n this.flush()\n\n resolvedFiles.push(resolvedFile)\n }\n\n return resolvedFiles\n }\n\n flush() {\n this.#cache.flush()\n }\n\n getByPath(path: KubbFile.Path): KubbFile.ResolvedFile | null {\n return this.#cache.get(path)\n }\n\n deleteByPath(path: KubbFile.Path): void {\n this.#cache.delete(path)\n }\n\n clear(): void {\n this.#cache.clear()\n }\n\n get files(): Array<KubbFile.ResolvedFile> {\n const cachedKeys = this.#cache.keys()\n\n // order by path length and if file is a barrel file\n const keys = orderBy(cachedKeys, [(v) => v.length, (v) => trimExtName(v).endsWith('index')])\n\n const files = keys.map((key) => this.#cache.get(key))\n\n return files.filter(Boolean)\n }\n\n async write(options: ProcessFilesProps): Promise<KubbFile.ResolvedFile[]> {\n const resolvedFiles = await this.processor.run(this.files, options)\n\n this.clear()\n\n return resolvedFiles\n }\n}\n","import { FileManager } from './FileManager.ts'\nimport { isFunction } from 'remeda'\nimport type { Plugin } from './plugins/types.ts'\nimport type { Parser } from './parsers/types.ts'\nimport { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'\nimport type { AppContext, AppEvents, AppOptions } from './App.ts'\n\nimport type { App } from './index.ts'\n\ntype RootRenderFunction<TApp extends App> = (app: TApp) => void | Promise<void>\n\nexport type DefineApp<TOptions> = (options?: TOptions) => App\n\nexport function defineApp<TOptions extends AppOptions>(instance?: RootRenderFunction<App<TOptions>>): DefineApp<TOptions> {\n function createApp(options?: TOptions): App {\n const events = new AsyncEventEmitter<AppEvents>()\n const installedPlugins = new Set<Plugin<any>>()\n const installedParsers = new Set<Parser<any>>()\n const fileManager = new FileManager({ events })\n const context = {\n events,\n options,\n fileManager,\n installedPlugins,\n installedParsers,\n } as AppContext<TOptions>\n\n const app = {\n context,\n get files() {\n return fileManager.files\n },\n async addFile(...newFiles) {\n await fileManager.add(...newFiles)\n },\n async use(pluginOrParser, ...options) {\n const args = options\n\n if (pluginOrParser.type === 'plugin') {\n if (installedPlugins.has(pluginOrParser)) {\n console.warn('Plugin has already been applied to target app.')\n } else {\n installedPlugins.add(pluginOrParser)\n }\n\n if (pluginOrParser.inject && isFunction(pluginOrParser.inject)) {\n const injecter = pluginOrParser.inject\n\n const extraApp = (injecter as any)(app, ...args)\n Object.assign(app, extraApp)\n }\n }\n if (pluginOrParser.type === 'parser') {\n if (installedParsers.has(pluginOrParser)) {\n console.warn('Parser has already been applied to target app.')\n } else {\n installedParsers.add(pluginOrParser)\n }\n }\n\n if (pluginOrParser && isFunction(pluginOrParser.install)) {\n const installer = pluginOrParser.install\n\n await (installer as any)(app, ...args)\n }\n\n return app\n },\n } as App<TOptions>\n\n if (instance) {\n instance(app)\n }\n\n return app\n }\n\n return createApp\n}\n","import { defineApp } from './defineApp.ts'\n\nexport const createApp = defineApp()\n"],"x_google_ignoreList":[1],"mappings":";;;;;;;;;;AAAA,IAAa,QAAb,MAAsB;;4DACV,IAAI,KAAgB;;CAE9B,IAAI,KAAuB;;AACzB,6DAAO,KAAY,CAAC,IAAI,IAAI,+DAAI;;CAGlC,IAAI,KAAa,OAAgB;AAC/B,uCAAY,CAAC,IAAI,KAAK,MAAM;;CAG9B,OAAO,KAAmB;AACxB,uCAAY,CAAC,OAAO,IAAI;;CAG1B,QAAc;AACZ,uCAAY,CAAC,OAAO;;CAGtB,OAAiB;AACf,SAAO,CAAC,mCAAG,KAAY,CAAC,MAAM,CAAC;;CAGjC,SAAmB;AACjB,SAAO,CAAC,mCAAG,KAAY,CAAC,QAAQ,CAAC;;CAGnC,QAAc;;;;;AC3BhB,MAAM,KAAE,QAAG,OAAOA,OAAG;;;;;ACErB,IAAa,oBAAb,MAAoE;CAClE,YAAY,cAAc,KAAK;6CAGpB,IAAIC,cAAkB;AAF/B,wCAAa,CAAC,gBAAgB,YAAY;;CAI5C,MAAM,KAAgD,WAAuB,GAAG,WAA+C;EAC7H,MAAM,6CAAY,KAAa,CAAC,UAAU,UAAU;AAEpD,MAAI,UAAU,WAAW,EACvB;AAGF,QAAM,QAAQ,IACZ,UAAU,IAAI,OAAO,aAAa;AAChC,OAAI;AACF,WAAO,MAAM,SAAS,GAAG,UAAU;YAC5B,KAAK;AACZ,YAAQ,MAAM,gCAAgC,UAAU,KAAK,IAAI;;IAEnE,CACH;;CAGH,GAA8C,WAAuB,SAA2D;AAC9H,wCAA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AE,QAAQ,CAAC;EAS/C,MAAM,MAAM;GACV,SATc;IACd;IACA;IACA;IACA;IACA;IACD;GAIC,IAAI,QAAQ;AACV,WAAO,YAAY;;GAErB,MAAM,QAAQ,GAAG,UAAU;AACzB,UAAM,YAAY,IAAI,GAAG,SAAS;;GAEpC,MAAM,IAAI,gBAAgB,GAAGC,WAAS;IACpC,MAAM,OAAOA;AAEb,QAAI,eAAe,SAAS,UAAU;AACpC,SAAI,iBAAiB,IAAI,eAAe,CACtC,SAAQ,KAAK,iDAAiD;SAE9D,kBAAiB,IAAI,eAAe;AAGtC,SAAI,eAAe,UAAUC,EAAW,eAAe,OAAO,EAAE;MAC9D,MAAM,WAAW,eAAe;MAEhC,MAAM,WAAY,SAAiB,KAAK,GAAG,KAAK;AAChD,aAAO,OAAO,KAAK,SAAS;;;AAGhC,QAAI,eAAe,SAAS,SAC1B,KAAI,iBAAiB,IAAI,eAAe,CACtC,SAAQ,KAAK,iDAAiD;QAE9D,kBAAiB,IAAI,eAAe;AAIxC,QAAI,kBAAkBA,EAAW,eAAe,QAAQ,EAAE;KACxD,MAAM,YAAY,eAAe;AAEjC,WAAO,UAAkB,KAAK,GAAG,KAAK;;AAGxC,WAAO;;GAEV;AAED,MAAI,SACF,UAAS,IAAI;AAGf,SAAO;;AAGT,QAAOF;;;;;AC3ET,MAAa,YAAY,WAAW"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["e","NodeEventEmitter","parser: Parser | undefined","resolvedFiles: Array<KubbFile.ResolvedFile>","createApp","options","isFunction"],"sources":["../src/utils/Cache.ts","../../../node_modules/.pnpm/remeda@2.32.0/node_modules/remeda/dist/isFunction-BJjFuZR7.js","../src/utils/AsyncEventEmitter.ts","../src/FileProcessor.ts","../src/FileManager.ts","../src/defineApp.ts","../src/createApp.ts"],"sourcesContent":["export class Cache<T> {\n #buffer = new Map<string, T>()\n\n get(key: string): T | null {\n return this.#buffer.get(key) ?? null\n }\n\n set(key: string, value: T): void {\n this.#buffer.set(key, value)\n }\n\n delete(key: string): void {\n this.#buffer.delete(key)\n }\n\n clear(): void {\n this.#buffer.clear()\n }\n\n keys(): string[] {\n return [...this.#buffer.keys()]\n }\n\n values(): Array<T> {\n return [...this.#buffer.values()]\n }\n\n flush(): void {\n // No-op for base cache\n }\n}\n","const e=e=>typeof e==`function`;export{e as isFunction};\n//# sourceMappingURL=isFunction-BJjFuZR7.js.map","import { EventEmitter as NodeEventEmitter } from 'node:events'\n\nexport class AsyncEventEmitter<TEvents extends Record<string, any>> {\n constructor(maxListener = 100) {\n this.#emitter.setMaxListeners(maxListener)\n }\n #emitter = new NodeEventEmitter()\n\n async emit<TEventName extends keyof TEvents & string>(eventName: TEventName, ...eventArgs: TEvents[TEventName]): Promise<void> {\n const listeners = this.#emitter.listeners(eventName) as Array<(...args: TEvents[TEventName]) => any>\n\n if (listeners.length === 0) {\n return undefined\n }\n\n await Promise.all(\n listeners.map(async (listener) => {\n try {\n return await listener(...eventArgs)\n } catch (err) {\n console.error(`Error in async listener for \"${eventName}\":`, err)\n }\n }),\n )\n }\n\n on<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void {\n this.#emitter.on(eventName, handler as any)\n }\n\n onOnce<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArgs: TEvents[TEventName]) => void): void {\n const wrapper = (...args: TEvents[TEventName]) => {\n this.off(eventName, wrapper)\n handler(...args)\n }\n this.on(eventName, wrapper)\n }\n\n off<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void {\n this.#emitter.off(eventName, handler as any)\n }\n removeAll(): void {\n this.#emitter.removeAllListeners()\n }\n}\n","import type * as KubbFile from './KubbFile.ts'\nimport pLimit from 'p-limit'\n\nimport type { Parser } from './parsers/types.ts'\nimport { defaultParser } from './parsers/defaultParser.ts'\nimport { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'\nimport type { AppEvents, AppMode } from './App.ts'\n\nexport type ProcessFilesProps = {\n parsers?: Set<Parser>\n extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>\n dryRun?: boolean\n /**\n * @default 'sequential'\n */\n mode?: AppMode\n}\n\ntype GetParseOptions = {\n parsers?: Set<Parser>\n extension?: Record<KubbFile.Extname, KubbFile.Extname | ''>\n}\n\ntype Options = {\n events?: AsyncEventEmitter<AppEvents>\n}\n\nexport class FileProcessor {\n #limit = pLimit(100)\n events: AsyncEventEmitter<AppEvents>\n\n constructor({ events = new AsyncEventEmitter<AppEvents>() }: Options = {}) {\n this.events = events\n\n return this\n }\n\n async parse(file: KubbFile.ResolvedFile, { parsers, extension }: GetParseOptions = {}): Promise<string> {\n const parseExtName = extension?.[file.extname] || undefined\n\n if (!parsers) {\n console.warn('No parsers provided, using default parser. If you want to use a specific parser, please provide it in the options.')\n\n return defaultParser.parse(file, { extname: parseExtName })\n }\n\n if (!file.extname) {\n return defaultParser.parse(file, { extname: parseExtName })\n }\n\n let parser: Parser | undefined\n for (const item of parsers) {\n if (item.extNames?.includes(file.extname)) {\n parser = item\n break\n }\n }\n\n if (!parser) {\n return defaultParser.parse(file, { extname: parseExtName })\n }\n\n return parser.parse(file, { extname: parseExtName })\n }\n\n async run(\n files: Array<KubbFile.ResolvedFile>,\n { parsers, mode = 'sequential', dryRun, extension }: ProcessFilesProps = {},\n ): Promise<KubbFile.ResolvedFile[]> {\n await this.events.emit('process:start', { files })\n\n let processed = 0\n const total = files.length\n\n const processOne = async (resolvedFile: KubbFile.ResolvedFile, index: number) => {\n const percentage = (processed / total) * 100\n\n await this.events.emit('file:start', { file: resolvedFile, index, total })\n\n const source = dryRun ? undefined : await this.parse(resolvedFile, { extension, parsers })\n\n await this.events.emit('process:progress', {\n file: resolvedFile,\n source,\n processed,\n percentage,\n total,\n })\n\n processed++\n\n await this.events.emit('file:end', { file: resolvedFile, index, total })\n }\n\n if (mode === 'sequential') {\n async function* asyncFiles() {\n for (let index = 0; index < files.length; index++) {\n yield [files[index], index] as const\n }\n }\n\n for await (const [file, index] of asyncFiles()) {\n if (file) {\n await processOne(file, index)\n }\n }\n } else {\n const promises = files.map((resolvedFile, index) => this.#limit(() => processOne(resolvedFile, index)))\n await Promise.all(promises)\n }\n\n await this.events.emit('process:end', { files })\n\n return files\n }\n}\n","import type * as KubbFile from './KubbFile.ts'\nimport { Cache } from './utils/Cache.ts'\nimport { trimExtName } from './utils/trimExtName.ts'\nimport { orderBy } from 'natural-orderby'\nimport { createFile } from './createFile.ts'\nimport { FileProcessor, type ProcessFilesProps } from './FileProcessor.ts'\nimport { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'\nimport type { AppEvents } from './App.ts'\n\nfunction mergeFile<TMeta extends object = object>(a: KubbFile.File<TMeta>, b: KubbFile.File<TMeta>): KubbFile.File<TMeta> {\n return {\n ...a,\n sources: [...(a.sources || []), ...(b.sources || [])],\n imports: [...(a.imports || []), ...(b.imports || [])],\n exports: [...(a.exports || []), ...(b.exports || [])],\n }\n}\n\ntype Options = {\n events?: AsyncEventEmitter<AppEvents>\n}\n\nexport class FileManager {\n #cache = new Cache<KubbFile.ResolvedFile>()\n events: AsyncEventEmitter<AppEvents>\n processor: FileProcessor\n\n constructor({ events = new AsyncEventEmitter<AppEvents>() }: Options = {}) {\n this.processor = new FileProcessor({ events })\n\n this.events = events\n return this\n }\n\n async add(...files: Array<KubbFile.File>) {\n const resolvedFiles: Array<KubbFile.ResolvedFile> = []\n\n const mergedFiles = new Map<string, KubbFile.File>()\n\n files.forEach((file) => {\n const existing = mergedFiles.get(file.path)\n if (existing) {\n mergedFiles.set(file.path, mergeFile(existing, file))\n } else {\n mergedFiles.set(file.path, file)\n }\n })\n\n for (const file of mergedFiles.values()) {\n const existing = this.#cache.get(file.path)\n\n const merged = existing ? mergeFile(existing, file) : file\n const resolvedFile = createFile(merged)\n\n this.#cache.set(resolvedFile.path, resolvedFile)\n this.flush()\n\n resolvedFiles.push(resolvedFile)\n }\n\n await this.events.emit('file:add', { files: resolvedFiles })\n\n return resolvedFiles\n }\n\n flush() {\n this.#cache.flush()\n }\n\n getByPath(path: KubbFile.Path): KubbFile.ResolvedFile | null {\n return this.#cache.get(path)\n }\n\n deleteByPath(path: KubbFile.Path): void {\n this.#cache.delete(path)\n }\n\n clear(): void {\n this.#cache.clear()\n }\n\n get files(): Array<KubbFile.ResolvedFile> {\n const cachedKeys = this.#cache.keys()\n\n // order by path length and if file is a barrel file\n const keys = orderBy(cachedKeys, [(v) => v.length, (v) => trimExtName(v).endsWith('index')])\n\n const files = keys.map((key) => this.#cache.get(key))\n\n return files.filter(Boolean)\n }\n\n //TODO add test and check if write of FileManager contains the newly added file\n async write(options: ProcessFilesProps): Promise<KubbFile.ResolvedFile[]> {\n await this.events.emit('write:start', { files: this.files })\n\n const resolvedFiles = await this.processor.run(this.files, options)\n\n this.clear()\n\n await this.events.emit('write:end', { files: resolvedFiles })\n\n return resolvedFiles\n }\n}\n","import { FileManager } from './FileManager.ts'\nimport { isFunction } from 'remeda'\nimport type { Plugin } from './plugins/types.ts'\nimport type { Parser } from './parsers/types.ts'\nimport { AsyncEventEmitter } from './utils/AsyncEventEmitter.ts'\nimport type { AppContext, AppEvents, AppOptions } from './App.ts'\n\nimport type { App } from './index.ts'\n\ntype RootRenderFunction<TApp extends App> = (app: TApp) => void | Promise<void>\n\nexport type DefineApp<TOptions> = (options?: TOptions) => App\n\nexport function defineApp<TOptions extends AppOptions>(instance?: RootRenderFunction<App<TOptions>>): DefineApp<TOptions> {\n function createApp(options?: TOptions): App {\n const events = new AsyncEventEmitter<AppEvents>()\n const installedPlugins = new Set<Plugin<any>>()\n const installedParsers = new Set<Parser<any>>()\n const fileManager = new FileManager({ events })\n const context = {\n events,\n options,\n fileManager,\n installedPlugins,\n installedParsers,\n } as AppContext<TOptions>\n\n const app = {\n context,\n get files() {\n return fileManager.files\n },\n async addFile(...newFiles) {\n await fileManager.add(...newFiles)\n },\n async use(pluginOrParser, ...options) {\n const args = options\n\n if (pluginOrParser.type === 'plugin') {\n if (installedPlugins.has(pluginOrParser)) {\n console.warn('Plugin has already been applied to target app.')\n } else {\n installedPlugins.add(pluginOrParser)\n }\n\n if (pluginOrParser.inject && isFunction(pluginOrParser.inject)) {\n const injecter = pluginOrParser.inject\n\n const extraApp = (injecter as any)(app, ...args)\n Object.assign(app, extraApp)\n }\n }\n if (pluginOrParser.type === 'parser') {\n if (installedParsers.has(pluginOrParser)) {\n console.warn('Parser has already been applied to target app.')\n } else {\n installedParsers.add(pluginOrParser)\n }\n }\n\n if (pluginOrParser && isFunction(pluginOrParser.install)) {\n const installer = pluginOrParser.install\n\n await (installer as any)(app, ...args)\n }\n\n return app\n },\n } as App<TOptions>\n\n if (instance) {\n instance(app)\n }\n\n return app\n }\n\n return createApp\n}\n","import { defineApp } from './defineApp.ts'\n\nexport const createApp = defineApp()\n"],"x_google_ignoreList":[1],"mappings":";;;;;;;;;;AAAA,IAAa,QAAb,MAAsB;;4DACV,IAAI,KAAgB;;CAE9B,IAAI,KAAuB;;AACzB,6DAAO,KAAY,CAAC,IAAI,IAAI,+DAAI;;CAGlC,IAAI,KAAa,OAAgB;AAC/B,uCAAY,CAAC,IAAI,KAAK,MAAM;;CAG9B,OAAO,KAAmB;AACxB,uCAAY,CAAC,OAAO,IAAI;;CAG1B,QAAc;AACZ,uCAAY,CAAC,OAAO;;CAGtB,OAAiB;AACf,SAAO,CAAC,mCAAG,KAAY,CAAC,MAAM,CAAC;;CAGjC,SAAmB;AACjB,SAAO,CAAC,mCAAG,KAAY,CAAC,QAAQ,CAAC;;CAGnC,QAAc;;;;;AC3BhB,MAAM,KAAE,QAAG,OAAOA,OAAG;;;;;ACErB,IAAa,oBAAb,MAAoE;CAClE,YAAY,cAAc,KAAK;6CAGpB,IAAIC,cAAkB;AAF/B,wCAAa,CAAC,gBAAgB,YAAY;;CAI5C,MAAM,KAAgD,WAAuB,GAAG,WAA+C;EAC7H,MAAM,6CAAY,KAAa,CAAC,UAAU,UAAU;AAEpD,MAAI,UAAU,W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ET,MAAa,YAAY,WAAW"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import "../App-
|
|
2
|
-
import { i as typescriptParser, n as createImport, r as print, t as createExport } from "../typescriptParser-
|
|
1
|
+
import "../App-Cjd-lGfW.cjs";
|
|
2
|
+
import { i as typescriptParser, n as createImport, r as print, t as createExport } from "../typescriptParser-CBt00C7L.cjs";
|
|
3
3
|
export { createExport, createImport, print, typescriptParser };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import "../App-
|
|
2
|
-
import { i as typescriptParser, n as createImport, r as print, t as createExport } from "../typescriptParser-
|
|
1
|
+
import "../App-ztRQpZS9.js";
|
|
2
|
+
import { i as typescriptParser, n as createImport, r as print, t as createExport } from "../typescriptParser-DDr_EXfe.js";
|
|
3
3
|
export { createExport, createImport, print, typescriptParser };
|
package/dist/parsers.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { o as Parser, s as UserParser } from "./App-
|
|
2
|
-
import { i as typescriptParser } from "./typescriptParser-
|
|
1
|
+
import { o as Parser, s as UserParser } from "./App-Cjd-lGfW.cjs";
|
|
2
|
+
import { i as typescriptParser } from "./typescriptParser-CBt00C7L.cjs";
|
|
3
3
|
|
|
4
4
|
//#region src/parsers/createParser.d.ts
|
|
5
5
|
declare function createParser<TOptions = unknown, TMeta extends object = any>(parser: UserParser<TOptions, TMeta>): Parser<TOptions, TMeta>;
|
package/dist/parsers.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { o as Parser, s as UserParser } from "./App-
|
|
2
|
-
import { i as typescriptParser } from "./typescriptParser-
|
|
1
|
+
import { o as Parser, s as UserParser } from "./App-ztRQpZS9.js";
|
|
2
|
+
import { i as typescriptParser } from "./typescriptParser-DDr_EXfe.js";
|
|
3
3
|
|
|
4
4
|
//#region src/parsers/createParser.d.ts
|
|
5
5
|
declare function createParser<TOptions = unknown, TMeta extends object = any>(parser: UserParser<TOptions, TMeta>): Parser<TOptions, TMeta>;
|