@atproto/lex-builder 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/dist/lex-builder.d.ts +34 -0
- package/dist/lex-builder.d.ts.map +1 -1
- package/dist/lex-builder.js +18 -9
- package/dist/lex-builder.js.map +1 -1
- package/dist/lex-def-builder.d.ts +6 -13
- package/dist/lex-def-builder.d.ts.map +1 -1
- package/dist/lex-def-builder.js +143 -131
- package/dist/lex-def-builder.js.map +1 -1
- package/package.json +2 -2
- package/src/lex-builder.ts +53 -9
- package/src/lex-def-builder.ts +171 -157
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @atproto/lex-builder
|
|
2
2
|
|
|
3
|
+
## 0.1.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#4981](https://github.com/bluesky-social/atproto/pull/4981) [`3aae4fe`](https://github.com/bluesky-social/atproto/commit/3aae4fe43448dca860fc6c4a24d18bfa64de084b) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Remove need for inference when computing XRPC method derived types
|
|
8
|
+
|
|
9
|
+
- [#4984](https://github.com/bluesky-social/atproto/pull/4984) [`5bdb4ad`](https://github.com/bluesky-social/atproto/commit/5bdb4addd6a3798bf6c8c391c74044b3e251008a) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Add `default` export of "main" schema in the namespace file
|
|
10
|
+
|
|
11
|
+
- [#4981](https://github.com/bluesky-social/atproto/pull/4981) [`3aae4fe`](https://github.com/bluesky-social/atproto/commit/3aae4fe43448dca860fc6c4a24d18bfa64de084b) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Export `lexiconMessageSchema` (and type) for subscription messages
|
|
12
|
+
|
|
13
|
+
- [#4979](https://github.com/bluesky-social/atproto/pull/4979) [`314df62`](https://github.com/bluesky-social/atproto/commit/314df62537bb519231aa375dd3a38360afc79ce0) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Always add `#__PURE__` annotations to function calls
|
|
14
|
+
|
|
15
|
+
- [#4983](https://github.com/bluesky-social/atproto/pull/4983) [`1259646`](https://github.com/bluesky-social/atproto/commit/125964673962a86282a05bf22f410fad8ad06b41) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Make the generation of the `$defs` namespace optional by default
|
|
16
|
+
|
|
17
|
+
## 0.1.1
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- [#4972](https://github.com/bluesky-social/atproto/pull/4972) [`fb1b403`](https://github.com/bluesky-social/atproto/commit/fb1b40350d46f3c49e512b7e24b071b03902e3b8) Thanks [@matthieusieben](https://github.com/matthieusieben)! - Fix XRPC methods being non tree-shakable
|
|
22
|
+
|
|
3
23
|
## 0.1.0
|
|
4
24
|
|
|
5
25
|
### Minor Changes
|
package/dist/lex-builder.d.ts
CHANGED
|
@@ -31,6 +31,40 @@ export type LexBuilderOptions = LexDefBuilderOptions & {
|
|
|
31
31
|
* @default '.ts'
|
|
32
32
|
*/
|
|
33
33
|
fileExt?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Whether to export the whole defs file as a namespace export (`export * as
|
|
36
|
+
* $defs from './xyz.defs.js'`). This is useful to have an escape hatch to
|
|
37
|
+
* access the definitions in case of name conflicts with child namespaces.
|
|
38
|
+
*
|
|
39
|
+
* For example if two documents with if `com.example.foo` and
|
|
40
|
+
* `com.example.foo.bar` coexist, the `com.example.foo.bar` namespace would
|
|
41
|
+
* shadow any `bar` definition from the `com.example.foo` namespace. In that
|
|
42
|
+
* case, having the `$defs` namespace export allows to still access those
|
|
43
|
+
* definitions via `com.example.foo.$defs.bar`.
|
|
44
|
+
*
|
|
45
|
+
* @note enabling this will negatively impact bundle size because and
|
|
46
|
+
* additional namespace object will be generated for each lexicon document.
|
|
47
|
+
*
|
|
48
|
+
* @default false
|
|
49
|
+
*/
|
|
50
|
+
defsExport?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Whether to generate a default export for the "main" lexicon definition
|
|
53
|
+
* schema in the parent namespace file.
|
|
54
|
+
*
|
|
55
|
+
* This allows simpler access the main schema when importing directly from the
|
|
56
|
+
* file instead of using the full namespace as in:
|
|
57
|
+
* `com.atproto.repo.getRecord`.
|
|
58
|
+
*
|
|
59
|
+
* ```ts
|
|
60
|
+
* import getRecord from './com/atproto/repo/getRecord.js'
|
|
61
|
+
* // instead of
|
|
62
|
+
* import { main as getRecord } from './com/atproto/repo/getRecord.js'
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @default false
|
|
66
|
+
*/
|
|
67
|
+
defaultExport?: boolean;
|
|
34
68
|
};
|
|
35
69
|
/**
|
|
36
70
|
* Options for loading lexicon documents into the builder.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lex-builder.d.ts","sourceRoot":"","sources":["../src/lex-builder.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,kBAAkB,EAAe,MAAM,aAAa,CAAA;AAE7D,OAAO,EAAa,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAC5D,OAAO,EAAiB,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AAC1E,OAAO,EAEL,8BAA8B,EAC/B,MAAM,gCAAgC,CAAA;AAGvC;;;;;;;GAOG;AACH,MAAM,MAAM,iBAAiB,GAAG,oBAAoB,GAAG;IACrD;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"lex-builder.d.ts","sourceRoot":"","sources":["../src/lex-builder.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,kBAAkB,EAAe,MAAM,aAAa,CAAA;AAE7D,OAAO,EAAa,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AAC5D,OAAO,EAAiB,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AAC1E,OAAO,EAEL,8BAA8B,EAC/B,MAAM,gCAAgC,CAAA;AAGvC;;;;;;;GAOG;AACH,MAAM,MAAM,iBAAiB,GAAG,oBAAoB,GAAG;IACrD;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB;;;;;;;;;;;;;;;OAeG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB;;;;;;;;;;;;;;;OAeG;IACH,aAAa,CAAC,EAAE,OAAO,CAAA;CACxB,CAAA;AAED;;;;;;;;GAQG;AACH,MAAM,MAAM,qBAAqB,GAAG,8BAA8B,GAChE,kBAAkB,CAAA;AAEpB;;;;GAIG;AACH,MAAM,MAAM,qBAAqB,GAAG,gBAAgB,GAAG;IACrD;;OAEG;IACH,GAAG,EAAE,MAAM,CAAA;IACX;;;;;;OAMG;IACH,KAAK,CAAC,EAAE,OAAO,CAAA;IACf;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAA;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,UAAU;;IAOT,OAAO,CAAC,QAAQ,CAAC,OAAO;gBAAP,OAAO,GAAE,iBAAsB;IAE5D,IAAI,OAAO,WAEV;IAED,IAAI,SAAS,WAEZ;IAEY,IAAI,CAAC,OAAO,EAAE,qBAAqB;IAkBnC,IAAI,CAAC,OAAO,EAAE,qBAAqB;IA4BhD,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,OAAO;YAID,gBAAgB;YAmEhB,cAAc;CAU7B"}
|
package/dist/lex-builder.js
CHANGED
|
@@ -128,15 +128,24 @@ export class LexBuilder {
|
|
|
128
128
|
file.addExportDeclaration({
|
|
129
129
|
moduleSpecifier: `./${namespaces.at(-1)}.defs${this.importExt}`,
|
|
130
130
|
});
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
131
|
+
if (this.options.defsExport) {
|
|
132
|
+
// @NOTE Individual exports exports from the defs file might conflict with
|
|
133
|
+
// child namespaces. For this reason, we also add a namespace export for the
|
|
134
|
+
// defs (export * as $defs from './xyz.defs.js'). This is an escape hatch
|
|
135
|
+
// allowing to still access the definitions if a hash get shadowed by a
|
|
136
|
+
// child namespace.
|
|
137
|
+
file.addExportDeclaration({
|
|
138
|
+
moduleSpecifier: `./${namespaces.at(-1)}.defs${this.importExt}`,
|
|
139
|
+
namespaceExport: '$defs',
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
if (this.options.defaultExport && doc.defs.main != null) {
|
|
143
|
+
// export { main as default } from './xyz.defs.js'
|
|
144
|
+
file.addExportDeclaration({
|
|
145
|
+
moduleSpecifier: `./${namespaces.at(-1)}.defs${this.importExt}`,
|
|
146
|
+
namedExports: [{ name: 'main', alias: 'default' }],
|
|
147
|
+
});
|
|
148
|
+
}
|
|
140
149
|
}
|
|
141
150
|
async createDefsFile(doc, indexer) {
|
|
142
151
|
const path = join('/', ...doc.id.split('.'));
|
package/dist/lex-builder.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lex-builder.js","sourceRoot":"","sources":["../src/lex-builder.ts"],"names":[],"mappings":";AAAA,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AAC7D,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACzC,OAAO,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AAEnD,OAAO,EAAsB,WAAW,EAAE,MAAM,aAAa,CAAA;AAC7D,OAAO,EAAE,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AAE;YAC/D,eAAe,EAAE,OAAO;SACzB,CAAC,CAAA;IACJ,CAAC;IAEO,KAAK,CAAC,cAAc,CAC1B,GAAoB,EACpB,OAAuB;QAEvB,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAA;QAC5C,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,IAAI,QAAQ,IAAI,CAAC,OAAO,EAAE,CAAC,CAAA;QAE3D,MAAM,WAAW,GAAG,IAAI,aAAa,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,CAAA;QACvE,MAAM,WAAW,CAAC,KAAK,EAAE,CAAA;IAC3B,CAAC;CACF;AAED,KAAK,UAAU,mBAAmB,CAAC,IAAY;IAC7C,IAAI,CAAC;QACH,MAAM,IAAI,CAAC,IAAI,CAAC,CAAA;QAChB,MAAM,IAAI,KAAK,CAAC,wBAAwB,IAAI,EAAE,CAAC,CAAA;IACjD,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,GAAG,YAAY,KAAK,IAAI,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,IAAI,KAAK,QAAQ;YAAE,OAAM;QAC1E,MAAM,GAAG,CAAA;IACX,CAAC;AACH,CAAC","sourcesContent":["import assert from 'node:assert'\nimport { mkdir, rm, stat, writeFile } from 'node:fs/promises'\nimport { join, resolve } from 'node:path'\nimport { IndentationText, Project } from 'ts-morph'\nimport { LexiconDocument, LexiconIndexer } from '@atproto/lex-document'\nimport { BuildFilterOptions, buildFilter } from './filter.js'\nimport { FilteredIndexer } from './filtered-indexer.js'\nimport { Formatter, FormatterOptions } from './formatter.js'\nimport { LexDefBuilder, LexDefBuilderOptions } from './lex-def-builder.js'\nimport {\n LexiconDirectoryIndexer,\n LexiconDirectoryIndexerOptions,\n} from './lexicon-directory-indexer.js'\nimport { asNamespaceExport } from './ts-lang.js'\n\n/**\n * Configuration options for the {@link LexBuilder} class.\n *\n * Extends {@link LexDefBuilderOptions} with additional settings for\n * controlling the generated TypeScript project structure.\n *\n * @see {@link LexDefBuilderOptions} for definition generation options\n */\nexport type LexBuilderOptions = LexDefBuilderOptions & {\n /**\n * Whether to generate an index file at the root exporting all top-level\n * namespaces.\n *\n * @note This could theoretically cause name conflicts if a\n * @default false\n */\n indexFile?: boolean\n /**\n * The file extension to use for import specifiers in the generated code.\n *\n * @default '.js'\n */\n importExt?: string\n /**\n * The file extension to use for generated TypeScript files.\n *\n * @default '.ts'\n */\n fileExt?: string\n}\n\n/**\n * Options for loading lexicon documents into the builder.\n *\n * Combines directory indexing options with filtering options to control\n * which lexicon documents are processed.\n *\n * @see {@link LexiconDirectoryIndexerOptions} for directory scanning options\n * @see {@link BuildFilterOptions} for include/exclude filtering\n */\nexport type LexBuilderLoadOptions = LexiconDirectoryIndexerOptions &\n BuildFilterOptions\n\n/**\n * Options for saving generated TypeScript files.\n *\n * Combines formatting options with output directory configuration.\n */\nexport type LexBuilderSaveOptions = FormatterOptions & {\n /**\n * The output directory path where generated TypeScript files will be written.\n */\n out: string\n /**\n * Whether to clear the output directory before writing files.\n *\n * When `true`, the entire output directory is deleted before writing new files.\n *\n * @default false\n */\n clear?: boolean\n /**\n * Whether to allow overwriting existing files.\n *\n * When `false`, an error is thrown if any output file already exists.\n *\n * @default false\n */\n override?: boolean\n}\n\n/**\n * Main builder class for generating TypeScript schemas from Lexicon documents.\n *\n * The LexBuilder orchestrates the entire code generation process:\n * 1. Loading and indexing lexicon documents from the filesystem\n * 2. Generating TypeScript type definitions and runtime schemas\n * 3. Creating namespace export trees for convenient imports\n * 4. Saving formatted output files\n *\n * @example\n * ```ts\n * const builder = new LexBuilder({ indexFile: true, pretty: true })\n *\n * // Load lexicons from a directory\n * await builder.load({ lexicons: './lexicons' })\n *\n * // Save generated TypeScript to output directory\n * await builder.save({ out: './src/generated', clear: true })\n * ```\n */\nexport class LexBuilder {\n readonly #imported = new Set<string>()\n readonly #project = new Project({\n useInMemoryFileSystem: true,\n manipulationSettings: { indentationText: IndentationText.TwoSpaces },\n })\n\n constructor(private readonly options: LexBuilderOptions = {}) {}\n\n get fileExt() {\n return this.options.fileExt ?? '.ts'\n }\n\n get importExt() {\n return this.options.importExt ?? '.js'\n }\n\n public async load(options: LexBuilderLoadOptions) {\n await using indexer = new FilteredIndexer(\n new LexiconDirectoryIndexer(options),\n buildFilter(options),\n )\n\n for await (const doc of indexer) {\n if (!this.#imported.has(doc.id)) {\n this.#imported.add(doc.id)\n } else {\n throw new Error(`Duplicate lexicon document id: ${doc.id}`)\n }\n\n await this.createDefsFile(doc, indexer)\n await this.createExportTree(doc)\n }\n }\n\n public async save(options: LexBuilderSaveOptions) {\n const files = this.#project.getSourceFiles()\n\n const destination = resolve(options.out)\n\n if (options.clear) {\n await rm(destination, { recursive: true, force: true })\n } else if (!options.override) {\n await Promise.all(\n files.map(async (f) =>\n assertNotFileExists(join(destination, f.getFilePath())),\n ),\n )\n }\n\n const formatter = new Formatter(options)\n\n await Promise.all(\n Array.from(files, async (file) => {\n const filePath = join(destination, file.getFilePath())\n const content = await formatter.format(file.getFullText())\n await mkdir(join(filePath, '..'), { recursive: true })\n await rm(filePath, { recursive: true, force: true })\n await writeFile(filePath, content, 'utf8')\n }),\n )\n }\n\n private createFile(path: string) {\n return this.#project.createSourceFile(path)\n }\n\n private getFile(path: string) {\n return this.#project.getSourceFile(path) || this.createFile(path)\n }\n\n private async createExportTree(doc: LexiconDocument) {\n const namespaces = doc.id.split('.')\n\n if (this.options.indexFile) {\n const indexFile = this.getFile(`/index${this.fileExt}`)\n\n const tldNs = namespaces[0]!\n assert(\n tldNs !== 'index',\n 'The \"indexFile\" options cannot be used with namespaces using a \".index\" tld.',\n )\n const tldNsSpecifier = `./${tldNs}${this.importExt}`\n if (!indexFile.getExportDeclaration(tldNsSpecifier)) {\n indexFile.addExportDeclaration({\n moduleSpecifier: tldNsSpecifier,\n namespaceExport: asNamespaceExport(tldNs),\n })\n }\n }\n\n // First create the parent namespaces\n for (let i = 0; i < namespaces.length - 1; i++) {\n const currentNs = namespaces[i]\n const childNs = namespaces[i + 1]\n\n const path = join('/', ...namespaces.slice(0, i + 1))\n const file = this.getFile(`${path}${this.fileExt}`)\n\n const childModuleSpecifier = `./${currentNs}/${childNs}${this.importExt}`\n const dec = file.getExportDeclaration(childModuleSpecifier)\n if (!dec) {\n file.addExportDeclaration({\n moduleSpecifier: childModuleSpecifier,\n namespaceExport: asNamespaceExport(childNs),\n })\n }\n }\n\n // The child file exports the schemas (as *)\n const path = join('/', ...namespaces)\n const file = this.getFile(`${path}${this.fileExt}`)\n\n file.addExportDeclaration({\n moduleSpecifier: `./${namespaces.at(-1)}.defs${this.importExt}`,\n })\n\n // @NOTE Individual exports exports from the defs file might conflict with\n // child namespaces. For this reason, we also add a namespace export for the\n // defs (export * as $defs from './xyz.defs.js'). This is an escape hatch\n // allowing to still access the definitions if a hash get shadowed by a\n // child namespace.\n file.addExportDeclaration({\n moduleSpecifier: `./${namespaces.at(-1)}.defs${this.importExt}`,\n namespaceExport: '$defs',\n })\n }\n\n private async createDefsFile(\n doc: LexiconDocument,\n indexer: LexiconIndexer,\n ): Promise<void> {\n const path = join('/', ...doc.id.split('.'))\n const file = this.createFile(`${path}.defs${this.fileExt}`)\n\n const fileBuilder = new LexDefBuilder(this.options, file, doc, indexer)\n await fileBuilder.build()\n }\n}\n\nasync function assertNotFileExists(file: string): Promise<void> {\n try {\n await stat(file)\n throw new Error(`File already exists: ${file}`)\n } catch (err) {\n if (err instanceof Error && 'code' in err && err.code === 'ENOENT') return\n throw err\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"lex-builder.js","sourceRoot":"","sources":["../src/lex-builder.ts"],"names":[],"mappings":";AAAA,OAAO,MAAM,MAAM,aAAa,CAAA;AAChC,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM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CAAA;QAChB,MAAM,IAAI,KAAK,CAAC,wBAAwB,IAAI,EAAE,CAAC,CAAA;IACjD,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,GAAG,YAAY,KAAK,IAAI,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,IAAI,KAAK,QAAQ;YAAE,OAAM;QAC1E,MAAM,GAAG,CAAA;IACX,CAAC;AACH,CAAC","sourcesContent":["import assert from 'node:assert'\nimport { mkdir, rm, stat, writeFile } from 'node:fs/promises'\nimport { join, resolve } from 'node:path'\nimport { IndentationText, Project } from 'ts-morph'\nimport { LexiconDocument, LexiconIndexer } from '@atproto/lex-document'\nimport { BuildFilterOptions, buildFilter } from './filter.js'\nimport { FilteredIndexer } from './filtered-indexer.js'\nimport { Formatter, FormatterOptions } from './formatter.js'\nimport { LexDefBuilder, LexDefBuilderOptions } from './lex-def-builder.js'\nimport {\n LexiconDirectoryIndexer,\n LexiconDirectoryIndexerOptions,\n} from './lexicon-directory-indexer.js'\nimport { asNamespaceExport } from './ts-lang.js'\n\n/**\n * Configuration options for the {@link LexBuilder} class.\n *\n * Extends {@link LexDefBuilderOptions} with additional settings for\n * controlling the generated TypeScript project structure.\n *\n * @see {@link LexDefBuilderOptions} for definition generation options\n */\nexport type LexBuilderOptions = LexDefBuilderOptions & {\n /**\n * Whether to generate an index file at the root exporting all top-level\n * namespaces.\n *\n * @note This could theoretically cause name conflicts if a\n * @default false\n */\n indexFile?: boolean\n /**\n * The file extension to use for import specifiers in the generated code.\n *\n * @default '.js'\n */\n importExt?: string\n /**\n * The file extension to use for generated TypeScript files.\n *\n * @default '.ts'\n */\n fileExt?: string\n /**\n * Whether to export the whole defs file as a namespace export (`export * as\n * $defs from './xyz.defs.js'`). This is useful to have an escape hatch to\n * access the definitions in case of name conflicts with child namespaces.\n *\n * For example if two documents with if `com.example.foo` and\n * `com.example.foo.bar` coexist, the `com.example.foo.bar` namespace would\n * shadow any `bar` definition from the `com.example.foo` namespace. In that\n * case, having the `$defs` namespace export allows to still access those\n * definitions via `com.example.foo.$defs.bar`.\n *\n * @note enabling this will negatively impact bundle size because and\n * additional namespace object will be generated for each lexicon document.\n *\n * @default false\n */\n defsExport?: boolean\n /**\n * Whether to generate a default export for the \"main\" lexicon definition\n * schema in the parent namespace file.\n *\n * This allows simpler access the main schema when importing directly from the\n * file instead of using the full namespace as in:\n * `com.atproto.repo.getRecord`.\n *\n * ```ts\n * import getRecord from './com/atproto/repo/getRecord.js'\n * // instead of\n * import { main as getRecord } from './com/atproto/repo/getRecord.js'\n * ```\n *\n * @default false\n */\n defaultExport?: boolean\n}\n\n/**\n * Options for loading lexicon documents into the builder.\n *\n * Combines directory indexing options with filtering options to control\n * which lexicon documents are processed.\n *\n * @see {@link LexiconDirectoryIndexerOptions} for directory scanning options\n * @see {@link BuildFilterOptions} for include/exclude filtering\n */\nexport type LexBuilderLoadOptions = LexiconDirectoryIndexerOptions &\n BuildFilterOptions\n\n/**\n * Options for saving generated TypeScript files.\n *\n * Combines formatting options with output directory configuration.\n */\nexport type LexBuilderSaveOptions = FormatterOptions & {\n /**\n * The output directory path where generated TypeScript files will be written.\n */\n out: string\n /**\n * Whether to clear the output directory before writing files.\n *\n * When `true`, the entire output directory is deleted before writing new files.\n *\n * @default false\n */\n clear?: boolean\n /**\n * Whether to allow overwriting existing files.\n *\n * When `false`, an error is thrown if any output file already exists.\n *\n * @default false\n */\n override?: boolean\n}\n\n/**\n * Main builder class for generating TypeScript schemas from Lexicon documents.\n *\n * The LexBuilder orchestrates the entire code generation process:\n * 1. Loading and indexing lexicon documents from the filesystem\n * 2. Generating TypeScript type definitions and runtime schemas\n * 3. Creating namespace export trees for convenient imports\n * 4. Saving formatted output files\n *\n * @example\n * ```ts\n * const builder = new LexBuilder({ indexFile: true, pretty: true })\n *\n * // Load lexicons from a directory\n * await builder.load({ lexicons: './lexicons' })\n *\n * // Save generated TypeScript to output directory\n * await builder.save({ out: './src/generated', clear: true })\n * ```\n */\nexport class LexBuilder {\n readonly #imported = new Set<string>()\n readonly #project = new Project({\n useInMemoryFileSystem: true,\n manipulationSettings: { indentationText: IndentationText.TwoSpaces },\n })\n\n constructor(private readonly options: LexBuilderOptions = {}) {}\n\n get fileExt() {\n return this.options.fileExt ?? '.ts'\n }\n\n get importExt() {\n return this.options.importExt ?? '.js'\n }\n\n public async load(options: LexBuilderLoadOptions) {\n await using indexer = new FilteredIndexer(\n new LexiconDirectoryIndexer(options),\n buildFilter(options),\n )\n\n for await (const doc of indexer) {\n if (!this.#imported.has(doc.id)) {\n this.#imported.add(doc.id)\n } else {\n throw new Error(`Duplicate lexicon document id: ${doc.id}`)\n }\n\n await this.createDefsFile(doc, indexer)\n await this.createExportTree(doc)\n }\n }\n\n public async save(options: LexBuilderSaveOptions) {\n const files = this.#project.getSourceFiles()\n\n const destination = resolve(options.out)\n\n if (options.clear) {\n await rm(destination, { recursive: true, force: true })\n } else if (!options.override) {\n await Promise.all(\n files.map(async (f) =>\n assertNotFileExists(join(destination, f.getFilePath())),\n ),\n )\n }\n\n const formatter = new Formatter(options)\n\n await Promise.all(\n Array.from(files, async (file) => {\n const filePath = join(destination, file.getFilePath())\n const content = await formatter.format(file.getFullText())\n await mkdir(join(filePath, '..'), { recursive: true })\n await rm(filePath, { recursive: true, force: true })\n await writeFile(filePath, content, 'utf8')\n }),\n )\n }\n\n private createFile(path: string) {\n return this.#project.createSourceFile(path)\n }\n\n private getFile(path: string) {\n return this.#project.getSourceFile(path) || this.createFile(path)\n }\n\n private async createExportTree(doc: LexiconDocument) {\n const namespaces = doc.id.split('.')\n\n if (this.options.indexFile) {\n const indexFile = this.getFile(`/index${this.fileExt}`)\n\n const tldNs = namespaces[0]!\n assert(\n tldNs !== 'index',\n 'The \"indexFile\" options cannot be used with namespaces using a \".index\" tld.',\n )\n const tldNsSpecifier = `./${tldNs}${this.importExt}`\n if (!indexFile.getExportDeclaration(tldNsSpecifier)) {\n indexFile.addExportDeclaration({\n moduleSpecifier: tldNsSpecifier,\n namespaceExport: asNamespaceExport(tldNs),\n })\n }\n }\n\n // First create the parent namespaces\n for (let i = 0; i < namespaces.length - 1; i++) {\n const currentNs = namespaces[i]\n const childNs = namespaces[i + 1]\n\n const path = join('/', ...namespaces.slice(0, i + 1))\n const file = this.getFile(`${path}${this.fileExt}`)\n\n const childModuleSpecifier = `./${currentNs}/${childNs}${this.importExt}`\n const dec = file.getExportDeclaration(childModuleSpecifier)\n if (!dec) {\n file.addExportDeclaration({\n moduleSpecifier: childModuleSpecifier,\n namespaceExport: asNamespaceExport(childNs),\n })\n }\n }\n\n // The child file exports the schemas (as *)\n const path = join('/', ...namespaces)\n const file = this.getFile(`${path}${this.fileExt}`)\n\n file.addExportDeclaration({\n moduleSpecifier: `./${namespaces.at(-1)}.defs${this.importExt}`,\n })\n\n if (this.options.defsExport) {\n // @NOTE Individual exports exports from the defs file might conflict with\n // child namespaces. For this reason, we also add a namespace export for the\n // defs (export * as $defs from './xyz.defs.js'). This is an escape hatch\n // allowing to still access the definitions if a hash get shadowed by a\n // child namespace.\n file.addExportDeclaration({\n moduleSpecifier: `./${namespaces.at(-1)}.defs${this.importExt}`,\n namespaceExport: '$defs',\n })\n }\n\n if (this.options.defaultExport && doc.defs.main != null) {\n // export { main as default } from './xyz.defs.js'\n file.addExportDeclaration({\n moduleSpecifier: `./${namespaces.at(-1)}.defs${this.importExt}`,\n namedExports: [{ name: 'main', alias: 'default' }],\n })\n }\n }\n\n private async createDefsFile(\n doc: LexiconDocument,\n indexer: LexiconIndexer,\n ): Promise<void> {\n const path = join('/', ...doc.id.split('.'))\n const file = this.createFile(`${path}.defs${this.fileExt}`)\n\n const fileBuilder = new LexDefBuilder(this.options, file, doc, indexer)\n await fileBuilder.build()\n }\n}\n\nasync function assertNotFileExists(file: string): Promise<void> {\n try {\n await stat(file)\n throw new Error(`File already exists: ${file}`)\n } catch (err) {\n if (err instanceof Error && 'code' in err && err.code === 'ENOENT') return\n throw err\n }\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SourceFile } from 'ts-morph';
|
|
2
|
-
import { LexiconDocument, LexiconIndexer
|
|
3
|
-
import { RefResolverOptions
|
|
2
|
+
import { LexiconDocument, LexiconIndexer } from '@atproto/lex-document';
|
|
3
|
+
import { RefResolverOptions } from './ref-resolver.js';
|
|
4
4
|
/**
|
|
5
5
|
* Configuration options for the {@link LexDefBuilder} class.
|
|
6
6
|
*
|
|
@@ -13,15 +13,6 @@ export type LexDefBuilderOptions = RefResolverOptions & {
|
|
|
13
13
|
* @default '@atproto/lex-schema'
|
|
14
14
|
*/
|
|
15
15
|
lib?: string;
|
|
16
|
-
/**
|
|
17
|
-
* Whether to add `#__PURE__` annotations to function calls.
|
|
18
|
-
*
|
|
19
|
-
* These annotations help bundlers with tree-shaking by marking
|
|
20
|
-
* side-effect-free function calls.
|
|
21
|
-
*
|
|
22
|
-
* @default false
|
|
23
|
-
*/
|
|
24
|
-
pureAnnotations?: boolean;
|
|
25
16
|
};
|
|
26
17
|
/**
|
|
27
18
|
* Builds TypeScript type definitions and runtime schemas from a single
|
|
@@ -42,15 +33,17 @@ export declare class LexDefBuilder {
|
|
|
42
33
|
private readonly doc;
|
|
43
34
|
private readonly refResolver;
|
|
44
35
|
constructor(options: LexDefBuilderOptions, file: SourceFile, doc: LexiconDocument, indexer: LexiconIndexer);
|
|
45
|
-
private pure;
|
|
46
36
|
build(): Promise<void>;
|
|
47
37
|
private addUtils;
|
|
48
38
|
private addDef;
|
|
49
39
|
private addPermissionSet;
|
|
40
|
+
private addParameters;
|
|
41
|
+
private addInput;
|
|
42
|
+
private addOutput;
|
|
43
|
+
private addMessage;
|
|
50
44
|
private addProcedure;
|
|
51
45
|
private addQuery;
|
|
52
46
|
private addSubscription;
|
|
53
|
-
addMethodTypeUtils(ref: ResolvedRef, def: LexiconProcedure | LexiconQuery | LexiconSubscription): void;
|
|
54
47
|
private addRecord;
|
|
55
48
|
private addObject;
|
|
56
49
|
private addToken;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lex-def-builder.d.ts","sourceRoot":"","sources":["../src/lex-def-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,UAAU,EAEX,MAAM,UAAU,CAAA;AACjB,OAAO,EAOL,eAAe,EAEf,cAAc,
|
|
1
|
+
{"version":3,"file":"lex-def-builder.d.ts","sourceRoot":"","sources":["../src/lex-def-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,UAAU,EAEX,MAAM,UAAU,CAAA;AACjB,OAAO,EAOL,eAAe,EAEf,cAAc,EAgBf,MAAM,uBAAuB,CAAA;AAE9B,OAAO,EAEL,kBAAkB,EAGnB,MAAM,mBAAmB,CAAA;AAG1B;;;;GAIG;AACH,MAAM,MAAM,oBAAoB,GAAG,kBAAkB,GAAG;IACtD;;;;OAIG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;CACb,CAAA;AAED;;;;;;;;;;;;GAYG;AACH,qBAAa,aAAa;IAItB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,GAAG;IALtB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAa;gBAGtB,OAAO,EAAE,oBAAoB,EAC7B,IAAI,EAAE,UAAU,EAChB,GAAG,EAAE,eAAe,EACrC,OAAO,EAAE,cAAc;IAKnB,KAAK;IAyBX,OAAO,CAAC,QAAQ;YAYF,MAAM;YA8BN,gBAAgB;YAsBhB,aAAa;YAkBb,QAAQ;YAyBR,SAAS;YAyBT,UAAU;YAkBV,YAAY;YA2BZ,QAAQ;YA0BR,eAAe;YA0Bf,SAAS;YAsBT,SAAS;YAiBT,QAAQ;YAQR,QAAQ;YAqBR,SAAS;YA2FT,cAAc;YAkBd,iBAAiB;YAQjB,mBAAmB;YAWnB,aAAa;YAKb,mBAAmB;YAKnB,wBAAwB;YAsBxB,sBAAsB;YAYtB,0BAA0B;YAuB1B,wBAAwB;YAoBx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