Skip to content

Commit 017e93b

Browse files
committed
fix #10, add cleanOutput option
1 parent bbb3a23 commit 017e93b

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ plugin documentation {
152152
| Option | Type | Default | Description |
153153
|---|---|---|---|
154154
| `output` | `string` | ZenStack default output path | Directory to write generated docs |
155+
| `cleanOutput` | `boolean` | `false` | When `true`, the output directory is removed before generation to ensure a clean build (use with caution) |
155156
| `title` | `string` | `"Schema Documentation"` | Heading on the index page |
156157
| `fieldOrder` | `"declaration"` or `"alphabetical"` | `"declaration"` | How fields are ordered in tables |
157158
| `includeInternalModels` | `boolean` | `false` | Include models marked `@@ignore` in output |

src/generator.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ export async function generate(context: CliGeneratorContext): Promise<void> {
4444
options.genCtx = genCtx;
4545

4646
try {
47+
if (pluginOptions.cleanOutput) {
48+
try {
49+
const root = path.parse(outputDir).root;
50+
if (outputDir === root) {
51+
throw new Error('Refusing to remove root directory');
52+
}
53+
54+
if (fs.existsSync(outputDir)) {
55+
fs.rmSync(outputDir, { force: true, recursive: true });
56+
}
57+
} catch (error) {
58+
throw new Error(
59+
`Failed to clean output directory "${outputDir}": ${error instanceof Error ? error.message : String(error)}`,
60+
);
61+
}
62+
}
63+
4764
fs.mkdirSync(outputDir, { recursive: true });
4865
} catch (error) {
4966
throw new Error(
@@ -302,6 +319,7 @@ function resolveOutputDir(options: PluginOptions, defaultPath: string): string {
302319
*/
303320
function resolvePluginOptions(raw: Record<string, unknown>): PluginOptions {
304321
return {
322+
cleanOutput: raw['cleanOutput'] === true,
305323
diagramEmbed: (['file', 'inline'] as const).includes(
306324
raw['diagramEmbed'] as 'file' | 'inline',
307325
)

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ export type PluginOptions = {
7474
* inline in the markdown (`'inline'`). Only applies when `diagramFormat` is
7575
* `'svg'` or `'both'`.
7676
*/
77+
/**
78+
* If true, delete the output directory before generating files.
79+
*/
80+
cleanOutput?: boolean;
7781
diagramEmbed?: 'file' | 'inline';
7882
diagramFormat?: 'both' | 'mermaid' | 'svg';
7983
erdFormat?: 'both' | 'mmd' | 'svg';

0 commit comments

Comments
 (0)