@@ -36,6 +36,9 @@ const cwd = process.cwd();
3636log ( 'Scrubbing file paths' ) ;
3737scrubPaths ( project ) ;
3838
39+ log ( 'Normalizing line endings' ) ;
40+ normalizeLineEndings ( project ) ;
41+
3942const json = JSON . stringify ( project . toObject ( ) , null , '\t' ) ;
4043
4144if ( ! existsSync ( 'docs' ) ) {
@@ -47,46 +50,75 @@ const outFile = join('docs', 'api.json');
4750writeFileSync ( outFile , json ) ;
4851log ( `Wrote API data to ${ outFile } ` ) ;
4952
53+ // Recursively walk an object, normalizing any line endings in strings
54+ function normalizeLineEndings ( reflection : any ) {
55+ walk (
56+ reflection ,
57+ '__lenormalized__' ,
58+ ( _ , value ) => typeof value === 'string' && / \r \n / . test ( value ) ,
59+ value => value . replace ( / \r \n / g, '\n' )
60+ ) ;
61+ }
62+
5063// Recursively walk an object, relativizing any paths
5164function scrubPaths ( reflection : any ) {
52- if ( reflection [ '__visited__' ] ) {
65+ walk (
66+ reflection ,
67+ '__scrubbed__' ,
68+ ( key , value ) =>
69+ typeof value === 'string' &&
70+ ( key === 'originalName' || key === 'fileName' || key === 'name' ) ,
71+ scrubPath
72+ ) ;
73+ }
74+
75+ // Relativize a path, or return the input if it's not an absolute path
76+ function scrubPath ( value : string ) {
77+ if ( / " .* " / . test ( value ) ) {
78+ const testValue = value . replace ( / ^ " / , '' ) . replace ( / " $ / , '' ) ;
79+ if ( isAbsolute ( testValue ) ) {
80+ const newPath = `"${ relative ( cwd , testValue ) } "` ;
81+ return newPath . replace ( / \\ / g, '/' ) ;
82+ }
83+ } else if ( isAbsolute ( value ) ) {
84+ const newPath = relative ( cwd , value ) ;
85+ return newPath . replace ( / \\ / g, '/' ) ;
86+ }
87+ return value ;
88+ }
89+
90+ // Walk a project reflection, modifying values as necessary
91+ function walk (
92+ reflection : any ,
93+ sentinel : string ,
94+ test : ( key : string , value : any ) = > boolean ,
95+ modify : ( value : any ) = > any
96+ ) {
97+ if ( reflection [ sentinel ] ) {
5398 return ;
5499 }
55100
56- reflection [ '__visited__' ] = true ;
101+ reflection [ sentinel ] = true ;
57102
58103 if ( Array . isArray ( reflection ) ) {
59- for ( let item of reflection ) {
104+ for ( const item of reflection ) {
60105 if ( typeof item === 'object' ) {
61- scrubPaths ( item ) ;
106+ walk ( item , sentinel , test , modify ) ;
62107 }
63108 }
64109 } else if ( typeof reflection === 'object' ) {
65110 const keys = Object . keys ( reflection ) ;
66- for ( let key of keys ) {
111+ for ( const key of keys ) {
67112 const value = reflection [ key ] ;
68113 if ( value == null ) {
69114 continue ;
70115 }
71116
72- if ( key === 'originalName' || key === 'fileName' || key === 'name' ) {
73- reflection [ key ] = scrubPath ( value ) ;
117+ if ( test ( key , value ) ) {
118+ reflection [ key ] = modify ( value ) ;
74119 } else if ( typeof value === 'object' ) {
75- scrubPaths ( value ) ;
120+ walk ( value , sentinel , test , modify ) ;
76121 }
77122 }
78123 }
79124}
80-
81- // Relativize a path, or return the input if it's not an absolute path
82- function scrubPath ( value : string ) {
83- if ( / " .* " / . test ( value ) ) {
84- const testValue = value . replace ( / ^ " / , '' ) . replace ( / " $ / , '' ) ;
85- if ( isAbsolute ( testValue ) ) {
86- return `"${ relative ( cwd , testValue ) } "` ;
87- }
88- } else if ( isAbsolute ( value ) ) {
89- return relative ( cwd , value ) ;
90- }
91- return value ;
92- }
0 commit comments