@@ -28,29 +28,7 @@ Each generator declares its dependency using the `dependsOn` field, allowing aut
2828
2929## Generator Structure
3030
31- A generator is defined as a module exporting an object conforming to the ` GeneratorMetadata ` interface:
32-
33- ``` typescript
34- interface GeneratorMetadata <Input , Output > {
35- name: string ;
36- version: string ;
37- description: string ;
38- dependsOn? : string ;
39-
40- // Core generation function
41- generate(
42- input : Input ,
43- options : Partial <GeneratorOptions >
44- ): Promise <Output > | AsyncGenerator <Output >;
45-
46- // Optional: for parallel processing
47- processChunk? (
48- fullInput : any ,
49- itemIndices : number [],
50- deps : any
51- ): Promise <Output >;
52- }
53- ```
31+ A generator is defined as a module exporting an object conforming to the ` GeneratorMetadata ` interface.
5432
5533## Creating a Basic Generator
5634
@@ -60,14 +38,35 @@ Create a new directory in `src/generators/`:
6038
6139```
6240src/generators/my-format/
63- ├── index.mjs # Main generator file
41+ ├── index.mjs # Main generator file (required)
6442├── constants.mjs # Constants (optional)
65- ├── types.d.ts # TypeScript types (optional )
43+ ├── types.d.ts # TypeScript types (required )
6644└── utils/ # Utility functions (optional)
6745 └── formatter.mjs
6846```
6947
70- ### Step 2: Implement the Generator
48+ ### Step 2: Define Types
49+
50+ Create a ` types.d.ts ` file containing a ` Generator ` export. Use this when typing your generator.
51+
52+ ``` ts
53+ export type Generator = GeneratorMetadata <
54+ {
55+ // If your generator supports a custom configuration,
56+ // define it here
57+ myCustomOption: string ;
58+ },
59+ Generate <InputToMyGenerator , Promise <OutputOfMyGenerator >>,
60+ // If your generator supports parallel processing:
61+ ProcessChunk <
62+ InputToMyParallelProcessor ,
63+ OutputOfMyParallelProcessor ,
64+ DependenciesOfMyParallelProcessor
65+ >
66+ >;
67+ ```
68+
69+ ### Step 3: Implement the Generator
7170
7271``` javascript
7372// src/generators/my-format/index.mjs
@@ -92,6 +91,15 @@ export default {
9291 // This generator depends on the metadata generator
9392 dependsOn: ' metadata' ,
9493
94+ defaultConfiguration: {
95+ // If your generator supports a custom configuration, define the defaults here
96+ myCustomOption: ' myDefaultValue'
97+
98+ // All generators support options in the GlobalConfiguration object
99+ // To override the defaults, they can be specified here
100+ ref: ' overriddenRef'
101+ }
102+
95103 /**
96104 * Main generation function
97105 *
@@ -126,7 +134,7 @@ function transformToMyFormat(entries, version) {
126134}
127135```
128136
129- ### Step 3 : Register the Generator
137+ ### Step 4 : Register the Generator
130138
131139Add your generator to the exports in ` src/generators/index.mjs ` :
132140
@@ -187,12 +195,15 @@ export default {
187195 * Main generation function that orchestrates worker threads
188196 *
189197 * @param {Input} input
190- * @param {Partial<GeneratorOptions> } options
198+ * @param {ParallelWorker } options
191199 */
192- async * generate (input , { worker, output }) {
200+ async * generate (input , worker ) {
201+ // Configuration for this generator is based on it's name
202+ const { 'parallel -generator ': config } = getConfig ();
203+
193204 // Prepare serializable dependencies
194205 const deps = {
195- version: options .version ,
206+ version: config .version ,
196207 ... someConfig,
197208 };
198209
@@ -245,9 +256,7 @@ export default {
245256 /**
246257 * Generator function that yields results incrementally
247258 */
248- async * generate (input , options ) {
249- const { worker } = options;
250-
259+ async * generate (input , worker ) {
251260 // Stream results as workers complete chunks
252261 for await (const chunkResult of worker .stream (input, input, {})) {
253262 // Yield immediately - downstream can start processing
@@ -277,7 +286,7 @@ export default {
277286 /**
278287 * Non-streaming - returns Promise instead of AsyncGenerator
279288 */
280- async generate (input , options ) {
289+ async generate (input , worker ) {
281290 // Collect all input (if dependency is streaming, this waits for completion)
282291 const allData = await collectAll (input);
283292
@@ -304,7 +313,7 @@ export default {
304313 name: ' my-generator' ,
305314 dependsOn: ' metadata' , // This generator requires metadata output
306315
307- async generate (input , options ) {
316+ async generate (input , worker ) {
308317 // input contains the output from 'metadata' generator
309318 },
310319};
@@ -352,60 +361,46 @@ The framework ensures `metadata` runs once and its output is cached for all cons
352361### Writing Output Files
353362
354363``` javascript
355- import { writeFile , mkdir } from ' node:fs/promises' ;
356- import { join } from ' node:path' ;
364+ const { 'my -generator ': config } = getConfig ();
357365
358- async generate (input , options ) {
359- const { output } = options;
366+ if (! config .output ) {
367+ // Return data without writing
368+ return result;
369+ }
360370
361- if (! output) {
362- // Return data without writing
363- return result;
364- }
371+ // Ensure directory exists
372+ await mkdir (config .output , { recursive: true });
365373
366- // Ensure directory exists
367- await mkdir ( output, { recursive : true } );
374+ // Write single file
375+ await writeFile ( join ( config . output , ' output.txt ' ), content, ' utf-8 ' );
368376
369- // Write single file
377+ // Write multiple files
378+ for (const item of items) {
370379 await writeFile (
371- join (output, ' output. txt' ),
372- content,
380+ join (config . output , ` ${ item . name } . txt` ),
381+ item . content ,
373382 ' utf-8'
374383 );
375-
376- // Write multiple files
377- for (const item of items) {
378- await writeFile (
379- join (output, ` ${ item .name } .txt` ),
380- item .content ,
381- ' utf-8'
382- );
383- }
384-
385- return result;
386384}
385+
386+ return result;
387387```
388388
389389### Copying Assets
390390
391391``` javascript
392- import { cp } from ' node:fs/promises' ;
393- import { join } from ' node:path' ;
394-
395- async generate (input , options ) {
396- const { output } = options;
397-
398- if (output) {
399- // Copy asset directory
400- await cp (
401- new URL (' ./assets' , import .meta.url),
402- join (output, ' assets' ),
403- { recursive: true }
404- );
405- }
406-
407- return result;
392+ const { 'my -generator ': config } = getConfig ();
393+
394+ if (config .output ) {
395+ // Copy asset directory
396+ await cp (
397+ new URL (' ./assets' , import .meta.url),
398+ join (config .output , ' assets' ),
399+ { recursive: true }
400+ );
408401}
402+
403+ return result;
409404` ` `
410405
411406### Output Structure
0 commit comments