Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"!{projectRoot}/CHANGELOG.md",
"!{projectRoot}/perf/**/*",
"!{projectRoot}/tools/**/*",
"!{projectRoot}/zod2md.config.ts",
Copy link
Collaborator

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.

"!{projectRoot}/eslint.config.?(c)js",
"!{workspaceRoot}/**/.code-pushup/**/*",
"!{projectRoot}/code-pushup.config.?(m)[jt]s",
Expand Down Expand Up @@ -46,7 +45,7 @@
"options": {
"command": "eslint",
"args": [
"'{projectRoot}/**/*.ts'",
"'{projectRoot}/**/*.{ts,cjs,mjs,js}'",
"{projectRoot}/package.json",
"--config={projectRoot}/eslint.config.js",
"--max-warnings=0",
Expand Down Expand Up @@ -337,6 +336,7 @@
"releaseTagPattern": "v{version}"
},
"plugins": [
"./tools/zod2md-jsdocs/src/nx-plugin.cjs",
{
"plugin": "@push-based/nx-verdaccio",
"options": {
Expand Down
3 changes: 0 additions & 3 deletions packages/models/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,4 @@ export default tseslint.config(
'@nx/dependency-checks': 'error',
},
},
{
ignores: ['packages/models/transformers/**/*.ts'],
},
);
3 changes: 2 additions & 1 deletion packages/models/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 dependencies should only list production dependencies, i.e. the minimum the end-user needs to have installed. Development tools like vitest or zod2md do not belong here.

The @nx/dependency-checks lint rule determines what the production dependencies should be by checking imports in all files matched by the production cache input. This is why we had the "!{projectRoot}/zod2md.config.ts" pattern in nx.json, because we know that zod2md.config.ts files aren't part of production builds. I've no idea why you removed that line.

},
"files": [
"src",
Expand Down
14 changes: 1 addition & 13 deletions packages/models/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 (zod2md.config.ts), and the Nx plugin (zod2md-jsdocs) takes care of everything else.

Also, I wouldn't place the ts-patch here. The previous setup was simpler, as there was a single tspatch (actually called pre-build before) target in zod2md-jsdocs, and it was a dependency of zod2md-jsdocs's build target. This way, there's only 1 target in 1 project. Which makes sense, because installing ts-patch isn't something that needs to run per project, our development dependencies are installed globally.

{ "projects": "zod2md-jsdocs", "target": "build" }
]
},
Expand Down
64 changes: 20 additions & 44 deletions tools/zod2md-jsdocs/README.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,41 @@
# @code-pushup/zod2md-jsdocs
# zod2md-jsdocs

TypeScript transformer plugin that automatically enhances type definitions with JSDoc comments and schema metadata.
A comprehensive toolset for generating and enhancing TypeScript documentation from Zod schemas. This package combines an Nx plugin for automated documentation generation with a TypeScript transformer that enriches type definitions with JSDoc comments.

## Purpose
## What's Included

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.
This package provides two main components:

## How It Works
1. **[Nx Plugin](./docs/zod2md-jsdocs-nx-plugin.md)** - Automatically generates documentation targets for projects with Zod schemas
2. **[TypeScript Transformer](./docs/zod2md-jsdocs-ts-transformer.md)** - Enhances generated type definitions with JSDoc comments and schema metadata

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:
## Quick Start

- The type name
- A description explaining the type is derived from a Zod schema
- A link to the type reference documentation
### Using the Nx Plugin

## Example
Add the plugin to your `nx.json`:

Given a type definition like:

```typescript
export type Report = {
// ... type properties
};
```jsonc
{
"plugins": ["./tools/zod2md-jsdocs/src/lib/plugin.js"],
}
```

The transformer automatically generates:
Create a `zod2md.config.ts` in your project, and you'll automatically get a `generate-docs` target.

```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
};
```
[Learn more about the Nx Plugin →](./docs/zod2md-jsdocs-nx-plugin.md)

## Usage
### Using the TypeScript Transformer

1. `ts-patch install`

2. Add the transformer to your `tsconfig.json`:
1. Install ts-patch: `ts-patch install`
2. Add to your `tsconfig.json`:

```json
{
"compilerOptions": {
"plugins": [
{
"transform": "./path/to/transformer/dist",
"transform": "./tools/zod2md-jsdocs/dist/src",
"afterDeclarations": true,
"baseUrl": "https://example.com/docs/api-reference.md"
}
Expand All @@ -60,12 +44,4 @@ export type Report = {
}
```

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`) |
[Learn more about the TypeScript Transformer →](./docs/zod2md-jsdocs-ts-transformer.md)
122 changes: 122 additions & 0 deletions tools/zod2md-jsdocs/docs/zod2md-jsdocs-nx-plugin.md
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"],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path ./tools/zod2md-jsdocs-nx-plugin/src/lib/plugin.js is incorrect; it's actually ./tools/zod2md-jsdocs/dist/src (as used in nx.json). It appears several times in this document.

}
```

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).
71 changes: 71 additions & 0 deletions tools/zod2md-jsdocs/docs/zod2md-jsdocs-ts-transformer.md
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`) |
18 changes: 3 additions & 15 deletions tools/zod2md-jsdocs/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 pre-build to tspatch, though.

"lint": {},
"build": {},
"unit-test": {}
}
}
Loading
Loading