3
3
LinkedFormattedSourceFile
4
4
} from '@code-to-json/formatter-linker' ;
5
5
import { isDefined } from '@code-to-json/utils' ;
6
+ import { SysHost } from '@code-to-json/utils-ts' ;
6
7
import { FileEmitter , FileEmitterOptions } from '@snap-doc/emitter' ;
7
8
import * as debug from 'debug' ;
8
9
import { markdownForDocFile } from './md/utils' ;
@@ -13,40 +14,51 @@ const log = debug('snap-doc:markdown-file-emitter');
13
14
export interface MarkdownFileEmitterOptions extends FileEmitterOptions { }
14
15
15
16
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 ) ;
19
19
}
20
20
21
21
public async generate ( data : LinkedFormattedOutputData ) : Promise < void > {
22
22
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
+ }
30
41
}
42
+
31
43
log ( `Creating new directory at ${ outDir } ` ) ;
32
- h . createFolder ( outDir ) ;
44
+ this . host . createFolder ( outDir ) ;
33
45
34
46
Object . keys ( files )
35
47
. map ( fname => files [ fname ] )
36
48
. filter ( isDefined )
37
49
. filter ( f => ! f . isDeclarationFile )
38
50
. forEach ( f => {
39
- const outPath = h . combinePaths ( this . options . outDir , f . path ) ;
51
+ const outPath = this . host . combinePaths ( this . options . outDir , f . path ) ;
40
52
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 ) ;
44
56
}
45
- if ( ! h . isFolder ( parentDir ) ) {
57
+ if ( ! this . host . isFolder ( parentDir ) ) {
46
58
throw new Error ( `${ parentDir } is not a directory` ) ;
47
59
}
48
60
const content = this . contentForModule ( data , f ) ;
49
- h . writeFileSync ( `${ outPath } .md` , content ) ;
61
+ this . host . writeFileSync ( `${ outPath } .md` , content ) ;
50
62
} ) ;
51
63
}
52
64
0 commit comments