Skip to content

Commit c332127

Browse files
author
Michael Hladky
committed
refactor: wip
1 parent 765ff26 commit c332127

File tree

6 files changed

+114
-28
lines changed

6 files changed

+114
-28
lines changed

packages/models/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"dependsOn": [
99
"^build",
1010
"generate-docs",
11+
"ts-patch",
1112
{ "projects": "zod2md-jsdocs", "target": "build" }
1213
]
1314
},

tools/zod2md-jsdocs/docs/zod2md-jsdocs-nx-plugin.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ Default values:
8080

8181
## Options
8282

83-
| Name | type | description |
84-
| -------------- | ---------------------------------- | ------------------------------------------------------ |
85-
| **targetName** | `string` (DEFAULT 'generate-docs') | The id used to identify a target in your project.json. |
83+
| Name | type | description |
84+
| --------------------------- | ---------------------------------- | ------------------------------------------------------------------- |
85+
| **docsTargetName** | `string` (DEFAULT 'generate-docs') | The name of the docs generation target. |
86+
| **jsDocsTypesAugmentation** | `boolean` (DEFAULT `true`) | Whether to enable TypeScript transformer integration with ts-patch. |
8687

8788
All options are optional and provided in the `nx.json` file.
8889

@@ -94,7 +95,8 @@ All options are optional and provided in the `nx.json` file.
9495
{
9596
"plugin": "./tools/zod2md-jsdocs-nx-plugin/src/lib/plugin.js",
9697
"options": {
97-
"targetName": "docs",
98+
"docsTargetName": "docs",
99+
"jsDocsTypesAugmentation": true,
98100
},
99101
},
100102
],

tools/zod2md-jsdocs/project.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@
55
"projectType": "library",
66
"targets": {
77
"lint": {},
8-
"build": {
9-
"dependsOn": ["pre-build"]
10-
},
11-
"pre-build": {
12-
"command": "ts-patch install",
13-
"cache": true,
14-
"inputs": ["sharedGlobals", { "runtime": "ts-patch check" }]
15-
}
8+
"build": {},
9+
"unit-test": {}
1610
}
1711
}

tools/zod2md-jsdocs/src/lib/transformers.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ import type * as ts from 'typescript';
33

44
const tsInstance: typeof ts = require('typescript');
55

6-
function generateJSDocComment(typeName: string, baseUrl: string): string {
6+
/**
7+
* Generates a JSDoc comment for a given type name and base URL.
8+
* @param typeName
9+
* @param baseUrl
10+
*/
11+
export function generateJSDocComment(
12+
typeName: string,
13+
baseUrl: string,
14+
): string {
715
const markdownLink = `${baseUrl}#${typeName.toLowerCase()}`;
816
return `*
917
* Type Definition: \`${typeName}\`
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { generateJSDocComment } from './transformers';
3+
4+
describe('generateJSDocComment', () => {
5+
it('should generate JSDoc comment with type name and base URL', () => {
6+
const result = generateJSDocComment(
7+
'UserSchema',
8+
'https://example.com/docs',
9+
);
10+
expect(result).toMatchInlineSnapshot(`
11+
"*
12+
* Type Definition: \`UserSchema\`
13+
*
14+
* This type is derived from a Zod schema and represents
15+
* the validated structure of \`UserSchema\` used within the application.
16+
*
17+
* @see {@link https://example.com/docs#userschema}
18+
"
19+
`);
20+
});
21+
22+
it('should generate JSDoc comment for different type name', () => {
23+
const result = generateJSDocComment(
24+
'ConfigOptions',
25+
'https://docs.site.com',
26+
);
27+
expect(result).toBe(
28+
`*
29+
* Type Definition: \`ConfigOptions\`
30+
*
31+
* This type is derived from a Zod schema and represents
32+
* the validated structure of \`ConfigOptions\` used within the application.
33+
*
34+
* @see {@link https://docs.site.com#configoptions}
35+
`,
36+
);
37+
});
38+
39+
it('should convert type name to lowercase in the link anchor', () => {
40+
const result = generateJSDocComment('MyComplexType', 'https://example.com');
41+
expect(result).toContain('https://example.com#');
42+
});
43+
44+
it('should handle type names with numbers', () => {
45+
const result = generateJSDocComment('Schema123', 'https://example.com/api');
46+
expect(result).toContain('Type Definition: `Schema123`');
47+
expect(result).toContain('#schema123');
48+
});
49+
});

tools/zod2md-jsdocs/src/nx-plugin.cjs

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,68 @@
11
const path = require('node:path');
22

33
const ZOD2MD_CONFIG_FILE = 'zod2md.config.ts';
4+
const TS_PATCH_TARGET_NAME = 'ts-patch';
5+
const GENERATE_DOCS_TARGET_NAME = 'generate-docs';
6+
7+
/**
8+
* Creates the ts-patch target configuration
9+
* @returns {object} ts-patch target configuration
10+
*/
11+
const createTsPatchTargetConfig = {
12+
command: 'ts-patch install',
13+
cache: true,
14+
inputs: ['sharedGlobals', { runtime: 'ts-patch check' }],
15+
};
16+
17+
/**
18+
* Creates the docs generation target configuration
19+
* @param {object} params - Configuration parameters
20+
* @param {string} params.config - Path to the zod2md config file
21+
* @param {string} params.output - Path to the output markdown file
22+
* @returns {object} Docs target configuration
23+
*/
24+
function createDocsTargetConfig({ config, output }) {
25+
return {
26+
executor: 'nx:run-commands',
27+
options: {
28+
commands: [
29+
`zod2md --config ${config} --output ${output}`,
30+
`prettier --write ${output}`,
31+
],
32+
parallel: false,
33+
},
34+
cache: true,
35+
inputs: ['production', '^production', config],
36+
outputs: [output],
37+
};
38+
}
439

540
const createNodesV2 = [
641
`**/${ZOD2MD_CONFIG_FILE}`,
742
async (zod2MdConfigurationFiles, createNodesOptions) => {
8-
const options = createNodesOptions ?? {};
9-
const targetName = options.targetName ?? 'generate-docs';
43+
const {
44+
docsTargetName = GENERATE_DOCS_TARGET_NAME,
45+
jsDocsTypesAugmentation = true,
46+
} = createNodesOptions ?? {};
1047

1148
return Promise.all(
1249
zod2MdConfigurationFiles.map(async zod2MdConfigurationFile => {
1350
const projectRoot = path.dirname(zod2MdConfigurationFile);
1451
const normalizedProjectRoot = projectRoot === '.' ? '' : projectRoot;
1552
const output = '{projectRoot}/docs/{projectName}-reference.md';
1653
const config = `{projectRoot}/${ZOD2MD_CONFIG_FILE}`;
54+
1755
const result = {
1856
projects: {
1957
[normalizedProjectRoot]: {
2058
targets: {
21-
[targetName]: {
22-
executor: 'nx:run-commands',
23-
options: {
24-
commands: [
25-
`zod2md --config ${config} --output ${output}`,
26-
`prettier --write ${output}`,
27-
],
28-
parallel: false,
29-
},
30-
cache: true,
31-
inputs: ['production', '^production', config],
32-
outputs: [output],
33-
},
59+
...(jsDocsTypesAugmentation
60+
? { [TS_PATCH_TARGET_NAME]: createTsPatchTargetConfig }
61+
: {}),
62+
[docsTargetName]: createDocsTargetConfig({
63+
config,
64+
output,
65+
}),
3466
},
3567
},
3668
},

0 commit comments

Comments
 (0)