Skip to content

Commit ed8dba4

Browse files
committed
feat: option to overwrite existing output dir
1 parent 5e09cd2 commit ed8dba4

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

packages/markdown-emitter/src/markdown-file-emitter.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
LinkedFormattedSourceFile
44
} from '@code-to-json/formatter-linker';
55
import { isDefined } from '@code-to-json/utils';
6+
import { SysHost } from '@code-to-json/utils-ts';
67
import { FileEmitter, FileEmitterOptions } from '@snap-doc/emitter';
78
import * as debug from 'debug';
89
import { markdownForDocFile } from './md/utils';
@@ -13,40 +14,51 @@ const log = debug('snap-doc:markdown-file-emitter');
1314
export interface MarkdownFileEmitterOptions extends FileEmitterOptions {}
1415

1516
export default class MarkdownFileEmitter extends FileEmitter<MarkdownFileEmitterOptions> {
16-
constructor(options: MarkdownFileEmitterOptions) {
17-
super(options);
18-
log('outDir', this.options.outDir);
17+
constructor(host: SysHost, options: MarkdownFileEmitterOptions) {
18+
super(host, options);
1919
}
2020

2121
public async generate(data: LinkedFormattedOutputData): Promise<void> {
2222
const { sourceFiles: files } = data;
23-
const { outDir, host: h } = this.options;
24-
const outExists = h.fileOrFolderExists(outDir);
25-
if (outExists && !h.isFolder(outDir)) {
26-
throw new Error(`Path ${outDir} exists, but is not a directory`);
27-
} else if (outExists) {
28-
log(`Deleting existing contents at ${outDir}`);
29-
await h.removeFolderAndContents(outDir);
23+
const { outDir } = this.options;
24+
25+
// Ensure that the
26+
if (this.host.fileOrFolderExists(outDir)) {
27+
if (!this.host.isFolder(outDir)) {
28+
throw new Error(`Path ${outDir} exists, but is not a directory`);
29+
} else if (this.options.overwriteOutDir) {
30+
log(`Deleting existing contents at ${outDir}`);
31+
await this.host.removeFolderAndContents(outDir);
32+
} else {
33+
throw new Error(
34+
`Existing content found at ${outDir}.
35+
Please use either
36+
(1) the 'overwriteOutDir' API option or
37+
(2) the '--force' CLI flag
38+
if you want to replace existing content in the output directory`
39+
);
40+
}
3041
}
42+
3143
log(`Creating new directory at ${outDir}`);
32-
h.createFolder(outDir);
44+
this.host.createFolder(outDir);
3345

3446
Object.keys(files)
3547
.map(fname => files[fname])
3648
.filter(isDefined)
3749
.filter(f => !f.isDeclarationFile)
3850
.forEach(f => {
39-
const outPath = h.combinePaths(this.options.outDir, f.path);
51+
const outPath = this.host.combinePaths(this.options.outDir, f.path);
4052
log(`Processing module: ${f.moduleName} (${f.path})`);
41-
const parentDir = h.combinePaths(outPath, '..');
42-
if (!h.fileOrFolderExists(parentDir)) {
43-
h.createFolder(parentDir);
53+
const parentDir = this.host.combinePaths(outPath, '..');
54+
if (!this.host.fileOrFolderExists(parentDir)) {
55+
this.host.createFolder(parentDir);
4456
}
45-
if (!h.isFolder(parentDir)) {
57+
if (!this.host.isFolder(parentDir)) {
4658
throw new Error(`${parentDir} is not a directory`);
4759
}
4860
const content = this.contentForModule(data, f);
49-
h.writeFileSync(`${outPath}.md`, content);
61+
this.host.writeFileSync(`${outPath}.md`, content);
5062
});
5163
}
5264

0 commit comments

Comments
 (0)