@nemmtor/ts-databuilders 0.0.1-alpha.5 → 0.0.1-alpha.7

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 +329 -96
  2. package/dist/main.js +4 -4
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -1,97 +1,9 @@
1
1
  # 🧱 TS DataBuilders
2
- A CLI tool that automatically generates builder classes from annotated TypeScript types.
2
+ Automatically generate type-safe builder classes from your TypeScript types to write cleaner, more focused tests.
3
3
 
4
- ## Usage
5
- Install the package:
6
- ```bash
7
- pnpm add -D @nemmtor/ts-databuilders
8
- ```
9
-
10
- Annotate the types you want to build with JsDoc tag:
11
- ```ts
12
- /**
13
- * @DataBuilder
14
- */
15
- type Example = {
16
- bar: string;
17
- baz: string;
18
- }
19
- ```
20
-
21
- Run the command:
22
- ```bash
23
- pnpm ts-databuilders
24
- ```
25
- By default it tries to find annotated types in `src/**/*.ts{,x}` and outputs builders in `generated/builders`.
26
- Above and much more is configurable - check out configuration section.
27
-
28
- Above example will result in output:
29
- ```ts
30
- import type { Example } from "../../src/example";
31
- import { DataBuilder } from "./data-builder";
32
-
33
- export class ExampleBuilder extends DataBuilder<Example> {
34
- constructor() {
35
- super({
36
- bar: "",
37
- baz: ""
38
- });
39
- }
40
-
41
- withBar(bar: Example['bar']) {
42
- return this.with({ bar: bar });
43
- }
44
-
45
- withBaz(baz: Example['baz']) {
46
- return this.with({ baz: baz });
47
- }
48
- }
49
- ```
50
-
51
- ## Configuration
52
- > [!NOTE]
53
- > Name column is equal to field names in ts-databuilders.json file.
54
-
55
- | Name | Flag | Description | Default | Type |
56
- |---------------|-----------------|----------------------------------------------------------------|--------------------------------------------------|------------|
57
- | jsdocTag | --jsdoc-tag | JSDoc tag used to mark types for data building generation. | DataBuilder | string |
58
- | outputDir | --output-dir -o | Output directory for generated builders. | generated/builders | string |
59
- | include | --include -i | Glob pattern for files included while searching for jsdoc tag. | src/**/*.ts{,x} | string |
60
- | fileSuffix | --file-suffix | File suffix for created builder files. | .builder | string |
61
- | builderSuffix | --builder-suffix | Suffix for generated classes. | Builder | string |
62
- | defaults | --defaults | Default values to be used in data builder constructor. | { string: '', number: 0, boolean: false, } | object/map |
63
-
64
- All of the above flags are optional and are having sensible defaults which should be good enough for most cases.
65
- You can configure these via cli flags or by creating `ts-databuilders.json` file in the root of your repository.
66
- Example of config file:
67
- ```json
68
- {
69
- "$schema": "https://raw.githubusercontent.com/nemmtor/ts-databuilders/refs/heads/main/schema.json",
70
- "include": "example-data/**",
71
- "builderSuffix": "GeneratedBuilder",
72
- "fileSuffix": ".generated-builder",
73
- "jsdocTag": "GenerateBuilder",
74
- "outputDir": "generated-builders/",
75
- "defaults": {
76
- "boolean": true,
77
- "number": 2000,
78
- "string": "foo"
79
- }
80
- }
81
- ```
82
-
83
- Priority when resolving config values is:
84
- 1. Cli flags
85
- 2. Config file values
86
- 3. Hardcoded defaults
87
-
88
- ## Motivation
89
- When writing tests you often want to test some scenario that is happening when
90
- one of the input values is in a specific shape.
91
- Often times this value is only one of many options provided.
92
-
93
- Imagine testing a case where document aggregate should emit an event when it successfully
94
- update it's content:
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:
95
7
  ```ts
96
8
  it('should emit a ContentUpdatedEvent', () => {
97
9
  const aggregate = DocumentAggregate.create({
@@ -165,8 +77,329 @@ it('should show validation error when email is invalid', async () => {
165
77
 
166
78
  This not only makes the test code less verbose but also highlights what is really being tested.
167
79
 
168
- ## Nested builders
169
- TODO
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
+
156
+ ### Initialize Config File (optional)
157
+ Generate a default `ts-databuilders.json` configuration file:
158
+ ```bash
159
+ pnpm ts-databuilders init
160
+ ```
161
+ You can also generate configuration file by providing values step by step in an interactive wizard:
162
+ ```bash
163
+ pnpm ts-databuilders init --wizard
164
+ ```
165
+
166
+ ### Configure via CLI flags (optional:
167
+ ```bash
168
+ pnpm ts-databuilders --output-dir="src/__generated__" --jsdoc-tag=MyBuilder
169
+ ```
170
+ You can also provide configuration by going through interactive wizard:
171
+ ```bash
172
+ pnpm ts-databuilders --wizard
173
+ ```
174
+
175
+ ### Options Reference
176
+
177
+ | Name | Flag | Description | Default |
178
+ |---------------|-------------------------------------------------------|-----------------------------------------|----------------------|
179
+ | jsdocTag | `--jsdoc-tag` | JSDoc tag to mark types for generation | `DataBuilder` |
180
+ | outputDir | `--output-dir -o` | Output directory for generated builders | `generated/builders` |
181
+ | include | `--include -i` | Glob pattern for source files | `src/**/*.ts{,x}` |
182
+ | fileSuffix | `--file-suffix` | File suffix for builder files | `.builder` |
183
+ | builderSuffix | `--builder-suffix` | Class name suffix | `Builder` |
184
+ | defaults | `--default-string --default-number --default-boolean` | Default values for primitives | See example above |
185
+
186
+ **Priority:** CLI flags > Config file > Built-in defaults
187
+
188
+ ## Nested Builders
189
+ 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.
190
+ ### Example
191
+
192
+ **Input types:**
193
+ ```ts
194
+ /**
195
+ * @DataBuilder
196
+ */
197
+ export type User = {
198
+ name: string;
199
+ address: Address;
200
+ };
201
+
202
+ /**
203
+ * @DataBuilder
204
+ */
205
+ export type Address = {
206
+ street: string;
207
+ city: string;
208
+ country: string;
209
+ };
210
+ ```
211
+ **Generated builders:**
212
+ ```ts
213
+ export class UserBuilder extends DataBuilder<User> {
214
+ constructor() {
215
+ super({
216
+ name: "",
217
+ address: new AddressBuilder().build();
218
+ });
219
+ }
220
+
221
+ withName(name: User['name']) {
222
+ return this.with({ name });
223
+ }
224
+
225
+ withAddress(address: DataBuilder<User['address']>) {
226
+ return this.with({ address: address.build() });
227
+ }
228
+ }
229
+
230
+ export class AddressBuilder extends DataBuilder<Address> {
231
+ constructor() {
232
+ super({
233
+ street: "",
234
+ city: "",
235
+ country: ""
236
+ });
237
+ }
238
+
239
+ withStreet(street: Address['street']) {
240
+ return this.with({ street });
241
+ }
242
+
243
+ withCity(city: Address['city']) {
244
+ return this.with({ city });
245
+ }
246
+
247
+ withCountry(country: Address['country']) {
248
+ return this.with({ country });
249
+ }
250
+ }
251
+ ```
252
+ **Usage:**
253
+ ```ts
254
+ // ✅ Compose builders fluently
255
+ const user = new UserBuilder()
256
+ .withName('John Doe')
257
+ .withAddress(
258
+ new AddressBuilder()
259
+ .withStreet('123 Main St')
260
+ .withCity('New York')
261
+ )
262
+ .build();
263
+ // {..., address: { street: "123 Main st", city: "New York", country: "" } }
264
+
265
+ // ✅ Use default values
266
+ const userWithDefaultAddress = new UserBuilder().build();
267
+ // {..., address: { street: "", city: "", country: "" } }
268
+
269
+ // ✅ Override just one nested field
270
+ const userWithCity = new UserBuilder()
271
+ .withAddress(
272
+ new AddressBuilder()
273
+ .withCity('San Francisco')
274
+ )
275
+ .build();
276
+ // {..., address: { street: "", city: "San Francisco", country: "" } }
277
+ ```
278
+
279
+ ### Supported Types
280
+
281
+ The library supports a wide range of TypeScript type features:
282
+
283
+ ✅ **Primitives & Built-ins**
284
+ - `string`, `number`, `boolean`, `Date`
285
+ - Literal types: `'active' | 'inactive'`, `1 | 2 | 3`
286
+
287
+ ✅ **Complex Structures**
288
+ - Objects and nested objects
289
+ - Arrays: `string[]`, `Array<number>`
290
+ - Tuples: `[string, number]`
291
+ - Records: `Record<string, string>` `Record<'foo' | 'bar', string>`
292
+
293
+ ✅ **Type Operations**
294
+ - Unions: `string | number | true | false`
295
+ - Intersections: `A & B`
296
+ - Utility types: `Pick<T, K>`, `Omit<T, K>`, `Partial<T>`, `Required<T>`, `Readonly<T>`, `Extract<T, U>`, `NonNullable<T>`
297
+ - Branded types: `type UserId = string & { __brand: 'UserId' }`
298
+
299
+ ✅ **References**
300
+ - Type references from the same file
301
+ - Type references from other files
302
+ - External library types (e.g., `z.infer<typeof schema>`)
303
+
304
+ **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.
305
+
306
+ ## Important Rules & Limitations
307
+
308
+ ### Unique Builder Names
309
+ Each type annotated with the JSDoc tag must have a **unique name** across your codebase:
310
+ ```ts
311
+ // ❌ Error: Duplicate builder names
312
+ // In file-a.ts
313
+ /** @DataBuilder */
314
+ export type User = { name: string };
315
+
316
+ // In file-b.ts
317
+ /** @DataBuilder */
318
+ export type User = { email: string }; // 💥 Duplicate!
319
+ ```
320
+
321
+ ### Exported Types Only
322
+ Types must be **exported** to generate builders:
323
+ ```ts
324
+ // ❌ Won't work
325
+ /** @DataBuilder */
326
+ type User = { name: string };
327
+
328
+ // ✅ Works
329
+ /** @DataBuilder */
330
+ export type User = { name: string };
331
+ ```
332
+
333
+ ### Type Aliases Only
334
+ Currently, only **type aliases** are supported as root builder types. Interfaces, classes, and enums are not supported:
335
+ ```ts
336
+ // ❌ Not supported
337
+ /** @DataBuilder */
338
+ export interface User {
339
+ name: string;
340
+ }
341
+
342
+ // ❌ Not supported
343
+ /** @DataBuilder */
344
+ export class User {
345
+ name: string;
346
+ }
347
+
348
+ // ✅ Supported
349
+ /** @DataBuilder */
350
+ export type User = {
351
+ name: string;
352
+ };
353
+ ```
354
+
355
+ ### Unsupported Type Features
356
+
357
+ Some TypeScript features are not yet supported and will cause generation errors:
358
+
359
+ - **Recursive types**: Types that reference themselves
360
+ ```ts
361
+ // ❌ Not supported
362
+ type TreeNode = {
363
+ value: string;
364
+ children: TreeNode[]; // Self-reference
365
+ };
366
+ ```
367
+
368
+ - **Function types**: Properties that are functions
369
+ ```ts
370
+ // ❌ Not supported
371
+ type WithCallback = {
372
+ onSave: (data: string) => void;
373
+ };
374
+ ```
375
+
376
+ - typeof, keyof, any, unknown, bigint, symbol
377
+
378
+ If you encounter an unsupported type, you'll see an error like:
379
+ ```
380
+ Unsupported syntax kind of id: XXX with raw type: YYY
381
+ ```
382
+ Support for above might be added in future.
383
+
384
+ ### Alpha Stage
385
+ ⚠️ **This library is in active development (v0.x.x)**
386
+
387
+ - Breaking changes may occur between minor versions
388
+ - Not all edge cases are covered yet
389
+ - Test thoroughly before using in production
390
+
391
+ **Found an issue?** Please [report it on GitHub](https://github.com/nemmtor/ts-databuilders/issues) with:
392
+ - The type definition causing the issue
393
+ - The error message received
394
+ - Your `ts-databuilders.json` config (if applicable)
395
+
396
+ Your feedback helps improve the library for everyone! 🙏
397
+
398
+
399
+ ## Contributing
400
+
401
+ Contributions welcome! Please open an issue or PR on [GitHub](https://github.com/nemmtor/ts-databuilders).
402
+
403
+ ## License
170
404
 
171
- ## Supported types
172
- TODO
405
+ MIT © [nemmtor](https://github.com/nemmtor)
package/dist/main.js CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env node
2
- import*as Ke from"@effect/platform-node/NodeContext";import*as Ze from"@effect/platform-node/NodeRuntime";import{Effect as Tt}from"effect";import*as ze from"effect/Layer";import*as M from"@effect/cli/Command";import*as Z from"effect/Layer";import*as De from"@effect/platform/FileSystem";import*as N from"effect/Effect";import*as ce from"@effect/platform/FileSystem";import*as he from"@effect/platform/Path";import*as Se from"effect/Context";import*as D from"effect/Effect";import*as k from"effect/Option";import*as a from"effect/Schema";import*as X from"effect/Effect";class J extends X.Service()("@TSDataBuilders/Process",{succeed:{cwd:X.sync(()=>process.cwd())}}){}var qe="ts-databuilders.json",ye=a.Struct({jsdocTag:a.NonEmptyTrimmedString,outputDir:a.NonEmptyTrimmedString,include:a.NonEmptyTrimmedString,fileSuffix:a.NonEmptyTrimmedString,builderSuffix:a.NonEmptyTrimmedString,defaults:a.Struct({string:a.String,number:a.Number,boolean:a.Boolean})}),ge=ye.make({jsdocTag:"DataBuilder",outputDir:"generated/builders",include:"src/**/*.ts{,x}",fileSuffix:".builder",builderSuffix:"Builder",defaults:{string:"",number:0,boolean:!1}}),He=ye.pipe(a.omit("defaults"),a.extend(a.Struct({$schema:a.optional(a.String),defaults:a.Struct({string:a.String,number:a.Number,boolean:a.Boolean}).pipe(a.partial)})),a.partial);class F extends Se.Tag("Configuration")(){}var U=a.Struct({jsdocTag:a.NonEmptyTrimmedString,outputDir:a.NonEmptyTrimmedString,include:a.NonEmptyTrimmedString,fileSuffix:a.NonEmptyTrimmedString,builderSuffix:a.NonEmptyTrimmedString,defaults:a.Struct({string:a.String,number:a.Number,boolean:a.Boolean}).pipe(a.partial)}),Ee=(t)=>D.gen(function*(){let m=yield*(yield*J).cwd,p=(yield*he.Path).join(m,qe),s=yield*et(p),n=yield*Qe({providedConfiguration:t,configFileContent:s});return F.of(n)}),Qe=(t)=>D.gen(function*(){let i=Xe(t),m=t.providedConfiguration.defaults.pipe(k.getOrElse(()=>({}))),e=k.flatMap(t.configFileContent,(f)=>k.fromNullable(f.defaults)).pipe(k.getOrElse(()=>({}))),p=Object.fromEntries(Object.entries(m).filter(([f,r])=>typeof r<"u")),n={...Object.fromEntries(Object.entries(e).filter(([f,r])=>typeof r<"u")),...p};return{builderSuffix:yield*i("builderSuffix"),include:yield*i("include"),fileSuffix:yield*i("fileSuffix"),jsdocTag:yield*i("jsdocTag"),outputDir:yield*i("outputDir"),defaults:{...ge.defaults,...n}}}),Xe=(t)=>(i)=>t.providedConfiguration[i].pipe(D.orElse(()=>k.flatMap(t.configFileContent,(m)=>k.fromNullable(m[i]))),D.orElseSucceed(()=>ge[i])),et=(t)=>D.gen(function*(){let i=yield*ce.FileSystem;if(yield*D.orDie(i.exists(t))){let e=yield*tt(t),p=yield*a.decodeUnknown(He)(e);return k.some(p)}else return k.none()}),tt=(t)=>D.gen(function*(){let i=yield*ce.FileSystem,m=yield*D.orDie(i.readFileString(t));return yield*D.try({try:()=>JSON.parse(m),catch:(e)=>`[FileSystem] Unable to read and parse JSON file from '${t}': ${String(e)}`})}).pipe(D.orDie);import{Path as it}from"@effect/platform";import*as be from"@effect/platform/FileSystem";import*as P from"effect/Effect";import*as T from"effect/Match";import{Project as nt}from"ts-morph";var ee=(t)=>t.replace(/([a-z0-9])([A-Z])/g,"$1-$2").replace(/([A-Z])([A-Z][a-z])/g,"$1-$2").replace(/_/g,"-").toLowerCase(),le=(t)=>{return t.split(/[-_\s]+/).flatMap((i)=>i.split(/(?=[A-Z])/)).filter(Boolean).map((i)=>i.charAt(0).toUpperCase()+i.slice(1).toLowerCase()).join("")},me=(t)=>{return t.split(/[-_\s]+/).flatMap((m)=>m.split(/(?=[A-Z])/)).filter(Boolean).map((m,e)=>{let p=m.toLowerCase();return e===0?p:p.charAt(0).toUpperCase()+p.slice(1)}).join("")};var Ce=(t)=>{let{fieldName:i,optional:m,typeName:e,isNestedBuilder:p}=t,s=i.replaceAll("'","").replaceAll('"',""),n=me(s),f=`with${le(s)}`,r=[`return this.with({ ${i}: ${n} });`],y=[`return this.with({ ${i}: ${n}.build() });`],o=p?y:r,l=[`if (!${n}) {`,` const { "${s}": _unused, ...rest } = this.build();`," return this.with(rest);","}"],E=m?[...l,...o]:o,h=`${e}['${s}']`;return{name:f,isPublic:!0,parameters:[{name:n,type:p?`DataBuilder<${h}>`:h}],statements:E}};class te extends P.Service()("@TSDataBuilders/BuilderGenerator",{effect:P.gen(function*(){let t=yield*be.FileSystem,i=yield*it.Path,{outputDir:m,fileSuffix:e,builderSuffix:p,defaults:s}=yield*F,n=(f)=>T.value(f).pipe(T.when({kind:"STRING"},()=>`"${s.string}"`),T.when({kind:"NUMBER"},()=>s.number),T.when({kind:"BOOLEAN"},()=>s.boolean),T.when({kind:"UNDEFINED"},()=>"undefined"),T.when({kind:"DATE"},()=>"new Date()"),T.when({kind:"ARRAY"},()=>"[]"),T.when({kind:"LITERAL"},(r)=>r.literalValue),T.when({kind:"TYPE_CAST"},(r)=>n(r.baseTypeMetadata)),T.when({kind:"TUPLE"},(r)=>{return`[${r.members.map((o)=>n(o)).map((o)=>`${o}`).join(", ")}]`}),T.when({kind:"TYPE_LITERAL"},(r)=>{return`{${Object.entries(r.metadata).filter(([o,{optional:l}])=>!l).map(([o,l])=>`${o}: ${n(l)}`).join(", ")}}`}),T.when({kind:"RECORD"},(r)=>{if(r.keyType.kind==="STRING"||r.keyType.kind==="NUMBER")return"{}";let y=n(r.keyType),o=n(r.valueType);return`{${y}: ${o}}`}),T.when({kind:"UNION"},(r)=>{let o=r.members.slice().sort((l,E)=>{let h=Te.indexOf(l.kind),C=Te.indexOf(E.kind);return(h===-1?1/0:h)-(C===-1?1/0:C)})[0];if(!o)return"never";return n(o)}),T.when({kind:"BUILDER"},(r)=>{return`new ${r.name}${p}().build()`}),T.exhaustive);return{generateBaseBuilder:P.fnUntraced(function*(){let f=i.resolve(m,"data-builder.ts");yield*P.orDie(t.writeFileString(f,ot))}),generateBuilder:P.fnUntraced(function*(f){let r=new nt,y=f.name,o=i.resolve(m,`${ee(y)}${e}.ts`),l=r.createSourceFile(o,"",{overwrite:!0}),E=i.resolve(f.path),h=i.relative(i.dirname(o),E).replace(/\.ts$/,"");if(l.addImportDeclaration({namedImports:[y],isTypeOnly:!0,moduleSpecifier:h}),l.addImportDeclaration({namedImports:["DataBuilder"],moduleSpecifier:"./data-builder"}),f.shape.kind!=="TYPE_LITERAL")return yield*P.dieMessage("[BuilderGenerator]: Expected root type to be type literal");[...new Set(at(f.shape.metadata))].forEach((B)=>{l.addImportDeclaration({namedImports:[`${B}${p}`],moduleSpecifier:`./${ee(B)}${e}`})});let $=Object.entries(f.shape.metadata).filter(([B,{optional:O}])=>!O).map(([B,O])=>`${B}: ${O.kind==="TYPE_CAST"?`${n(O)} as ${y}['${B}']`:n(O)}`),w=Object.entries(f.shape.metadata).map(([B,{optional:O,kind:Q}])=>Ce({fieldName:B,optional:O,typeName:y,isNestedBuilder:Q==="BUILDER"})),z=`{
3
- ${$.join(`,
2
+ import*as it from"@effect/platform-node/NodeContext";import*as rt from"@effect/platform-node/NodeRuntime";import{Effect as Ut}from"effect";import*as ot from"effect/Layer";import*as nt from"effect/Logger";import*as at from"effect/LogLevel";import*as U from"@effect/cli/Command";import*as X from"effect/Layer";import*as $e from"@effect/platform/FileSystem";import*as we from"@effect/platform/Path";import*as B from"effect/Effect";import*as pe from"@effect/platform/FileSystem";import*as Te from"@effect/platform/Path";import*as xe from"effect/Context";import*as C from"effect/Effect";import*as T from"effect/Option";import*as r from"effect/Schema";var D={jsdocTag:"JSDoc tag used to mark types for data building generation.",outputDir:"Output directory for generated builders.",include:"Glob pattern for files included while searching for jsdoc tag.",fileSuffix:"File suffix for created builder files.",builderSuffix:"Suffix for generated classes.",defaults:"Default values to be used in data builder constructor.",defaultString:"Default string value to be used in data builder constructor.",defaultNumber:"Default number value to be used in data builder constructor.",defaultBoolean:"Default boolean value to be used in data builder constructor."};import*as oe from"effect/Effect";class $ extends oe.Service()("@TSDataBuilders/Process",{succeed:{cwd:oe.sync(()=>process.cwd())}}){}var De=(t)=>{return t!==void 0};var ee=(t)=>{return Object.fromEntries(Object.entries(t).filter(([i,c])=>De(c)))};var ge="ts-databuilders.json",st=r.Struct({jsdocTag:r.NonEmptyTrimmedString,outputDir:r.NonEmptyTrimmedString,include:r.NonEmptyTrimmedString,fileSuffix:r.NonEmptyTrimmedString,builderSuffix:r.NonEmptyTrimmedString,defaults:r.Struct({string:r.String,number:r.Number,boolean:r.Boolean})}),M=st.make({jsdocTag:"DataBuilder",outputDir:"generated/builders",include:"src/**/*.ts{,x}",fileSuffix:".builder",builderSuffix:"Builder",defaults:{string:"",number:0,boolean:!1}}),he=r.Struct({$schema:r.optional(r.String),jsdocTag:r.String.pipe(r.annotations({description:D.jsdocTag})),outputDir:r.String.pipe(r.annotations({description:D.outputDir})),include:r.String.pipe(r.annotations({description:D.include})),fileSuffix:r.String.pipe(r.annotations({description:D.fileSuffix})),builderSuffix:r.String.pipe(r.annotations({description:D.builderSuffix})),defaults:r.Struct({string:r.String.pipe(r.annotations({description:D.defaultString})),number:r.Number.pipe(r.annotations({description:D.defaultNumber})),boolean:r.Boolean.pipe(r.annotations({description:D.defaultBoolean}))}).pipe(r.partial,r.annotations({description:D.defaults}))}).pipe(r.partial);class N extends xe.Tag("Configuration")(){}var R=r.Struct({jsdocTag:r.NonEmptyTrimmedString,outputDir:r.NonEmptyTrimmedString,include:r.NonEmptyTrimmedString,fileSuffix:r.NonEmptyTrimmedString,builderSuffix:r.NonEmptyTrimmedString,defaultString:r.String,defaultNumber:r.NumberFromString,defaultBoolean:r.BooleanFromString}),Fe=(t)=>C.gen(function*(){yield*C.logDebug("[Configuration]: Loading configuration");let c=yield*(yield*$).cwd,g=(yield*Te.Path).join(c,ge),s=yield*lt(g),o=yield*ct({fromCLI:t,fromConfigFile:s});return N.of(o)}),ct=(t)=>C.gen(function*(){let i=ft(t),c=T.flatMap(t.fromConfigFile,(o)=>T.fromNullable(o.defaults)).pipe(T.map((o)=>ee(o)),T.getOrElse(()=>({}))),e=ee({string:t.fromCLI.defaultString.pipe(T.getOrUndefined),number:t.fromCLI.defaultNumber.pipe(T.getOrUndefined),boolean:t.fromCLI.defaultBoolean.pipe(T.getOrUndefined)}),g={...c,...e},s={builderSuffix:yield*i("builderSuffix"),include:yield*i("include"),fileSuffix:yield*i("fileSuffix"),jsdocTag:yield*i("jsdocTag"),outputDir:yield*i("outputDir"),defaults:{...M.defaults,...g}};return yield*C.logDebug(`[Configuration]: Resolving config with value: ${JSON.stringify(s,null,4)}`),s}),ft=(t)=>(i)=>t.fromCLI[i].pipe(C.orElse(()=>T.flatMap(t.fromConfigFile,(c)=>T.fromNullable(c[i]))),C.orElseSucceed(()=>M[i])),lt=(t)=>C.gen(function*(){let i=yield*pe.FileSystem;if(yield*C.orDie(i.exists(t))){yield*C.logDebug("[Configuration]: Found config file - attempting to read it");let e=yield*dt(t),g=yield*r.decodeUnknown(he)(e);return T.some(g)}else return yield*C.logDebug("[Configuration]: No config file found"),T.none()}),dt=(t)=>C.gen(function*(){let i=yield*pe.FileSystem,c=yield*C.orDie(i.readFileString(t));return yield*C.try({try:()=>JSON.parse(c),catch:(e)=>`[FileSystem] Unable to read and parse JSON file from '${t}': ${String(e)}`})}).pipe(C.orDie);import*as Ne from"@effect/platform/FileSystem";import*as Oe from"@effect/platform/Path";import*as k from"effect/Effect";import*as x from"effect/Match";import{Project as mt}from"ts-morph";var ne=(t)=>t.replace(/([a-z0-9])([A-Z])/g,"$1-$2").replace(/([A-Z])([A-Z][a-z])/g,"$1-$2").replace(/_/g,"-").toLowerCase(),Se=(t)=>{return t.split(/[-_\s]+/).flatMap((i)=>i.split(/(?=[A-Z])/)).filter(Boolean).map((i)=>i.charAt(0).toUpperCase()+i.slice(1).toLowerCase()).join("")},ye=(t)=>{return t.split(/[-_\s]+/).flatMap((c)=>c.split(/(?=[A-Z])/)).filter(Boolean).map((c,e)=>{let g=c.toLowerCase();return e===0?g:g.charAt(0).toUpperCase()+g.slice(1)}).join("")};var Be=(t)=>{let{fieldName:i,optional:c,typeName:e,isNestedBuilder:g}=t,s=i.replaceAll("'","").replaceAll('"',""),o=ye(s),m=`with${Se(s)}`,d=[`return this.with({ ${i}: ${o} });`],u=[`return this.with({ ${i}: ${o}.build() });`],a=g?u:d,n=[`if (!${o}) {`,` const { "${s}": _unused, ...rest } = this.build();`," return this.with(rest);","}"],S=c?[...n,...a]:a,h=`${e}['${s}']`;return{name:m,isPublic:!0,parameters:[{name:o,type:g?`DataBuilder<${h}>`:h}],statements:S}};class ae extends k.Service()("@TSDataBuilders/BuilderGenerator",{effect:k.gen(function*(){let t=yield*Ne.FileSystem,i=yield*Oe.Path,c=yield*$,e=yield*N,{fileSuffix:g,builderSuffix:s,defaults:o}=e,m=(d)=>x.value(d).pipe(x.when({kind:"STRING"},()=>`"${o.string}"`),x.when({kind:"NUMBER"},()=>o.number),x.when({kind:"BOOLEAN"},()=>o.boolean),x.when({kind:"UNDEFINED"},()=>"undefined"),x.when({kind:"DATE"},()=>"new Date()"),x.when({kind:"ARRAY"},()=>"[]"),x.when({kind:"LITERAL"},(u)=>u.literalValue),x.when({kind:"TYPE_CAST"},(u)=>m(u.baseTypeMetadata)),x.when({kind:"TUPLE"},(u)=>{return`[${u.members.map((n)=>m(n)).map((n)=>`${n}`).join(", ")}]`}),x.when({kind:"TYPE_LITERAL"},(u)=>{return`{${Object.entries(u.metadata).filter(([n,{optional:S}])=>!S).map(([n,S])=>`${n}: ${m(S)}`).join(", ")}}`}),x.when({kind:"RECORD"},(u)=>{if(u.keyType.kind==="STRING"||u.keyType.kind==="NUMBER")return"{}";let a=m(u.keyType),n=m(u.valueType);return`{${a}: ${n}}`}),x.when({kind:"UNION"},(u)=>{let n=u.members.slice().sort((S,h)=>{let b=ke.indexOf(S.kind),O=ke.indexOf(h.kind);return(b===-1?1/0:b)-(O===-1?1/0:O)})[0];if(!n)return"never";return m(n)}),x.when({kind:"BUILDER"},(u)=>{return`new ${u.name}${s}().build()`}),x.exhaustive);return{generateBaseBuilder:k.fnUntraced(function*(){let d=i.join(yield*c.cwd,e.outputDir),u=i.resolve(d,"data-builder.ts");yield*k.logDebug(`[Builders]: Creating base builder at ${u}`),yield*k.orDie(t.writeFileString(u,pt))}),generateBuilder:k.fnUntraced(function*(d){let u=new mt,a=d.name;yield*k.logDebug(`[Builders]: Creating builder for ${a}`);let n=i.join(yield*c.cwd,e.outputDir),S=i.resolve(n,`${ne(a)}${g}.ts`);yield*k.logDebug(`[Builders]: Creating builder file at ${S}`);let h=u.createSourceFile(S,"",{overwrite:!0}),b=i.resolve(d.path),O=i.relative(i.dirname(S),b).replace(/\.ts$/,"");if(h.addImportDeclaration({namedImports:[a],isTypeOnly:!0,moduleSpecifier:O}),h.addImportDeclaration({namedImports:["DataBuilder"],moduleSpecifier:"./data-builder"}),d.shape.kind!=="TYPE_LITERAL")return yield*k.dieMessage("[BuilderGenerator]: Expected root type to be type literal");[...new Set(gt(d.shape.metadata))].forEach((v)=>{h.addImportDeclaration({namedImports:[`${v}${s}`],moduleSpecifier:`./${ne(v)}${g}`})});let Y=Object.entries(d.shape.metadata).filter(([v,{optional:j}])=>!j).map(([v,j])=>`${v}: ${j.kind==="TYPE_CAST"?`${m(j)} as ${a}['${v}']`:m(j)}`),K=Object.entries(d.shape.metadata).map(([v,{optional:j,kind:me}])=>Be({fieldName:v,optional:j,typeName:a,isNestedBuilder:me==="BUILDER"})),J=`{
3
+ ${Y.join(`,
4
4
  `)}
5
- }`;l.addClass({name:`${y}${p}`,isExported:!0,extends:`DataBuilder<${y}>`,methods:[{name:"constructor",statements:[`super(${z});`]},...w]}),l.saveSync()})}})}){}var Te=["UNDEFINED","BOOLEAN","NUMBER","STRING","DATE","LITERAL","TYPE_LITERAL","ARRAY","TUPLE","RECORD"],ot=`export abstract class DataBuilder<T> {
5
+ }`;h.addClass({name:`${a}${s}`,isExported:!0,extends:`DataBuilder<${a}>`,methods:[{name:"constructor",statements:[`super(${J});`]},...K]}),yield*k.logDebug(`[Builders]: Saving builder content at ${S}`),h.saveSync()})}})}){}var ke=["UNDEFINED","BOOLEAN","NUMBER","STRING","DATE","LITERAL","TYPE_LITERAL","ARRAY","TUPLE","RECORD"],pt=`export abstract class DataBuilder<T> {
6
6
  private data: T;
7
7
 
8
8
  constructor(initialData: T) {
@@ -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("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);
21
+ `;function gt(t){let i=[];function c(e){switch(e.kind){case"BUILDER":i.push(e.name);break;case"TYPE_LITERAL":Object.values(e.metadata).forEach(c);break;case"UNION":case"TUPLE":e.members.forEach(c);break;case"RECORD":c(e.keyType),c(e.valueType);break}}return Object.values(t).forEach(c),i}class z extends B.Service()("@TSDataBuilders/Builders",{effect:B.gen(function*(){let t=yield*$e.FileSystem,i=yield*ae,c=yield*$,e=yield*we.Path,g=yield*N;return{create:B.fnUntraced(function*(s){let o=e.join(yield*c.cwd,g.outputDir);if(yield*B.orDie(t.exists(o)))yield*B.logDebug(`[Builders]: Removing already existing output directory at ${o}`),yield*B.orDie(t.remove(o,{recursive:!0}));yield*B.logDebug(`[Builders]: Creating output directory at ${o}`),yield*B.orDie(t.makeDirectory(o,{recursive:!0})),yield*i.generateBaseBuilder();let d=s.map((n)=>n.name),u=d.filter((n,S)=>d.indexOf(n)!==S),a=[...new Set(u)];if(u.length>0)return yield*B.dieMessage(`Duplicated builders: ${a.join(", ")}`);yield*B.all(s.map((n)=>i.generateBuilder(n)),{concurrency:"unbounded"})})}}),dependencies:[ae.Default]}){}import*as l from"@effect/cli/Options";var St=l.text("jsdoc-tag").pipe(l.withDescription(D.jsdocTag),l.withSchema(R.fields.jsdocTag),l.optional),yt=l.text("output-dir").pipe(l.withAlias("o"),l.withDescription(D.outputDir),l.withSchema(R.fields.outputDir),l.optional),Et=l.text("include").pipe(l.withAlias("i"),l.withDescription(D.include),l.withSchema(R.fields.include),l.optional),bt=l.text("file-suffix").pipe(l.withDescription(D.fileSuffix),l.withSchema(R.fields.fileSuffix),l.optional),Ct=l.text("builder-suffix").pipe(l.withDescription(D.builderSuffix),l.withSchema(R.fields.builderSuffix),l.optional),Dt=l.text("default-string").pipe(l.withDescription(D.defaultString),l.withSchema(R.fields.defaultString),l.optional),Tt=l.text("default-number").pipe(l.withDescription(D.defaultNumber),l.withSchema(R.fields.defaultNumber),l.optional),xt=l.text("default-boolean").pipe(l.withDescription(D.defaultBoolean),l.withSchema(R.fields.defaultBoolean),l.optional),Ee={jsdocTag:St,outputDir:yt,include:Et,fileSuffix:bt,builderSuffix:Ct,defaultString:Dt,defaultNumber:Tt,defaultBoolean:xt};import*as Pe from"@effect/platform/FileSystem";import*as Ue from"@effect/platform/Path";import*as ve from"effect/Effect";import*as L from"effect/Option";import*as Ae from"effect/Schema";var Ie=(t)=>ve.gen(function*(){let i=yield*$,c=yield*Pe.FileSystem,e=yield*i.cwd,s=(yield*Ue.Path).join(e,ge),o=ee({string:t.defaultString.pipe(L.getOrUndefined),number:t.defaultNumber.pipe(L.getOrUndefined),boolean:t.defaultBoolean.pipe(L.getOrUndefined)}),m=yield*Ae.decode(he)({$schema:"https://raw.githubusercontent.com/nemmtor/ts-databuilders/refs/heads/main/schema.json",builderSuffix:t.builderSuffix.pipe(L.getOrElse(()=>M.builderSuffix)),fileSuffix:t.fileSuffix.pipe(L.getOrElse(()=>M.fileSuffix)),include:t.include.pipe(L.getOrElse(()=>M.include)),jsdocTag:t.jsdocTag.pipe(L.getOrElse(()=>M.jsdocTag)),outputDir:t.outputDir.pipe(L.getOrElse(()=>M.outputDir)),defaults:{...M.defaults,...o}});yield*c.writeFileString(s,JSON.stringify(m,null,2))});import*as _ from"effect/Chunk";import*as F from"effect/Effect";import*as _e from"effect/Function";import*as V from"effect/Option";import*as ce from"effect/Stream";import*as Le from"effect/Data";import*as G from"effect/Effect";import*as je from"effect/Stream";import{glob as Ft}from"glob";class te extends G.Service()("@TSDataBuilders/TreeWalker",{effect:G.gen(function*(){let t=yield*$;return{walk:G.fnUntraced(function*(i){let c=yield*t.cwd;return yield*G.logDebug(`[TreeWalker]: Walking path: ${c}/${i}`),je.fromAsyncIterable(Ft.stream(i,{cwd:c,nodir:!0}),(e)=>new Me({cause:e}))})}})}){}class Me extends Le.TaggedError("TreeWalkerError"){}import*as Re from"@effect/platform/FileSystem";import*as A from"effect/Effect";import*as P from"effect/Stream";class se extends A.Service()("@TSDataBuilders/FileContentChecker",{effect:A.gen(function*(){let t=yield*Re.FileSystem,i=new TextDecoder;return{check:A.fnUntraced(function*(c){let{content:e,filePath:g}=c;return yield*A.logDebug(`[FileContentChecker](${g}): Checking file content`),yield*P.orDie(t.stream(g,{chunkSize:16384})).pipe(P.map((m)=>i.decode(m,{stream:!0})),P.mapAccum("",(m,d)=>{let u=m+d;return[u.slice(-e.length+1),u.includes(e)]}),P.find(Boolean),P.tap((m)=>m?A.logDebug(`[FileContentChecker](${g}): found expected content`):A.void),P.runCollect)})}})}){}class H extends F.Service()("@TSDataBuilders/Finder",{effect:F.gen(function*(){let t=yield*se,i=yield*te,{include:c,jsdocTag:e}=yield*N,g=`@${e}`;return{find:F.fnUntraced(function*(){yield*F.logDebug("[Finder]: Attempting to find files with builders");let o=yield*(yield*i.walk(c)).pipe(ce.mapEffect((m)=>t.check({filePath:m,content:g}).pipe(F.map(_.map((d)=>d?V.some(m):V.none()))),{concurrency:"unbounded"}),ce.runCollect,F.map(_.flatMap(_e.identity)),F.map(_.filter((m)=>V.isSome(m))),F.map(_.map((m)=>m.value)));return yield*F.logDebug(`[Finder]: Found builders in files: ${o.pipe(_.toArray).join(", ")}`),o},F.catchTag("TreeWalkerError",(s)=>F.die(s)))}}),dependencies:[te.Default,se.Default]}){}import*as Je from"@effect/platform/FileSystem";import*as q from"effect/Data";import*as p from"effect/Effect";import*as re from"effect/Either";import{Project as Ot,SyntaxKind as ue}from"ts-morph";import*as W from"effect/Data";import*as f from"effect/Effect";import*as y from"effect/Match";import*as Ge from"effect/Option";import{Node as Nt,SyntaxKind as E}from"ts-morph";import{randomUUID as kt}from"node:crypto";import*as fe from"effect/Effect";class le extends fe.Service()("@TSDataBuilders/RandomUUID",{succeed:{generate:fe.sync(()=>kt())}}){}class de extends f.Service()("@TSDataBuilders/TypeNodeParser",{effect:f.gen(function*(){let{jsdocTag:t}=yield*N,i=yield*le,c=(g)=>f.gen(function*(){let{type:s,contextNode:o,optional:m}=g,d=s.getProperties();if(!s.isObject()||d.length===0)return yield*new Ke({raw:s.getText(),kind:o.getKind()});let u={};for(let a of d){let n=a.getName(),S=a.getTypeAtLocation(o),h=a.isOptional(),b=o.getProject().createSourceFile(`__temp_${yield*i.generate}.ts`,`type __T = ${S.getText()}`,{overwrite:!0}),O=b.getTypeAliasOrThrow("__T").getTypeNodeOrThrow(),I=yield*f.suspend(()=>e({typeNode:O,optional:h}));u[n]=I,o.getProject().removeSourceFile(b)}return{kind:"TYPE_LITERAL",metadata:u,optional:m}}),e=(g)=>f.gen(function*(){let{typeNode:s,optional:o}=g,m=s.getKind(),d=y.value(m).pipe(y.when(y.is(E.StringKeyword),()=>f.succeed({kind:"STRING",optional:o})),y.when(y.is(E.NumberKeyword),()=>f.succeed({kind:"NUMBER",optional:o})),y.when(y.is(E.BooleanKeyword),()=>f.succeed({kind:"BOOLEAN",optional:o})),y.when(y.is(E.UndefinedKeyword),()=>f.succeed({kind:"UNDEFINED",optional:o})),y.when(y.is(E.ArrayType),()=>f.succeed({kind:"ARRAY",optional:o})),y.when(y.is(E.LiteralType),()=>f.succeed({kind:"LITERAL",literalValue:s.asKindOrThrow(E.LiteralType).getLiteral().getText(),optional:o})),y.when(y.is(E.TypeLiteral),()=>f.gen(function*(){let n=s.asKindOrThrow(E.TypeLiteral).getMembers();return{kind:"TYPE_LITERAL",metadata:yield*f.reduce(n,{},(h,b)=>f.gen(function*(){if(!b.isKind(E.PropertySignature))return h;let O=b.getTypeNode();if(!O)return h;let I=b.getNameNode().getText(),Y=b.hasQuestionToken(),K=yield*f.suspend(()=>e({typeNode:O,optional:Y}));return{...h,[I]:K}})),optional:o}})),y.when(y.is(E.ImportType),()=>f.gen(function*(){let a=s.asKindOrThrow(E.ImportType),n=a.getType(),S=n.getSymbol();if(!S)return yield*f.die(new We);let h=S.getDeclarations();if(h&&h.length>1)return yield*f.die(new be);let[b]=h;if(!b)return yield*f.die(new Ce);return yield*c({type:n,contextNode:a,optional:o})})),y.when(y.is(E.TupleType),()=>f.gen(function*(){let n=s.asKindOrThrow(E.TupleType).getElements(),S=yield*f.all(n.map((h)=>f.suspend(()=>e({typeNode:h,optional:!1}))),{concurrency:"unbounded"});return{kind:"TUPLE",optional:o,members:S}})),y.when(y.is(E.TypeReference),()=>f.gen(function*(){let a=s.asKindOrThrow(E.TypeReference),n=a.getTypeName().getText();if(n==="Date")return{kind:"DATE",optional:o};if(n==="Array")return{kind:"ARRAY",optional:o};let S=a.getTypeArguments();if(n==="Record"){let[J,v]=S;if(!J||!v)return yield*new ie({kind:m,raw:s.getText()});let j=yield*f.suspend(()=>e({typeNode:J,optional:!1})),me=yield*f.suspend(()=>e({typeNode:v,optional:!1}));return{kind:"RECORD",keyType:j,valueType:me,optional:o}}if(["Pick","Omit","Partial","Required","Readonly","Extract","NonNullable"].includes(n))return yield*c({type:a.getType(),contextNode:a,optional:o});let b=a.getType().getAliasSymbol();if(!b)return yield*c({type:a.getType(),contextNode:a,optional:o});let O=b.getDeclarations();if(O&&O.length>1)return yield*f.die(new be);let[I]=O;if(!I)return yield*f.die(new Ce);let Y=b?.getJsDocTags().map((J)=>J.getName()).includes(t);if(!Nt.isTypeAliasDeclaration(I))return yield*f.die(new Ye);let K=I.getTypeNode();if(!K)return yield*new ie({kind:m,raw:s.getText()});if(!Y)return yield*f.suspend(()=>e({typeNode:K,optional:o}));return{kind:"BUILDER",name:I.getName(),optional:o}})),y.when(y.is(E.UnionType),()=>f.gen(function*(){let a=yield*f.all(s.asKindOrThrow(E.UnionType).getTypeNodes().map((n)=>f.suspend(()=>e({typeNode:n,optional:!1}))),{concurrency:"unbounded"});return{kind:"UNION",optional:o,members:a}})),y.when(y.is(E.IntersectionType),()=>f.gen(function*(){let n=s.asKindOrThrow(E.IntersectionType).getTypeNodes(),S=[E.StringKeyword,E.NumberKeyword,E.BooleanKeyword],h=n.find((b)=>S.includes(b.getKind()));if(h&&n.length>1)return{kind:"TYPE_CAST",baseTypeMetadata:yield*f.suspend(()=>e({typeNode:h,optional:!1})),optional:o};return yield*new ie({kind:m,raw:s.getText()})})),y.option);if(Ge.isNone(d))return yield*new ie({kind:m,raw:s.getText()});return yield*d.value});return{generateMetadata:e}}),dependencies:[le.Default]}){}class ie extends W.TaggedError("UnsupportedSyntaxKindError"){}class be extends W.TaggedError("MultipleSymbolDeclarationsError"){}class Ce extends W.TaggedError("MissingSymbolDeclarationError"){}class We extends W.TaggedError("MissingSymbolError"){}class Ye extends W.TaggedError("UnsupportedTypeAliasDeclaration"){}class Ke extends W.TaggedError("CannotBuildTypeReferenceMetadata"){}class Q extends p.Service()("@TSDataBuilders/Parser",{effect:p.gen(function*(){let t=yield*Je.FileSystem,i=yield*de,{jsdocTag:c}=yield*N;return{generateBuildersMetadata:p.fnUntraced(function*(e){yield*p.logDebug(`[Parser](${e}): Generating builder metadata`),yield*p.logDebug(`[Parser](${e}): Reading source code`);let g=yield*p.orDie(t.readFileString(e)),s=yield*p.try({try:()=>{return new Ot().createSourceFile(e,g,{overwrite:!0}).getTypeAliases().filter((n)=>n.getJsDocs().flatMap((S)=>S.getTags().flatMap((h)=>h.getTagName())).includes(c)).map((n)=>{let S=n.getName();if(!n.isExported())return re.left(new ze({path:e,typeName:S}));let h=n.getTypeNode();if(!(h?.isKind(ue.TypeLiteral)||h?.isKind(ue.TypeReference)))return re.left(new Ve({path:e,typeName:n.getName()}));return re.right({name:n.getName(),node:h})}).filter(Boolean)},catch:(d)=>new Ze({cause:d})}),o=yield*p.all(s.map((d)=>d),{concurrency:"unbounded"});return yield*p.logDebug(`[Parser](${e}): Generating metadata for types: ${o.map(({name:d})=>d).join(", ")}`),yield*p.all(o.map(({name:d,node:u})=>i.generateMetadata({typeNode:u,optional:!1}).pipe(p.tap(()=>p.logDebug(`[Parser](${e}): Finished generating metadata for type: ${d}`)),p.map((a)=>({name:d,shape:a,path:e})),p.catchTags({UnsupportedSyntaxKindError:(a)=>p.fail(new He({kind:a.kind,raw:a.raw,path:e,typeName:d})),CannotBuildTypeReferenceMetadata:(a)=>p.fail(new qe({kind:a.kind,raw:a.raw,path:e,typeName:d}))}))),{concurrency:"unbounded"})},p.catchTags({ParserError:(e)=>p.die(e),UnexportedDatabuilderError:(e)=>p.dieMessage(`[Parser](${e.path}): Unexported databuilder ${e.typeName}`),RichUnsupportedSyntaxKindError:(e)=>p.dieMessage(`[Parser](${e.path}): Unsupported syntax kind of id: ${e.kind} with raw type: ${e.raw} found in type ${e.typeName}`),RichCannotBuildTypeReferenceMetadata:(e)=>p.dieMessage(`[Parser](${e.path}): Cannot build type reference metadata with kind of id: ${e.kind} with raw type: ${e.raw} found in type ${e.typeName}. Is it a root of databuilder?`),UnsupportedBuilderTypeError:(e)=>p.dieMessage(`[Parser](${e.path}): Unsupported builder type ${e.typeName}`)}))}}),dependencies:[de.Default]}){}class Ze extends q.TaggedError("ParserError"){}class ze extends q.TaggedError("UnexportedDatabuilderError"){}class Ve extends q.TaggedError("UnsupportedBuilderTypeError"){}class He extends q.TaggedError("RichUnsupportedSyntaxKindError"){}class qe extends q.TaggedError("RichCannotBuildTypeReferenceMetadata"){}import*as Qe from"effect/Chunk";import*as w from"effect/Effect";import*as Xe from"effect/Function";var et=w.gen(function*(){let t=yield*H,i=yield*Q,c=yield*z;yield*w.logInfo("[TSDatabuilders]: Generating builders for your project.");let e=yield*t.find();yield*w.logInfo(`[TSDatabuilders]: Found builders in ${e.length} file(s).`),yield*w.logDebug("[TSDatabuilders]: Attempting to generate builders metadata");let g=yield*w.all(Qe.map(e,(s)=>i.generateBuildersMetadata(s)),{concurrency:"unbounded"}).pipe(w.map((s)=>s.flatMap(Xe.identity)));if(g.length===0)return;yield*w.logDebug("[TSDatabuilders]: Attempting to create builders files"),yield*c.create(g),yield*w.logInfo(`[TSDatabuilders]: Created ${g.length} builder(s).`)});var wt=U.make("init",Ee).pipe(U.withHandler(Ie)),Pt=U.make("ts-databuilders",Ee),tt=Pt.pipe(U.withHandler(()=>et),U.withSubcommands([wt]),U.provide((t)=>X.mergeAll(H.Default,Q.Default,z.Default).pipe(X.provide(X.effect(N,Fe(t))))),U.run({name:"Typescript Databuilders generator",version:"v0.0.1"}));var vt=ot.mergeAll(nt.minimumLogLevel(at.Debug),$.Default,it.layer);tt(process.argv).pipe(Ut.provide(vt),rt.runMain);
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@nemmtor/ts-databuilders",
4
+ "version": "0.0.1-alpha.7",
4
5
  "type": "module",
5
6
  "private": false,
6
7
  "description": "CLI tool that automatically generates builder classes from annotated TypeScript types.",
@@ -27,7 +28,7 @@
27
28
  ],
28
29
  "scripts": {
29
30
  "build:node": "bun build src/main.ts --target node --outfile dist/main.js --production --packages external",
30
- "start": "bun --console-depth 10 src/main.ts",
31
+ "start": "bun src/main.ts",
31
32
  "format": "biome format",
32
33
  "format:fix": "biome format --write",
33
34
  "lint": "biome lint",
@@ -57,6 +58,5 @@
57
58
  "effect": "^3.18.4",
58
59
  "glob": "^11.0.3",
59
60
  "ts-morph": "^27.0.2"
60
- },
61
- "version": "0.0.1-alpha.5"
61
+ }
62
62
  }