Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export default tseslint.config(
ignores: [
'**/*.mock.*',
'**/code-pushup.config.ts',
'**/zod2md.config.ts',
'**/mocks/fixtures/**',
'**/__snapshots__/**',
'**/dist',
Expand Down
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",
"!{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"
},
"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",
{ "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"],
}
```

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": "./path/to/transformer/dist",
"afterDeclarations": true,
"baseUrl": "https://example.com/docs/api-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`) |
11 changes: 0 additions & 11 deletions tools/zod2md-jsdocs/eslint.config.cjs

This file was deleted.

22 changes: 22 additions & 0 deletions tools/zod2md-jsdocs/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const baseConfig = require('../../eslint.config.js').default;

module.exports = [
...baseConfig,
{
files: ['**/*.json'],
rules: {
'@nx/dependency-checks': 'error',
},
},
{
files: ['**/*.ts', '**/*.cjs', '**/*.js'],
rules: {
'import/no-commonjs': 'off',
'@typescript-eslint/no-require-imports': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'unicorn/prefer-module': 'off',
'functional/immutable-data': 'off',
'arrow-body-style': 'off',
},
},
];
Loading