@nemmtor/ts-databuilders 0.0.1-alpha.4 → 0.0.1-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +402 -10
  2. package/dist/main.js +1 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,14 +1,406 @@
1
1
  # 🧱 TS DataBuilders
2
- DataBuilder Generator is a lightweight CLI tool that automatically generates builder classes from annotated TypeScript types.
3
- Just add a @DataBuilder JSDoc tag, run one command, and get a fully-typed builder ready to use in your tests or factories.
2
+ Automatically generate type-safe builder classes from your TypeScript types to write cleaner, more focused tests.
4
3
 
5
- Built with [Effect](https://effect.website/).
4
+ ## Why?
5
+ Tests often become cluttered with boilerplate when you need to create complex objects just to test one specific field. DataBuilders let you focus on what matters:
6
+ Imagine testing a case where document aggregate should emit an event when it successfully update it's content:
7
+ ```ts
8
+ it('should emit a ContentUpdatedEvent', () => {
9
+ const aggregate = DocumentAggregate.create({
10
+ id: '1',
11
+ createdAt: new Date(),
12
+ updatedAt: new Date(),
13
+ content: 'old-content'
14
+ });
15
+ const userId = '1';
6
16
 
7
- [Read about TS DataBuilders.](http://www.natpryce.com/articles/000714.html)
17
+ aggregate.updateContent({ updatedBy: userId, content: 'new-content' });
8
18
 
9
- ## 🚀 Features
10
- - 🔍 Scans your repo for specific pattern to understand what builders to build
11
- - ⚡ Generates builder classes with fluent withX() methods and sensible defaults
12
- - 🧩 Type-safe builders are generated directly from your TypeScript types
13
- - 🧠 Fast and Memory-efficient processes files asynchronously and incrementally via streams
14
- - 🛠️ Pluggable — works with any TypeScript project layout
19
+ expect(...);
20
+ })
21
+ ```
22
+ Above code obfuscated with all of the default values you need to provide in order to satisfy typescript.
23
+ Where in reality the only thing specific to this single test is the fact that some new content was provided to `updateContent` method.
24
+
25
+ Imagine even more complex scenario:
26
+ ```tsx
27
+ it('should show validation error when email is invalid', async () => {
28
+ render(<ProfileForm defaultValues={
29
+ firstName: '',
30
+ lastName: '',
31
+ age: 0,
32
+ socials: {
33
+ linkedin: '',
34
+ github: '',
35
+ website: '',
36
+ twitter: '',
37
+ },
38
+ address: {
39
+ street: '',
40
+ city: '',
41
+ state: '',
42
+ zip: '',
43
+ },
44
+ skills: [],
45
+ bio: '',
46
+ email: 'invalid-email'
47
+ }
48
+ />)
49
+
50
+ await submitForm();
51
+
52
+ expect(...);
53
+ })
54
+ ```
55
+ Again - in reality you should only be worried about email, not about whole form data.
56
+
57
+ Here's how above tests could be written with databuilders:
58
+ ```ts
59
+ it('should emit a ContentUpdatedEvent', () => {
60
+ const aggregate = DocumentAggregate.create(new CreateDocumentAggregatedPayloadBuilder().build());
61
+
62
+ aggregate.updateContent(new UpdateDocumentContentPayloadBuilder().withContent('new-content').build());
63
+
64
+ expect(...);
65
+ })
66
+ ```
67
+
68
+ ```tsx
69
+ it('should show validation error when email is invalid', async () => {
70
+ render(<ProfileForm defaultValues={new ProfileFormInputBuilder.withEmail('invalid-email').build()} />)
71
+
72
+ await submitForm();
73
+
74
+ expect(...);
75
+ })
76
+ ```
77
+
78
+ This not only makes the test code less verbose but also highlights what is really being tested.
79
+
80
+ ## Installation
81
+ Install the package:
82
+ ```bash
83
+ # npm
84
+ npm install -D @nemmtor/ts-databuilders
85
+
86
+ # pnpm
87
+ pnpm add -D @nemmtor/ts-databuilders
88
+
89
+ # yarn
90
+ yarn add -D @nemmtor/ts-databuilders
91
+ ```
92
+
93
+ ## Quick Start
94
+ **1. Annotate your types with JSDoc:**
95
+ ```ts
96
+ /**
97
+ * @DataBuilder
98
+ */
99
+ type User = {
100
+ id: string;
101
+ email: string;
102
+ name: string;
103
+ isActive: boolean;
104
+ }
105
+ ```
106
+ **2. Generate builders:**
107
+ ```bash
108
+ pnpm ts-databuilders
109
+ ```
110
+ **3. Use in your tests:**
111
+ ```ts
112
+ import { UserBuilder } from '...';
113
+
114
+ const testUser = new UserBuilder()
115
+ .withEmail('test@example.com')
116
+ .withIsActive(false)
117
+ .build();
118
+ ```
119
+ ## Generated Output
120
+
121
+ For the `User` type above, you'll get:
122
+ ```ts
123
+ import type { User } from "...";
124
+ import { DataBuilder } from "./data-builder";
125
+
126
+ export class UserBuilder extends DataBuilder<User> {
127
+ constructor() {
128
+ super({
129
+ id: "",
130
+ email: "",
131
+ name: "",
132
+ isActive: false
133
+ });
134
+ }
135
+
136
+ withId(id: User['id']) {
137
+ return this.with({ id });
138
+ }
139
+
140
+ withEmail(email: User['email']) {
141
+ return this.with({ email });
142
+ }
143
+
144
+ withName(name: User['name']) {
145
+ return this.with({ name });
146
+ }
147
+
148
+ withIsActive(isActive: User['isActive']) {
149
+ return this.with({ isActive });
150
+ }
151
+ }
152
+ ```
153
+ ## Configuration
154
+ All configuration is optional with sensible defaults.
155
+ ### Via CLI flags:
156
+ ```bash
157
+ pnpm ts-databuilders --output-dir="src/__generated__" --jsdoc-tag=MyBuilder
158
+ ```
159
+ ### Via config file (`ts-databuilders.json`):
160
+ ```json
161
+ {
162
+ "$schema": "https://raw.githubusercontent.com/nemmtor/ts-databuilders/refs/heads/main/schema.json",
163
+ "include": "src/**/*.ts",
164
+ "outputDir": "src/__generated__/builders",
165
+ "jsdocTag": "DataBuilder",
166
+ "fileSuffix": ".builder",
167
+ "builderSuffix": "Builder",
168
+ "defaults": {
169
+ "string": "",
170
+ "number": 0,
171
+ "boolean": false
172
+ }
173
+ }
174
+ ```
175
+
176
+ ### Options Reference
177
+
178
+ | Name | Flag | Description | Default |
179
+ |---------------|-------------------|--------------------------------------------------|-----------------------------|
180
+ | jsdocTag | `--jsdoc-tag` | JSDoc tag to mark types for generation | `DataBuilder` |
181
+ | outputDir | `--output-dir -o` | Output directory for generated builders | `generated/builders` |
182
+ | include | `--include -i` | Glob pattern for source files | `src/**/*.ts{,x}` |
183
+ | fileSuffix | `--file-suffix` | File suffix for builder files | `.builder` |
184
+ | builderSuffix | `--builder-suffix`| Class name suffix | `Builder` |
185
+ | defaults | `--defaults` | Default values for primitives | See example above |
186
+
187
+ **Priority:** CLI flags > Config file > Built-in defaults
188
+
189
+ ## Nested Builders
190
+ When your types contain complex nested objects, you can annotate their type definitions and TS DataBuilders will automatically generate nested builders, allowing you to compose them fluently.
191
+ ### Example
192
+
193
+ **Input types:**
194
+ ```ts
195
+ /**
196
+ * @DataBuilder
197
+ */
198
+ export type User = {
199
+ name: string;
200
+ address: Address;
201
+ };
202
+
203
+ /**
204
+ * @DataBuilder
205
+ */
206
+ export type Address = {
207
+ street: string;
208
+ city: string;
209
+ country: string;
210
+ };
211
+ ```
212
+ **Generated builders:**
213
+ ```ts
214
+ export class UserBuilder extends DataBuilder<User> {
215
+ constructor() {
216
+ super({
217
+ name: "",
218
+ address: new AddressBuilder().build();
219
+ });
220
+ }
221
+
222
+ withName(name: User['name']) {
223
+ return this.with({ name });
224
+ }
225
+
226
+ withAddress(address: DataBuilder<User['address']>) {
227
+ return this.with({ address: address.build() });
228
+ }
229
+ }
230
+
231
+ export class AddressBuilder extends DataBuilder<Address> {
232
+ constructor() {
233
+ super({
234
+ street: "",
235
+ city: "",
236
+ country: ""
237
+ });
238
+ }
239
+
240
+ withStreet(street: Address['street']) {
241
+ return this.with({ street });
242
+ }
243
+
244
+ withCity(city: Address['city']) {
245
+ return this.with({ city });
246
+ }
247
+
248
+ withCountry(country: Address['country']) {
249
+ return this.with({ country });
250
+ }
251
+ }
252
+ ```
253
+ **Usage:**
254
+ ```ts
255
+ // ✅ Compose builders fluently
256
+ const user = new UserBuilder()
257
+ .withName('John Doe')
258
+ .withAddress(
259
+ new AddressBuilder()
260
+ .withStreet('123 Main St')
261
+ .withCity('New York')
262
+ )
263
+ .build();
264
+ // {..., address: { street: "123 Main st", city: "New York", country: "" } }
265
+
266
+ // ✅ Use default values
267
+ const userWithDefaultAddress = new UserBuilder().build();
268
+ // {..., address: { street: "", city: "", country: "" } }
269
+
270
+ // ✅ Override just one nested field
271
+ const userWithCity = new UserBuilder()
272
+ .withAddress(
273
+ new AddressBuilder()
274
+ .withCity('San Francisco')
275
+ )
276
+ .build();
277
+ // {..., address: { street: "", city: "San Francisco", country: "" } }
278
+ ```
279
+
280
+ ### Supported Types
281
+
282
+ The library supports a wide range of TypeScript type features:
283
+
284
+ ✅ **Primitives & Built-ins**
285
+ - `string`, `number`, `boolean`, `Date`
286
+ - Literal types: `'active' | 'inactive'`, `1 | 2 | 3`
287
+
288
+ ✅ **Complex Structures**
289
+ - Objects and nested objects
290
+ - Arrays: `string[]`, `Array<number>`
291
+ - Tuples: `[string, number]`
292
+ - Records: `Record<string, string>` `Record<'foo' | 'bar', string>`
293
+
294
+ ✅ **Type Operations**
295
+ - Unions: `string | number | true | false`
296
+ - Intersections: `A & B`
297
+ - Utility types: `Pick<T, K>`, `Omit<T, K>`, `Partial<T>`, `Required<T>`, `Readonly<T>`, `Extract<T, U>`, `NonNullable<T>`
298
+ - Branded types: `type UserId = string & { __brand: 'UserId' }`
299
+
300
+ ✅ **References**
301
+ - Type references from the same file
302
+ - Type references from other files
303
+ - External library types (e.g., `z.infer<typeof schema>`)
304
+
305
+ **For a comprehensive example** of supported types, check out the [example-data/bar.ts](https://github.com/nemmtor/ts-databuilders/blob/main/example-data/bar.ts) file in the repository. This file is used during development and demonstrates complex real-world type scenarios.
306
+
307
+ ## Important Rules & Limitations
308
+
309
+ ### Unique Builder Names
310
+ Each type annotated with the JSDoc tag must have a **unique name** across your codebase:
311
+ ```ts
312
+ // ❌ Error: Duplicate builder names
313
+ // In file-a.ts
314
+ /** @DataBuilder */
315
+ export type User = { name: string };
316
+
317
+ // In file-b.ts
318
+ /** @DataBuilder */
319
+ export type User = { email: string }; // 💥 Duplicate!
320
+ ```
321
+
322
+ ### Exported Types Only
323
+ Types must be **exported** to generate builders:
324
+ ```ts
325
+ // ❌ Won't work
326
+ /** @DataBuilder */
327
+ type User = { name: string };
328
+
329
+ // ✅ Works
330
+ /** @DataBuilder */
331
+ export type User = { name: string };
332
+ ```
333
+
334
+ ### Type Aliases Only
335
+ Currently, only **type aliases** are supported as root builder types. Interfaces, classes, and enums are not supported:
336
+ ```ts
337
+ // ❌ Not supported
338
+ /** @DataBuilder */
339
+ export interface User {
340
+ name: string;
341
+ }
342
+
343
+ // ❌ Not supported
344
+ /** @DataBuilder */
345
+ export class User {
346
+ name: string;
347
+ }
348
+
349
+ // ✅ Supported
350
+ /** @DataBuilder */
351
+ export type User = {
352
+ name: string;
353
+ };
354
+ ```
355
+
356
+ ### Unsupported Type Features
357
+
358
+ Some TypeScript features are not yet supported and will cause generation errors:
359
+
360
+ - **Recursive types**: Types that reference themselves
361
+ ```ts
362
+ // ❌ Not supported
363
+ type TreeNode = {
364
+ value: string;
365
+ children: TreeNode[]; // Self-reference
366
+ };
367
+ ```
368
+
369
+ - **Function types**: Properties that are functions
370
+ ```ts
371
+ // ❌ Not supported
372
+ type WithCallback = {
373
+ onSave: (data: string) => void;
374
+ };
375
+ ```
376
+
377
+ - typeof, keyof, any, unknown, bigint, symbol
378
+
379
+ If you encounter an unsupported type, you'll see an error like:
380
+ ```
381
+ Unsupported syntax kind of id: XXX with raw type: YYY
382
+ ```
383
+ Support for above might be added in future.
384
+
385
+ ### Alpha Stage
386
+ ⚠️ **This library is in active development (v0.x.x)**
387
+
388
+ - Breaking changes may occur between minor versions
389
+ - Not all edge cases are covered yet
390
+ - Test thoroughly before using in production
391
+
392
+ **Found an issue?** Please [report it on GitHub](https://github.com/nemmtor/ts-databuilders/issues) with:
393
+ - The type definition causing the issue
394
+ - The error message received
395
+ - Your `ts-databuilders.json` config (if applicable)
396
+
397
+ Your feedback helps improve the library for everyone! 🙏
398
+
399
+
400
+ ## Contributing
401
+
402
+ Contributions welcome! Please open an issue or PR on [GitHub](https://github.com/nemmtor/ts-databuilders).
403
+
404
+ ## License
405
+
406
+ MIT © [nemmtor](https://github.com/nemmtor)
package/dist/main.js CHANGED
@@ -18,4 +18,4 @@ import*as Ke from"@effect/platform-node/NodeContext";import*as Ze from"@effect/p
18
18
  return this;
19
19
  }
20
20
  }
21
- `;function at(t){let i=[];function m(e){switch(e.kind){case"BUILDER":i.push(e.name);break;case"TYPE_LITERAL":Object.values(e.metadata).forEach(m);break;case"UNION":case"TUPLE":e.members.forEach(m);break;case"RECORD":m(e.keyType),m(e.valueType);break}}return Object.values(t).forEach(m),i}class R extends N.Service()("@TSDataBuilders/Builders",{effect:N.gen(function*(){let t=yield*De.FileSystem,i=yield*te,{outputDir:m}=yield*F;return{create:N.fnUntraced(function*(e){if(yield*N.orDie(t.exists(m)))yield*N.orDie(t.remove(m,{recursive:!0}));yield*N.orDie(t.makeDirectory(m,{recursive:!0})),yield*i.generateBaseBuilder();let s=e.map((r)=>r.name),n=s.filter((r,y)=>s.indexOf(r)!==y),f=[...new Set(n)];if(n.length>0)return yield*N.dieMessage(`Duplicated builders: ${f.join(", ")}`);yield*N.all(e.map((r)=>i.generateBuilder(r)),{concurrency:"unbounded"})})}}),dependencies:[te.Default]}){}import*as d from"@effect/cli/Options";import{Option as fe}from"effect";import*as v from"effect/HashMap";import*as b from"effect/Schema";var ct=d.text("jsdocTag").pipe(d.withDescription("JSDoc tag used to mark types for data building generation."),d.withSchema(U.fields.jsdocTag),d.optional),lt=d.text("output-dir").pipe(d.withAlias("o"),d.withDescription("Output directory for generated builders."),d.withSchema(U.fields.outputDir),d.optional),mt=d.text("include").pipe(d.withAlias("i"),d.withDescription("Glob pattern for files included while searching for jsdoc tag."),d.withSchema(U.fields.include),d.optional),ft=d.text("file-suffix").pipe(d.withDescription("File suffix for created builder files."),d.withSchema(U.fields.fileSuffix),d.optional),dt=d.text("builder-suffix").pipe(d.withDescription("Suffix for generated classes."),d.withSchema(U.fields.builderSuffix),d.optional),pt=d.keyValueMap("defaults").pipe(d.withDescription("Default values to be used in data builder constructor."),d.withSchema(b.HashMapFromSelf({key:b.Literal("string","number","boolean"),value:b.String}).pipe(b.transform(b.Struct({string:b.String,number:b.NumberFromString,boolean:b.BooleanFromString}).pipe(b.partial),{decode:(t)=>{return{string:t.pipe(v.get("string"),fe.getOrUndefined),number:t.pipe(v.get("number"),fe.getOrUndefined),boolean:t.pipe(v.get("boolean"),fe.getOrUndefined)}},encode:(t)=>{return v.make(["string",t.string],["number",t.number],["boolean",t.boolean])},strict:!1}))),d.optional),xe={jsdocTag:ct,outputDir:lt,include:mt,fileSuffix:ft,builderSuffix:dt,defaults:pt};import*as A from"effect/Chunk";import*as x from"effect/Effect";import*as $e from"effect/Function";import*as _ from"effect/Option";import*as ie from"effect/Stream";import*as Fe from"effect/Data";import*as Ne from"effect/Effect";import*as Be from"effect/Stream";import{glob as ut}from"glob";class V extends Ne.Service()("@TSDataBuilders/TreeWalker",{succeed:{walk:(t)=>{return Be.fromAsyncIterable(ut.stream(t,{cwd:".",nodir:!0}),(i)=>new ke({cause:i}))}}}){}class ke extends Fe.TaggedError("TreeWalkerError"){}import*as Oe from"@effect/platform/FileSystem";import*as j from"effect/Effect";import*as I from"effect/Stream";class re extends j.Service()("@TSDataBuilders/FileContentChecker",{effect:j.gen(function*(){let t=yield*Oe.FileSystem,i=new TextDecoder;return{check:j.fnUntraced(function*(m){let{content:e,filePath:p}=m;return yield*I.orDie(t.stream(p,{chunkSize:16384})).pipe(I.map((f)=>i.decode(f,{stream:!0})),I.mapAccum("",(f,r)=>{let y=f+r;return[y.slice(-e.length+1),y.includes(e)]}),I.find(Boolean),I.runCollect)})}})}){}class G extends x.Service()("@TSDataBuilders/Finder",{effect:x.gen(function*(){let t=yield*re,i=yield*V,{include:m,jsdocTag:e}=yield*F,p=`@${e}`;return{find:x.fnUntraced(function*(){return yield*i.walk(m).pipe(ie.mapEffect((f)=>t.check({filePath:f,content:p}).pipe(x.map(A.map((r)=>r?_.some(f):_.none()))),{concurrency:"unbounded"}),ie.runCollect,x.map(A.flatMap($e.identity)),x.map(A.filter((f)=>_.isSome(f))),x.map(A.map((f)=>f.value)))},x.catchTag("TreeWalkerError",(s)=>x.die(s)))}}),dependencies:[V.Default,re.Default]}){}import*as Ae from"@effect/platform/FileSystem";import*as W from"effect/Data";import*as S from"effect/Effect";import*as H from"effect/Either";import{Project as gt,SyntaxKind as se}from"ts-morph";import*as L from"effect/Data";import*as c from"effect/Effect";import*as u from"effect/Match";import*as we from"effect/Option";import{Node as yt,SyntaxKind as g}from"ts-morph";import{randomUUID as St}from"node:crypto";import*as ne from"effect/Effect";class oe extends ne.Service()("@TSDataBuilders/RandomUUID",{succeed:{generate:ne.sync(()=>St())}}){}class ae extends c.Service()("@TSDataBuilders/TypeNodeParser",{effect:c.gen(function*(){let{jsdocTag:t}=yield*F,i=yield*oe,m=(p)=>c.gen(function*(){let{type:s,contextNode:n,optional:f}=p,r=s.getProperties();if(!s.isObject()||r.length===0)return yield*new Me({raw:s.getText(),kind:n.getKind()});let y={};for(let o of r){let l=o.getName(),E=o.getTypeAtLocation(n),h=o.isOptional(),C=n.getProject().createSourceFile(`__temp_${yield*i.generate}.ts`,`type __T = ${E.getText()}`,{overwrite:!0}),$=C.getTypeAliasOrThrow("__T").getTypeNodeOrThrow(),w=yield*c.suspend(()=>e({typeNode:$,optional:h}));y[l]=w,n.getProject().removeSourceFile(C)}return{kind:"TYPE_LITERAL",metadata:y,optional:f}}),e=(p)=>c.gen(function*(){let{typeNode:s,optional:n}=p,f=s.getKind(),r=u.value(f).pipe(u.when(u.is(g.StringKeyword),()=>c.succeed({kind:"STRING",optional:n})),u.when(u.is(g.NumberKeyword),()=>c.succeed({kind:"NUMBER",optional:n})),u.when(u.is(g.BooleanKeyword),()=>c.succeed({kind:"BOOLEAN",optional:n})),u.when(u.is(g.UndefinedKeyword),()=>c.succeed({kind:"UNDEFINED",optional:n})),u.when(u.is(g.ArrayType),()=>c.succeed({kind:"ARRAY",optional:n})),u.when(u.is(g.LiteralType),()=>c.succeed({kind:"LITERAL",literalValue:s.asKindOrThrow(g.LiteralType).getLiteral().getText(),optional:n})),u.when(u.is(g.TypeLiteral),()=>c.gen(function*(){let l=s.asKindOrThrow(g.TypeLiteral).getMembers();return{kind:"TYPE_LITERAL",metadata:yield*c.reduce(l,{},(h,C)=>c.gen(function*(){if(!C.isKind(g.PropertySignature))return h;let $=C.getTypeNode();if(!$)return h;let w=C.getNameNode().getText(),z=C.hasQuestionToken(),B=yield*c.suspend(()=>e({typeNode:$,optional:z}));return{...h,[w]:B}})),optional:n}})),u.when(u.is(g.ImportType),()=>c.gen(function*(){let o=s.asKindOrThrow(g.ImportType),l=o.getType(),E=l.getSymbol();if(!E)return yield*c.die(new Pe);let h=E.getDeclarations();if(h&&h.length>1)return yield*c.die(new de);let[C]=h;if(!C)return yield*c.die(new pe);return yield*m({type:l,contextNode:o,optional:n})})),u.when(u.is(g.TupleType),()=>c.gen(function*(){let l=s.asKindOrThrow(g.TupleType).getElements(),E=yield*c.all(l.map((h)=>c.suspend(()=>e({typeNode:h,optional:!1}))));return{kind:"TUPLE",optional:n,members:E}})),u.when(u.is(g.TypeReference),()=>c.gen(function*(){let o=s.asKindOrThrow(g.TypeReference),l=o.getTypeName().getText();if(l==="Date")return{kind:"DATE",optional:n};if(l==="Array")return{kind:"ARRAY",optional:n};let E=o.getTypeArguments();if(l==="Record"){let[O,Q]=E;if(!O||!Q)return yield*new q({kind:f,raw:s.getText()});let Je=yield*c.suspend(()=>e({typeNode:O,optional:!1})),Ve=yield*c.suspend(()=>e({typeNode:Q,optional:!1}));return{kind:"RECORD",keyType:Je,valueType:Ve,optional:n}}if(["Pick","Omit","Partial","Required","Readonly","Extract","NonNullable"].includes(l))return yield*m({type:o.getType(),contextNode:o,optional:n});let C=o.getType().getAliasSymbol();if(!C)return yield*m({type:o.getType(),contextNode:o,optional:n});let $=C.getDeclarations();if($&&$.length>1)return yield*c.die(new de);let[w]=$;if(!w)return yield*c.die(new pe);let z=C?.getJsDocTags().map((O)=>O.getName()).includes(t);if(!yt.isTypeAliasDeclaration(w))return yield*c.die(new Ie);let B=w.getTypeNode();if(!B)return yield*new q({kind:f,raw:s.getText()});if(!z)return yield*c.suspend(()=>e({typeNode:B,optional:n}));return{kind:"BUILDER",name:w.getName(),optional:n}})),u.when(u.is(g.UnionType),()=>c.gen(function*(){let o=yield*c.all(s.asKindOrThrow(g.UnionType).getTypeNodes().map((l)=>c.suspend(()=>e({typeNode:l,optional:!1}))));return{kind:"UNION",optional:n,members:o}})),u.when(u.is(g.IntersectionType),()=>c.gen(function*(){let l=s.asKindOrThrow(g.IntersectionType).getTypeNodes(),E=[g.StringKeyword,g.NumberKeyword,g.BooleanKeyword],h=l.find((C)=>E.includes(C.getKind()));if(h&&l.length>1)return{kind:"TYPE_CAST",baseTypeMetadata:yield*c.suspend(()=>e({typeNode:h,optional:!1})),optional:n};return yield*new q({kind:f,raw:s.getText()})})),u.option);if(we.isNone(r))return yield*new q({kind:f,raw:s.getText()});return yield*r.value});return{generateMetadata:e}}),dependencies:[oe.Default]}){}class q extends L.TaggedError("UnsupportedSyntaxKindError"){}class de extends L.TaggedError("MultipleSymbolDeclarationsError"){}class pe extends L.TaggedError("MissingSymbolDeclarationError"){}class Pe extends L.TaggedError("MissingSymbolError"){}class Ie extends L.TaggedError("UnsupportedTypeAliasDeclaration"){}class Me extends L.TaggedError("CannotBuildTypeReferenceMetadata"){}class Y extends S.Service()("@TSDataBuilders/Parser",{effect:S.gen(function*(){let t=yield*Ae.FileSystem,i=yield*ae,{jsdocTag:m}=yield*F;return{generateBuildersMetadata:S.fnUntraced(function*(e){let p=yield*S.orDie(t.readFileString(e)),s=yield*S.try({try:()=>{return new gt().createSourceFile(e,p,{overwrite:!0}).getTypeAliases().filter((l)=>l.getJsDocs().flatMap((E)=>E.getTags().flatMap((h)=>h.getTagName())).includes(m)).map((l)=>{let E=l.getName();if(!l.isExported())return H.left(new Ue({path:e,typeName:E}));let h=l.getTypeNode();if(!(h?.isKind(se.TypeLiteral)||h?.isKind(se.TypeReference)))return H.left(new Re({path:e,typeName:l.getName()}));return H.right({name:l.getName(),node:h})}).filter(Boolean)},catch:(r)=>new Le({cause:r})}),n=yield*S.all(s.map((r)=>r));return yield*S.all(n.map(({name:r,node:y})=>i.generateMetadata({typeNode:y,optional:!1}).pipe(S.map((o)=>({name:r,shape:o,path:e})),S.catchTags({UnsupportedSyntaxKindError:(o)=>S.fail(new ve({kind:o.kind,raw:o.raw,path:e,typeName:r})),CannotBuildTypeReferenceMetadata:(o)=>S.fail(new je({kind:o.kind,raw:o.raw,path:e,typeName:r}))}))))},S.catchTags({ParserError:(e)=>S.die(e),UnexportedDatabuilderError:(e)=>S.dieMessage(`[Parser]: Unexported databuilder ${e.typeName} at ${e.path}`),RichUnsupportedSyntaxKindError:(e)=>S.dieMessage(`[Parser]: Unsupported syntax kind of id: ${e.kind} with raw type: ${e.raw} found in type ${e.typeName} in file ${e.path}`),RichCannotBuildTypeReferenceMetadata:(e)=>S.dieMessage(`[Parser]: Cannot build type reference metadata with kind of id: ${e.kind} with raw type: ${e.raw} found in type ${e.typeName} in file ${e.path}. Is it a root of databuilder?`),UnsupportedBuilderTypeError:(e)=>S.dieMessage(`[Parser]: Unsupported builder type ${e.typeName} in file ${e.path}.`)}))}}),dependencies:[ae.Default]}){}class Le extends W.TaggedError("ParserError"){}class Ue extends W.TaggedError("UnexportedDatabuilderError"){}class Re extends W.TaggedError("UnsupportedBuilderTypeError"){}class ve extends W.TaggedError("RichUnsupportedSyntaxKindError"){}class je extends W.TaggedError("RichCannotBuildTypeReferenceMetadata"){}import*as _e from"effect/Chunk";import*as K from"effect/Effect";import*as Ge from"effect/Function";var We=K.gen(function*(){let t=yield*G,i=yield*Y,m=yield*R,e=yield*t.find(),p=yield*K.all(_e.map(e,(s)=>i.generateBuildersMetadata(s)),{concurrency:"unbounded"}).pipe(K.map((s)=>s.flatMap(Ge.identity)));if(p.length===0)return;yield*m.create(p)});var Ct=M.make("ts-databuilders",xe),Ye=Ct.pipe(M.withHandler(()=>We),M.provide((t)=>Z.mergeAll(G.Default,Y.Default,R.Default).pipe(Z.provide(Z.effect(F,Ee(t))))),M.run({name:"Typescript Databuilders generator",version:"v0.0.1"}));var bt=ze.mergeAll(J.Default,Ke.layer);Ye(process.argv).pipe(Tt.provide(bt),Ze.runMain);
21
+ `;function at(t){let i=[];function m(e){switch(e.kind){case"BUILDER":i.push(e.name);break;case"TYPE_LITERAL":Object.values(e.metadata).forEach(m);break;case"UNION":case"TUPLE":e.members.forEach(m);break;case"RECORD":m(e.keyType),m(e.valueType);break}}return Object.values(t).forEach(m),i}class R extends N.Service()("@TSDataBuilders/Builders",{effect:N.gen(function*(){let t=yield*De.FileSystem,i=yield*te,{outputDir:m}=yield*F;return{create:N.fnUntraced(function*(e){if(yield*N.orDie(t.exists(m)))yield*N.orDie(t.remove(m,{recursive:!0}));yield*N.orDie(t.makeDirectory(m,{recursive:!0})),yield*i.generateBaseBuilder();let s=e.map((r)=>r.name),n=s.filter((r,y)=>s.indexOf(r)!==y),f=[...new Set(n)];if(n.length>0)return yield*N.dieMessage(`Duplicated builders: ${f.join(", ")}`);yield*N.all(e.map((r)=>i.generateBuilder(r)),{concurrency:"unbounded"})})}}),dependencies:[te.Default]}){}import*as d from"@effect/cli/Options";import{Option as fe}from"effect";import*as v from"effect/HashMap";import*as b from"effect/Schema";var ct=d.text("jsdoc-tag").pipe(d.withDescription("JSDoc tag used to mark types for data building generation."),d.withSchema(U.fields.jsdocTag),d.optional),lt=d.text("output-dir").pipe(d.withAlias("o"),d.withDescription("Output directory for generated builders."),d.withSchema(U.fields.outputDir),d.optional),mt=d.text("include").pipe(d.withAlias("i"),d.withDescription("Glob pattern for files included while searching for jsdoc tag."),d.withSchema(U.fields.include),d.optional),ft=d.text("file-suffix").pipe(d.withDescription("File suffix for created builder files."),d.withSchema(U.fields.fileSuffix),d.optional),dt=d.text("builder-suffix").pipe(d.withDescription("Suffix for generated classes."),d.withSchema(U.fields.builderSuffix),d.optional),pt=d.keyValueMap("defaults").pipe(d.withDescription("Default values to be used in data builder constructor."),d.withSchema(b.HashMapFromSelf({key:b.Literal("string","number","boolean"),value:b.String}).pipe(b.transform(b.Struct({string:b.String,number:b.NumberFromString,boolean:b.BooleanFromString}).pipe(b.partial),{decode:(t)=>{return{string:t.pipe(v.get("string"),fe.getOrUndefined),number:t.pipe(v.get("number"),fe.getOrUndefined),boolean:t.pipe(v.get("boolean"),fe.getOrUndefined)}},encode:(t)=>{return v.make(["string",t.string],["number",t.number],["boolean",t.boolean])},strict:!1}))),d.optional),xe={jsdocTag:ct,outputDir:lt,include:mt,fileSuffix:ft,builderSuffix:dt,defaults:pt};import*as A from"effect/Chunk";import*as x from"effect/Effect";import*as $e from"effect/Function";import*as _ from"effect/Option";import*as ie from"effect/Stream";import*as Fe from"effect/Data";import*as Ne from"effect/Effect";import*as Be from"effect/Stream";import{glob as ut}from"glob";class V extends Ne.Service()("@TSDataBuilders/TreeWalker",{succeed:{walk:(t)=>{return Be.fromAsyncIterable(ut.stream(t,{cwd:".",nodir:!0}),(i)=>new ke({cause:i}))}}}){}class ke extends Fe.TaggedError("TreeWalkerError"){}import*as Oe from"@effect/platform/FileSystem";import*as j from"effect/Effect";import*as I from"effect/Stream";class re extends j.Service()("@TSDataBuilders/FileContentChecker",{effect:j.gen(function*(){let t=yield*Oe.FileSystem,i=new TextDecoder;return{check:j.fnUntraced(function*(m){let{content:e,filePath:p}=m;return yield*I.orDie(t.stream(p,{chunkSize:16384})).pipe(I.map((f)=>i.decode(f,{stream:!0})),I.mapAccum("",(f,r)=>{let y=f+r;return[y.slice(-e.length+1),y.includes(e)]}),I.find(Boolean),I.runCollect)})}})}){}class G extends x.Service()("@TSDataBuilders/Finder",{effect:x.gen(function*(){let t=yield*re,i=yield*V,{include:m,jsdocTag:e}=yield*F,p=`@${e}`;return{find:x.fnUntraced(function*(){return yield*i.walk(m).pipe(ie.mapEffect((f)=>t.check({filePath:f,content:p}).pipe(x.map(A.map((r)=>r?_.some(f):_.none()))),{concurrency:"unbounded"}),ie.runCollect,x.map(A.flatMap($e.identity)),x.map(A.filter((f)=>_.isSome(f))),x.map(A.map((f)=>f.value)))},x.catchTag("TreeWalkerError",(s)=>x.die(s)))}}),dependencies:[V.Default,re.Default]}){}import*as Ae from"@effect/platform/FileSystem";import*as W from"effect/Data";import*as S from"effect/Effect";import*as H from"effect/Either";import{Project as gt,SyntaxKind as se}from"ts-morph";import*as L from"effect/Data";import*as c from"effect/Effect";import*as u from"effect/Match";import*as we from"effect/Option";import{Node as yt,SyntaxKind as g}from"ts-morph";import{randomUUID as St}from"node:crypto";import*as ne from"effect/Effect";class oe extends ne.Service()("@TSDataBuilders/RandomUUID",{succeed:{generate:ne.sync(()=>St())}}){}class ae extends c.Service()("@TSDataBuilders/TypeNodeParser",{effect:c.gen(function*(){let{jsdocTag:t}=yield*F,i=yield*oe,m=(p)=>c.gen(function*(){let{type:s,contextNode:n,optional:f}=p,r=s.getProperties();if(!s.isObject()||r.length===0)return yield*new Me({raw:s.getText(),kind:n.getKind()});let y={};for(let o of r){let l=o.getName(),E=o.getTypeAtLocation(n),h=o.isOptional(),C=n.getProject().createSourceFile(`__temp_${yield*i.generate}.ts`,`type __T = ${E.getText()}`,{overwrite:!0}),$=C.getTypeAliasOrThrow("__T").getTypeNodeOrThrow(),w=yield*c.suspend(()=>e({typeNode:$,optional:h}));y[l]=w,n.getProject().removeSourceFile(C)}return{kind:"TYPE_LITERAL",metadata:y,optional:f}}),e=(p)=>c.gen(function*(){let{typeNode:s,optional:n}=p,f=s.getKind(),r=u.value(f).pipe(u.when(u.is(g.StringKeyword),()=>c.succeed({kind:"STRING",optional:n})),u.when(u.is(g.NumberKeyword),()=>c.succeed({kind:"NUMBER",optional:n})),u.when(u.is(g.BooleanKeyword),()=>c.succeed({kind:"BOOLEAN",optional:n})),u.when(u.is(g.UndefinedKeyword),()=>c.succeed({kind:"UNDEFINED",optional:n})),u.when(u.is(g.ArrayType),()=>c.succeed({kind:"ARRAY",optional:n})),u.when(u.is(g.LiteralType),()=>c.succeed({kind:"LITERAL",literalValue:s.asKindOrThrow(g.LiteralType).getLiteral().getText(),optional:n})),u.when(u.is(g.TypeLiteral),()=>c.gen(function*(){let l=s.asKindOrThrow(g.TypeLiteral).getMembers();return{kind:"TYPE_LITERAL",metadata:yield*c.reduce(l,{},(h,C)=>c.gen(function*(){if(!C.isKind(g.PropertySignature))return h;let $=C.getTypeNode();if(!$)return h;let w=C.getNameNode().getText(),z=C.hasQuestionToken(),B=yield*c.suspend(()=>e({typeNode:$,optional:z}));return{...h,[w]:B}})),optional:n}})),u.when(u.is(g.ImportType),()=>c.gen(function*(){let o=s.asKindOrThrow(g.ImportType),l=o.getType(),E=l.getSymbol();if(!E)return yield*c.die(new Pe);let h=E.getDeclarations();if(h&&h.length>1)return yield*c.die(new de);let[C]=h;if(!C)return yield*c.die(new pe);return yield*m({type:l,contextNode:o,optional:n})})),u.when(u.is(g.TupleType),()=>c.gen(function*(){let l=s.asKindOrThrow(g.TupleType).getElements(),E=yield*c.all(l.map((h)=>c.suspend(()=>e({typeNode:h,optional:!1}))));return{kind:"TUPLE",optional:n,members:E}})),u.when(u.is(g.TypeReference),()=>c.gen(function*(){let o=s.asKindOrThrow(g.TypeReference),l=o.getTypeName().getText();if(l==="Date")return{kind:"DATE",optional:n};if(l==="Array")return{kind:"ARRAY",optional:n};let E=o.getTypeArguments();if(l==="Record"){let[O,Q]=E;if(!O||!Q)return yield*new q({kind:f,raw:s.getText()});let Je=yield*c.suspend(()=>e({typeNode:O,optional:!1})),Ve=yield*c.suspend(()=>e({typeNode:Q,optional:!1}));return{kind:"RECORD",keyType:Je,valueType:Ve,optional:n}}if(["Pick","Omit","Partial","Required","Readonly","Extract","NonNullable"].includes(l))return yield*m({type:o.getType(),contextNode:o,optional:n});let C=o.getType().getAliasSymbol();if(!C)return yield*m({type:o.getType(),contextNode:o,optional:n});let $=C.getDeclarations();if($&&$.length>1)return yield*c.die(new de);let[w]=$;if(!w)return yield*c.die(new pe);let z=C?.getJsDocTags().map((O)=>O.getName()).includes(t);if(!yt.isTypeAliasDeclaration(w))return yield*c.die(new Ie);let B=w.getTypeNode();if(!B)return yield*new q({kind:f,raw:s.getText()});if(!z)return yield*c.suspend(()=>e({typeNode:B,optional:n}));return{kind:"BUILDER",name:w.getName(),optional:n}})),u.when(u.is(g.UnionType),()=>c.gen(function*(){let o=yield*c.all(s.asKindOrThrow(g.UnionType).getTypeNodes().map((l)=>c.suspend(()=>e({typeNode:l,optional:!1}))));return{kind:"UNION",optional:n,members:o}})),u.when(u.is(g.IntersectionType),()=>c.gen(function*(){let l=s.asKindOrThrow(g.IntersectionType).getTypeNodes(),E=[g.StringKeyword,g.NumberKeyword,g.BooleanKeyword],h=l.find((C)=>E.includes(C.getKind()));if(h&&l.length>1)return{kind:"TYPE_CAST",baseTypeMetadata:yield*c.suspend(()=>e({typeNode:h,optional:!1})),optional:n};return yield*new q({kind:f,raw:s.getText()})})),u.option);if(we.isNone(r))return yield*new q({kind:f,raw:s.getText()});return yield*r.value});return{generateMetadata:e}}),dependencies:[oe.Default]}){}class q extends L.TaggedError("UnsupportedSyntaxKindError"){}class de extends L.TaggedError("MultipleSymbolDeclarationsError"){}class pe extends L.TaggedError("MissingSymbolDeclarationError"){}class Pe extends L.TaggedError("MissingSymbolError"){}class Ie extends L.TaggedError("UnsupportedTypeAliasDeclaration"){}class Me extends L.TaggedError("CannotBuildTypeReferenceMetadata"){}class Y extends S.Service()("@TSDataBuilders/Parser",{effect:S.gen(function*(){let t=yield*Ae.FileSystem,i=yield*ae,{jsdocTag:m}=yield*F;return{generateBuildersMetadata:S.fnUntraced(function*(e){let p=yield*S.orDie(t.readFileString(e)),s=yield*S.try({try:()=>{return new gt().createSourceFile(e,p,{overwrite:!0}).getTypeAliases().filter((l)=>l.getJsDocs().flatMap((E)=>E.getTags().flatMap((h)=>h.getTagName())).includes(m)).map((l)=>{let E=l.getName();if(!l.isExported())return H.left(new Ue({path:e,typeName:E}));let h=l.getTypeNode();if(!(h?.isKind(se.TypeLiteral)||h?.isKind(se.TypeReference)))return H.left(new Re({path:e,typeName:l.getName()}));return H.right({name:l.getName(),node:h})}).filter(Boolean)},catch:(r)=>new Le({cause:r})}),n=yield*S.all(s.map((r)=>r));return yield*S.all(n.map(({name:r,node:y})=>i.generateMetadata({typeNode:y,optional:!1}).pipe(S.map((o)=>({name:r,shape:o,path:e})),S.catchTags({UnsupportedSyntaxKindError:(o)=>S.fail(new ve({kind:o.kind,raw:o.raw,path:e,typeName:r})),CannotBuildTypeReferenceMetadata:(o)=>S.fail(new je({kind:o.kind,raw:o.raw,path:e,typeName:r}))}))))},S.catchTags({ParserError:(e)=>S.die(e),UnexportedDatabuilderError:(e)=>S.dieMessage(`[Parser]: Unexported databuilder ${e.typeName} at ${e.path}`),RichUnsupportedSyntaxKindError:(e)=>S.dieMessage(`[Parser]: Unsupported syntax kind of id: ${e.kind} with raw type: ${e.raw} found in type ${e.typeName} in file ${e.path}`),RichCannotBuildTypeReferenceMetadata:(e)=>S.dieMessage(`[Parser]: Cannot build type reference metadata with kind of id: ${e.kind} with raw type: ${e.raw} found in type ${e.typeName} in file ${e.path}. Is it a root of databuilder?`),UnsupportedBuilderTypeError:(e)=>S.dieMessage(`[Parser]: Unsupported builder type ${e.typeName} in file ${e.path}.`)}))}}),dependencies:[ae.Default]}){}class Le extends W.TaggedError("ParserError"){}class Ue extends W.TaggedError("UnexportedDatabuilderError"){}class Re extends W.TaggedError("UnsupportedBuilderTypeError"){}class ve extends W.TaggedError("RichUnsupportedSyntaxKindError"){}class je extends W.TaggedError("RichCannotBuildTypeReferenceMetadata"){}import*as _e from"effect/Chunk";import*as K from"effect/Effect";import*as Ge from"effect/Function";var We=K.gen(function*(){let t=yield*G,i=yield*Y,m=yield*R,e=yield*t.find(),p=yield*K.all(_e.map(e,(s)=>i.generateBuildersMetadata(s)),{concurrency:"unbounded"}).pipe(K.map((s)=>s.flatMap(Ge.identity)));if(p.length===0)return;yield*m.create(p)});var Ct=M.make("ts-databuilders",xe),Ye=Ct.pipe(M.withHandler(()=>We),M.provide((t)=>Z.mergeAll(G.Default,Y.Default,R.Default).pipe(Z.provide(Z.effect(F,Ee(t))))),M.run({name:"Typescript Databuilders generator",version:"v0.0.1"}));var bt=ze.mergeAll(J.Default,Ke.layer);Ye(process.argv).pipe(Tt.provide(bt),Ze.runMain);
package/package.json CHANGED
@@ -58,5 +58,5 @@
58
58
  "glob": "^11.0.3",
59
59
  "ts-morph": "^27.0.2"
60
60
  },
61
- "version": "0.0.1-alpha.4"
61
+ "version": "0.0.1-alpha.6"
62
62
  }