@sdk-it/nx 0.0.0-stage → 0.51.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,56 @@
1
- # Temporary Holding Version
1
+ # @sdk-it/nx
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Keeps each committed derived OpenAPI document of an Nx workspace in step with the backend it is derived from. It ships one Nx sync generator, `openapi-sync`. `nx sync` writes every stale document, and `nx sync:check` fails when one is stale.
4
+
5
+ A derived document is the file an app imports to get its backend's client: `import { createBackend } from '#backend'`, where `#backend` is `src/backend.openapi`, and the app's tsconfig names the backend's definition module (`contentMappers[].options.derived`, see [`@sdk-it/content-mapper`](../content-mapper/README.md)). TypeScript needs the file to exist before it resolves the import, so the document is committed. The readers (the content mapper in `tsc` and the editor, `sdkIt.imports()` in Vite, the Node hook) derive it from the backend in memory each time and never write it. This generator is its only writer.
6
+
7
+ ## Register the generator
8
+
9
+ ```jsonc
10
+ // nx.json
11
+ {
12
+ "sync": { "globalGenerators": ["@sdk-it/nx:openapi-sync"] },
13
+ }
14
+ ```
15
+
16
+ The generator takes no options: Nx 24 passes a sync generator nothing but the tree. It reads every declaration from the tsconfig files themselves.
17
+
18
+ ## Keep the documents fresh
19
+
20
+ | You run | What happens underneath |
21
+ | -------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
22
+ | `nx sync` | For each project in the project graph, each `tsconfig*.json` with a `derived` entry for `@sdk-it/content-mapper`: each document is derived from its backend (`@sdk-it/openapi`'s `generateDocument`), and written when its bytes differ. |
23
+ | `nx sync:check` (CI) | The same, writing nothing: exits 1 and names each stale document. |
24
+ | A pre-commit hook that runs `nx sync` (zukhruf's `Sync`) | Writes and stages each document the commit makes stale, into the same commit. A backend that needs generated code (a Prisma client) gets it from `Sync`'s `before` targets. |
25
+ | `nx run <target>` | Nothing: a global sync generator never runs before a task, so no task pays for an analysis. |
26
+ | The editor, `tsc`, a Vite dev server | Nothing is written. Each derives the document in memory from the backend as it is now. |
27
+
28
+ ## Errors
29
+
30
+ - A backend that does not analyze (a definition that does not load, `generateDocument` throws) fails `nx sync` and `nx sync:check` with the analysis message and the document it was for.
31
+ - A `derived` entry whose definition module does not exist fails the same way.
32
+
33
+ ## The same concepts in other systems
34
+
35
+ | System | Derived file | Writer | Check |
36
+ | --------------------------- | ---------------------------------------------------- | ----------------------------------------- | --------------- |
37
+ | Nx `@nx/js:typescript-sync` | tsconfig project references (committed) | a sync generator, via the project graph | `nx sync:check` |
38
+ | `@juristr/nx-tailwind-sync` | Tailwind `@source` lines in `styles.css` (committed) | a sync generator, via the project graph | `nx sync:check` |
39
+ | TanStack Router | `routeTree.gen.ts` (managed, read-only) | its Vite plugin, at dev and build | none |
40
+ | SvelteKit | `.svelte-kit` types (gitignored) | `svelte-kit sync` as the `prepare` script | none |
41
+
42
+ ## npm candidates
43
+
44
+ Searched "nx sync generator openapi", "nx plugin openapi sync", "openapi document nx sync". `@driimus/nx-plugin-openapi` generates libraries _from_ a spec; none derives a spec from a TypeScript backend or keeps a committed one in step.
45
+
46
+ ## Who uses what
47
+
48
+ - `@sdk-it/nx` depends on `@nx/devkit` (the tree, `createProjectGraphAsync`) and on `@sdk-it/content-mapper` (`DerivedDocument`).
49
+ - `@sdk-it/content-mapper` exports `DerivedDocument`, and its readers stop writing ([ADR 0001](./docs/adr/0001-the-sync-generator-is-the-only-writer-of-a-derived-document.md), [ADR 0004](./docs/adr/0004-content-mapper-exports-the-derived-documents-of-a-tsconfig.md)).
50
+ - First consumers: meddit (`apps/web/src/backend.openapi`) and deepagents (`apps/evals-web-runner/frontend/src/evals.openapi`).
51
+
52
+ ## Now, later, never
53
+
54
+ | Now | Later | Never |
55
+ | --------------------------------- | --------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- |
56
+ | the `openapi-sync` sync generator | a generator that sets up a derived document in an app (the tsconfig entry, `openapi.ts`, the `#backend` import) | a CLI command or an executor that writes documents; formatting the document |
package/dist/index.js ADDED
@@ -0,0 +1,46 @@
1
+ // packages/nx/src/index.ts
2
+ import { createProjectGraphAsync } from "@nx/devkit";
3
+ import { join, relative } from "node:path";
4
+ import { DerivedDocument } from "@sdk-it/content-mapper";
5
+ async function openapiSync(tree) {
6
+ const graph = await createProjectGraphAsync();
7
+ const documents = /* @__PURE__ */ new Map();
8
+ for (const { data } of Object.values(graph.nodes)) {
9
+ const tsconfigs = tree.children(data.root).filter((name) => /^tsconfig.*\.json$/.test(name)).toSorted();
10
+ for (const name of tsconfigs) {
11
+ const tsconfig = join(data.root, name);
12
+ if (!tree.read(tsconfig, "utf8")?.includes('"derived"')) continue;
13
+ for (const document of await DerivedDocument.declaredBy(
14
+ join(tree.root, tsconfig)
15
+ )) {
16
+ documents.set(document.path, document);
17
+ }
18
+ }
19
+ }
20
+ const stale = [];
21
+ const failed = [];
22
+ for (const document of documents.values()) {
23
+ const path = relative(tree.root, document.path);
24
+ const derivation = await document.derive();
25
+ if (derivation.kind === "failed") {
26
+ failed.push(`${path}: ${derivation.message}`);
27
+ } else if (tree.read(path, "utf8") !== derivation.text) {
28
+ tree.write(path, derivation.text);
29
+ stale.push(path);
30
+ }
31
+ }
32
+ if (failed.length > 0) {
33
+ throw new Error(
34
+ `A derived OpenAPI document's backend does not analyze:
35
+ ${failed.join("\n")}`
36
+ );
37
+ }
38
+ return stale.length === 0 ? {} : {
39
+ outOfSyncMessage: "Some derived OpenAPI documents differ from what their backends produce.",
40
+ outOfSyncDetails: stale
41
+ };
42
+ }
43
+ export {
44
+ openapiSync as default
45
+ };
46
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts"],
4
+ "sourcesContent": ["import { type Tree, createProjectGraphAsync } from '@nx/devkit';\nimport { join, relative } from 'node:path';\n\nimport { DerivedDocument } from '@sdk-it/content-mapper';\n\n/**\n * The `openapi-sync` sync generator: writes each derived OpenAPI document\n * that a project's tsconfig*.json declares (`contentMappers[].options.derived`)\n * when the bytes its backend derives differ from the committed ones. It is\n * the documents' only writer; the readers derive them in memory.\n */\nexport default async function openapiSync(tree: Tree) {\n const graph = await createProjectGraphAsync();\n const documents = new Map<string, DerivedDocument>();\n for (const { data } of Object.values(graph.nodes)) {\n const tsconfigs = tree\n .children(data.root)\n .filter((name) => /^tsconfig.*\\.json$/.test(name))\n .toSorted();\n for (const name of tsconfigs) {\n const tsconfig = join(data.root, name);\n // A tsconfig that never says \"derived\" declares no derived document, so\n // most projects start no TypeScript process.\n if (!tree.read(tsconfig, 'utf8')?.includes('\"derived\"')) continue;\n for (const document of await DerivedDocument.declaredBy(\n join(tree.root, tsconfig),\n )) {\n documents.set(document.path, document);\n }\n }\n }\n\n const stale: string[] = [];\n const failed: string[] = [];\n for (const document of documents.values()) {\n const path = relative(tree.root, document.path);\n const derivation = await document.derive();\n if (derivation.kind === 'failed') {\n failed.push(`${path}: ${derivation.message}`);\n } else if (tree.read(path, 'utf8') !== derivation.text) {\n tree.write(path, derivation.text);\n stale.push(path);\n }\n }\n if (failed.length > 0) {\n throw new Error(\n `A derived OpenAPI document's backend does not analyze:\\n${failed.join('\\n')}`,\n );\n }\n return stale.length === 0\n ? {}\n : {\n outOfSyncMessage:\n 'Some derived OpenAPI documents differ from what their backends produce.',\n outOfSyncDetails: stale,\n };\n}\n"],
5
+ "mappings": ";AAAA,SAAoB,+BAA+B;AACnD,SAAS,MAAM,gBAAgB;AAE/B,SAAS,uBAAuB;AAQhC,eAAO,YAAmC,MAAY;AACpD,QAAM,QAAQ,MAAM,wBAAwB;AAC5C,QAAM,YAAY,oBAAI,IAA6B;AACnD,aAAW,EAAE,KAAK,KAAK,OAAO,OAAO,MAAM,KAAK,GAAG;AACjD,UAAM,YAAY,KACf,SAAS,KAAK,IAAI,EAClB,OAAO,CAAC,SAAS,qBAAqB,KAAK,IAAI,CAAC,EAChD,SAAS;AACZ,eAAW,QAAQ,WAAW;AAC5B,YAAM,WAAW,KAAK,KAAK,MAAM,IAAI;AAGrC,UAAI,CAAC,KAAK,KAAK,UAAU,MAAM,GAAG,SAAS,WAAW,EAAG;AACzD,iBAAW,YAAY,MAAM,gBAAgB;AAAA,QAC3C,KAAK,KAAK,MAAM,QAAQ;AAAA,MAC1B,GAAG;AACD,kBAAU,IAAI,SAAS,MAAM,QAAQ;AAAA,MACvC;AAAA,IACF;AAAA,EACF;AAEA,QAAM,QAAkB,CAAC;AACzB,QAAM,SAAmB,CAAC;AAC1B,aAAW,YAAY,UAAU,OAAO,GAAG;AACzC,UAAM,OAAO,SAAS,KAAK,MAAM,SAAS,IAAI;AAC9C,UAAM,aAAa,MAAM,SAAS,OAAO;AACzC,QAAI,WAAW,SAAS,UAAU;AAChC,aAAO,KAAK,GAAG,IAAI,KAAK,WAAW,OAAO,EAAE;AAAA,IAC9C,WAAW,KAAK,KAAK,MAAM,MAAM,MAAM,WAAW,MAAM;AACtD,WAAK,MAAM,MAAM,WAAW,IAAI;AAChC,YAAM,KAAK,IAAI;AAAA,IACjB;AAAA,EACF;AACA,MAAI,OAAO,SAAS,GAAG;AACrB,UAAM,IAAI;AAAA,MACR;AAAA,EAA2D,OAAO,KAAK,IAAI,CAAC;AAAA,IAC9E;AAAA,EACF;AACA,SAAO,MAAM,WAAW,IACpB,CAAC,IACD;AAAA,IACE,kBACE;AAAA,IACF,kBAAkB;AAAA,EACpB;AACN;",
6
+ "names": []
7
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "generators": {
3
+ "openapi-sync": {
4
+ "factory": "./dist/index.js",
5
+ "schema": "./schema.json",
6
+ "description": "Writes each committed derived OpenAPI document that a tsconfig's contentMappers[].options.derived declares, from its backend",
7
+ "hidden": true
8
+ }
9
+ }
10
+ }
package/package.json CHANGED
@@ -1,6 +1,35 @@
1
1
  {
2
2
  "name": "@sdk-it/nx",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.51.11",
4
+ "type": "module",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/JanuaryLabs/sdk-it",
8
+ "directory": "packages/nx"
9
+ },
10
+ "homepage": "https://github.com/JanuaryLabs/sdk-it/tree/main/packages/nx#readme",
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "generators": "./generators.json",
15
+ "exports": {
16
+ "./package.json": "./package.json",
17
+ "./generators.json": "./generators.json"
18
+ },
19
+ "files": [
20
+ "dist",
21
+ "generators.json",
22
+ "schema.json",
23
+ "!**/*.tsbuildinfo",
24
+ "!**/*.test.*"
25
+ ],
26
+ "dependencies": {
27
+ "@sdk-it/content-mapper": "0.51.11"
28
+ },
29
+ "peerDependencies": {
30
+ "@nx/devkit": ">=23.0.0"
31
+ },
32
+ "devDependencies": {
33
+ "@sdk-it/testing": "*"
34
+ }
35
+ }
package/schema.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "$schema": "https://json-schema.org/schema",
3
+ "type": "object",
4
+ "properties": {}
5
+ }