Skip to content

Commit 7e03339

Browse files
committed
add cleanOutput option, fixes #10
1 parent 6c2ffc4 commit 7e03339

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/generator.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ 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+
if (fs.existsSync(outputDir)) {
54+
fs.rmSync(outputDir, { recursive: true, force: true });
55+
}
56+
} catch (err) {
57+
throw new Error(
58+
`Failed to clean output directory "${outputDir}": ${err instanceof Error ? err.message : String(err)}`,
59+
);
60+
}
61+
}
4762
fs.mkdirSync(outputDir, { recursive: true });
4863
} catch (error) {
4964
throw new Error(
@@ -328,6 +343,7 @@ function resolvePluginOptions(raw: Record<string, unknown>): PluginOptions {
328343
includeRelationships: raw['includeRelationships'] !== false,
329344
includeValidation: raw['includeValidation'] !== false,
330345
output: typeof raw['output'] === 'string' ? raw['output'] : undefined,
346+
cleanOutput: raw['cleanOutput'] === true,
331347
title: typeof raw['title'] === 'string' ? raw['title'] : undefined,
332348
};
333349
}

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ export type PluginOptions = {
8888
includeValidation?: boolean;
8989
output?: string;
9090
title?: string;
91+
/**
92+
* If true, delete the output directory before generating files.
93+
*/
94+
cleanOutput?: boolean;
9195
};
9296

9397
// ── Page Props ──────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)