-
Notifications
You must be signed in to change notification settings - Fork 15
chore: add zod2md-nx-plugin #1162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
867c80d
594f451
00256ee
9112bf0
c7882cb
1cf9a7a
12a12d5
4d629f4
fb004e1
8253323
670b232
a4cf4b9
b02a6a0
4d6c335
ce500c7
1a158f1
25c79f4
1daa238
9068301
c0385c4
df9cdd2
765ff26
c332127
551b791
7f32330
3598f8a
8d90f87
0180436
e4a4864
880e997
8db0605
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,8 @@ | |
| "dependencies": { | ||
| "ansis": "^3.3.2", | ||
| "vscode-material-icons": "^0.1.0", | ||
| "zod": "^4.0.5" | ||
| "zod": "^4.0.5", | ||
| "zod2md": "^0.2.4" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, this is wrong. Think about what you are doing, please. The package's The |
||
| }, | ||
| "files": [ | ||
| "src", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,23 +4,11 @@ | |
| "sourceRoot": "packages/models/src", | ||
| "projectType": "library", | ||
| "targets": { | ||
| "generate-docs": { | ||
| "executor": "nx:run-commands", | ||
| "options": { | ||
| "commands": [ | ||
| "zod2md --config {projectRoot}/zod2md.config.ts", | ||
| "prettier --write {projectRoot}/docs/models-reference.md" | ||
| ], | ||
| "parallel": false | ||
| }, | ||
| "cache": true, | ||
| "inputs": ["production", "^production", "{projectRoot}/zod2md.config.ts"], | ||
| "outputs": ["{projectRoot}/docs/models-reference.md"] | ||
| }, | ||
| "build": { | ||
| "dependsOn": [ | ||
| "^build", | ||
| "generate-docs", | ||
| "ts-patch", | ||
|
Comment on lines
10
to
+11
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why doesn't the plugin add these dependencies? The whole idea is that each project creates a config file ( Also, I wouldn't place the |
||
| { "projects": "zod2md-jsdocs", "target": "build" } | ||
| ] | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| # @code-pushup/zod2md-jsdocs-nx-plugin | ||
|
|
||
| The Nx Plugin for [zod2md](https://github.com/matejchalk/zod2md), a tool for generating documentation from Zod schemas. | ||
|
|
||
| Why should you use this plugin? | ||
|
|
||
| - Zero setup cost. Just add a `zod2md.config.ts` file and you're good to go. | ||
| - Automatic target generation | ||
| - Minimal configuration | ||
| - Automated caching and dependency tracking | ||
|
|
||
| ## Usage | ||
|
|
||
| ```jsonc | ||
| // nx.json | ||
| { | ||
| //... | ||
| "plugins": ["./tools/zod2md-jsdocs-nx-plugin/src/lib/plugin.js"], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The path |
||
| } | ||
| ``` | ||
|
|
||
| or with options: | ||
|
|
||
| ```jsonc | ||
| // nx.json | ||
| { | ||
| //... | ||
| "plugins": [ | ||
| { | ||
| "plugin": "./tools/zod2md-jsdocs-nx-plugin/src/lib/plugin.js", | ||
| "options": { | ||
| "targetName": "zod-docs", | ||
| }, | ||
| }, | ||
| ], | ||
| } | ||
| ``` | ||
|
|
||
| Now every project with a `zod2md.config.ts` file will have a `generate-docs` target automatically created. | ||
|
|
||
| - `nx run <project-name>:generate-docs` | ||
|
|
||
| Run it and the project will automatically generate documentation from your Zod schemas. | ||
|
|
||
| ```text | ||
| Root/ | ||
| ├── project-name/ | ||
| │ ├── zod2md.config.ts | ||
| │ ├── docs/ | ||
| │ │ └── project-name-reference.md 👈 generated | ||
| │ └── ... | ||
| └── ... | ||
| ``` | ||
|
|
||
| The generated target: | ||
|
|
||
| 1. Runs `zod2md` with the project's configuration | ||
| 2. Formats the generated markdown with Prettier | ||
| 3. Caches the result for better performance | ||
|
|
||
| ### Passing zod2md options | ||
|
|
||
| You can override the config and output paths when running the target: | ||
|
|
||
| ```bash | ||
| # Use custom output file | ||
| nx generate-docs my-project --output=docs/custom-api.md | ||
|
|
||
| # Use custom config file | ||
| nx generate-docs my-project --config=custom-zod2md.config.ts | ||
|
|
||
| # Use both | ||
| nx generate-docs my-project --config=custom.config.ts --output=docs/api.md | ||
| ``` | ||
|
|
||
| Default values: | ||
|
|
||
| - `config`: `{projectRoot}/zod2md.config.ts` | ||
| - `output`: `{projectRoot}/docs/{projectName}-reference.md` | ||
|
|
||
| ## Options | ||
|
|
||
| | Name | type | description | | ||
| | --------------------------- | ---------------------------------- | ------------------------------------------------------------------- | | ||
| | **docsTargetName** | `string` (DEFAULT 'generate-docs') | The name of the docs generation target. | | ||
| | **jsDocsTypesAugmentation** | `boolean` (DEFAULT `true`) | Whether to enable TypeScript transformer integration with ts-patch. | | ||
|
|
||
| All options are optional and provided in the `nx.json` file. | ||
|
|
||
| ```jsonc | ||
| // nx.json | ||
| { | ||
| //... | ||
| "plugins": [ | ||
| { | ||
| "plugin": "./tools/zod2md-jsdocs-nx-plugin/src/lib/plugin.js", | ||
| "options": { | ||
| "docsTargetName": "docs", | ||
| "jsDocsTypesAugmentation": true, | ||
| }, | ||
| }, | ||
| ], | ||
| } | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| Create a `zod2md.config.ts` file in your project: | ||
|
|
||
| ```ts | ||
| import type { Config } from 'zod2md'; | ||
|
|
||
| export default { | ||
| entry: 'packages/models/src/index.ts', | ||
| tsconfig: 'packages/models/tsconfig.lib.json', | ||
| format: 'esm', | ||
| title: 'Models reference', | ||
| output: 'packages/models/docs/models-reference.md', | ||
| } satisfies Config; | ||
| ``` | ||
|
|
||
| For a full list of configuration options visit the [zod2md documentation](https://github.com/matejchalk/zod2md?tab=readme-ov-file#configuration). | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # zod2md-jsdocs-ts-transformer | ||
|
|
||
| TypeScript transformer plugin that automatically enhances type definitions with JSDoc comments and schema metadata. | ||
|
|
||
| ## Purpose | ||
|
|
||
| This package provides a TypeScript compiler transformer that automatically adds JSDoc documentation to type aliases and interfaces during compilation. It's designed to improve developer experience by injecting helpful metadata and documentation links directly into generated type definitions. | ||
|
|
||
| ## How It Works | ||
|
|
||
| The [TS transformer](https://github.com/itsdouges/typescript-transformer-handbook) hooks into the TypeScript compilation process using `ts-patch` and automatically adds JSDoc comments above type definitions. Each comment includes: | ||
|
|
||
| - The type name | ||
| - A description explaining the type is derived from a Zod schema | ||
| - A link to the type reference documentation | ||
|
|
||
| ## Example | ||
|
|
||
| Given a type definition like: | ||
|
|
||
| ```typescript | ||
| export type Report = { | ||
| // ... type properties | ||
| }; | ||
| ``` | ||
|
|
||
| The transformer automatically generates: | ||
|
|
||
| ```typescript | ||
| /** | ||
| * Type Definition: `Report` | ||
| * | ||
| * This type is derived from a Zod schema and represents | ||
| * the validated structure of `Report` used within the application. | ||
| * | ||
| * @see {@link https://github.com/code-pushup/cli/blob/main/packages/models/docs/models-reference.md#report} | ||
| */ | ||
| export type Report = { | ||
| // ... type properties | ||
| }; | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| 1. `ts-patch install` | ||
|
|
||
| 2. Add the transformer to your `tsconfig.json`: | ||
|
|
||
| ```json | ||
| { | ||
| "compilerOptions": { | ||
| "plugins": [ | ||
| { | ||
| "transform": "./tools/zod2md-jsdocs/dist", | ||
| "afterDeclarations": true, | ||
| "baseUrl": "https://github.com/code-pushup/cli/blob/main/packages/<PROJECT-NAME>/docs/models-reference.md" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| 3. Build your TypeScript project. The transformer will run automatically and add JSDoc comments to your type definitions. | ||
|
|
||
| ### Options | ||
|
|
||
| | Option | Type | Required | Description | | ||
| | ------------------- | --------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| | `transform` | `string` | Yes | Path to the transformer module | | ||
| | `afterDeclarations` | `boolean` | No | Set to `true` to run the transformer after TypeScript generates declaration files (`.d.ts`). This ensures JSDoc comments are added to the emitted type definitions. | | ||
| | `baseUrl` | `string` | Yes | Base URL for documentation links (e.g., `https://example.com/docs/api-reference.md`) | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,20 +4,8 @@ | |
| "sourceRoot": "tools/zod2md-jsdocs/src", | ||
| "projectType": "library", | ||
| "targets": { | ||
| "build": { | ||
| "executor": "@nx/js:tsc", | ||
| "outputs": ["{options.outputPath}"], | ||
| "dependsOn": ["pre-build"], | ||
| "options": { | ||
| "outputPath": "tools/zod2md-jsdocs/dist", | ||
| "main": "tools/zod2md-jsdocs/src/index.ts", | ||
| "tsConfig": "tools/zod2md-jsdocs/tsconfig.lib.json" | ||
| } | ||
| }, | ||
| "pre-build": { | ||
| "command": "ts-patch install", | ||
| "cache": true, | ||
| "inputs": ["sharedGlobals", { "runtime": "ts-patch check" }] | ||
| } | ||
|
Comment on lines
-17
to
-21
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would restore this target - see previous comment. I'm fine with renaming |
||
| "lint": {}, | ||
| "build": {}, | ||
| "unit-test": {} | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please restore this line - see previous comment.