1212// Write:
1313// node scripts/convertRelativeLinksToHardcoded.js scripts/fixtures/input.md --write
1414
15- const glob = require ( "glob" )
16- const { readFileSync, writeFileSync, existsSync} = require ( "fs" )
17- const { join} = require ( "path" )
15+ const glob = require ( "glob" ) ;
16+ const { readFileSync, writeFileSync, existsSync } = require ( "fs" ) ;
17+ const { join } = require ( "path" ) ;
1818const { execSync } = require ( "child_process" ) ;
1919const escapeRegex = require ( "escape-regex-string" ) ;
2020
21- if ( ! process . argv [ 2 ] ) throw new Error ( "Did not include a glob for markdown files to change" )
21+ if ( ! process . argv [ 2 ] ) throw new Error ( "Did not include a glob for markdown files to change" ) ;
2222
2323// This can be anything
24- const write = process . argv [ 3 ] !== undefined
24+ const write = process . argv [ 3 ] !== undefined ;
2525
26- const possibleTSRepo = [ "../typescript-compiler" , "../TypeScript" , "TypeScript" ]
27- let repoPath = possibleTSRepo . find ( existsSync )
26+ const possibleTSRepo = [ "../typescript-compiler" , "../TypeScript" , "TypeScript" ] ;
27+ let repoPath = possibleTSRepo . find ( f => existsSync ( join ( f , "package.json" ) ) ) ;
2828if ( ! repoPath ) throw new Error ( "Could not find a TypeScript repo" ) ;
2929
3030const repoHead = execSync ( `git rev-parse HEAD | cut -c 1-8` , { cwd : repoPath , encoding : "utf8" } ) ;
31- if ( ! repoHead ) throw new Error ( "Could not get the git info from the sibling TypeScript repo" )
31+ if ( ! repoHead ) throw new Error ( "Could not get the git info from the sibling TypeScript repo" ) ;
3232
33- const files = glob . sync ( process . argv [ 2 ] )
34- if ( ! files . length ) throw new Error ( "Did not get any files with that glob" )
33+ const files = glob . sync ( process . argv [ 2 ] ) ;
34+ if ( ! files . length ) throw new Error ( "Did not get any files with that glob" ) ;
35+
36+ let failed = [ ] ;
3537
3638files . forEach ( file => {
37- let content = readFileSync ( file , "utf8" )
39+ if ( file === "README.md" ) return ;
40+
41+ let content = readFileSync ( file , "utf8" ) ;
3842 // https://regex101.com/r/w1dEG1/1
39- const regex = new RegExp ( / \[ .* ] : < ( .* ) - ( .* ) > / g)
40-
41- let result
42- while ( result = regex . exec ( content ) ) {
43- const file = result [ 1 ] ;
43+ const regex = new RegExp ( / \[ .* ] : < ( .* ) - ( .* ) > / g) ;
44+
45+ let result ;
46+ while ( ( result = regex . exec ( content ) ) ) {
47+ const fileRef = result [ 1 ] ;
4448 const searchTerm = result [ 2 ] ;
45- const original = `: <${ file } - ${ searchTerm } >`
46- const originalFile = readFileSync ( join ( repoPath , file ) , "utf8" )
47- if ( ! originalFile ) throw new Error ( `Could not find a file at '${ join ( repoPath , file ) } '` )
48-
49- const line = getLineNo ( originalFile , new RegExp ( escapeRegex ( searchTerm ) ) )
50- const lineRef = line && line [ 0 ] && line [ 0 ] . number ? `#L${ line [ 0 ] . number } ` : ""
51- const replacement = `: https://github.com/microsoft/TypeScript/blob/${ repoHead . trim ( ) } /${ file } ${ lineRef } `
52- content = content . replace ( original , replacement )
49+ const original = `: <${ fileRef } - ${ searchTerm } >` ;
50+ try {
51+ const originalFile = readFileSync ( join ( repoPath , fileRef ) , "utf8" ) ;
52+ const line = getLineNo ( originalFile , new RegExp ( escapeRegex ( searchTerm ) ) ) ;
53+ const lineRef = line && line [ 0 ] && line [ 0 ] . number ? `#L${ line [ 0 ] . number } ` : "" ;
54+ const replacement = `: https://github.com/microsoft/TypeScript/blob/${ repoHead . trim ( ) } /${ fileRef } ${ lineRef } ` ;
55+ content = content . replace ( original , replacement ) ;
56+ } catch ( e ) {
57+ failed . push ( [ file , fileRef ] ) ;
58+ }
5359 }
5460
5561 if ( write ) {
56- writeFileSync ( file , content )
62+ writeFileSync ( file , content ) ;
5763 } else {
58- console . log ( content )
64+ console . log ( content ) ;
5965 }
6066} ) ;
6167
68+ if ( failed . length ) {
69+ console . error ( "Could not find:" ) ;
70+ console . error ( failed ) ;
71+ process . exit ( 1 ) ;
72+ }
6273
6374/*!
6475 * line-number <https://github.com/jonschlinkert/line-number>
@@ -67,13 +78,16 @@ files.forEach(file => {
6778 * Licensed under the MIT License
6879 */
6980function getLineNo ( str , re ) {
70- return str . split ( / \r ? \n / ) . map ( function ( line , i ) {
71- if ( re . test ( line ) ) {
72- return {
73- line : line ,
74- number : i + 1 ,
75- match : line . match ( re ) [ 0 ]
76- } ;
77- }
78- } ) . filter ( Boolean ) ;
79- } ;
81+ return str
82+ . split ( / \r ? \n / )
83+ . map ( function ( line , i ) {
84+ if ( re . test ( line ) ) {
85+ return {
86+ line : line ,
87+ number : i + 1 ,
88+ match : line . match ( re ) [ 0 ]
89+ } ;
90+ }
91+ } )
92+ . filter ( Boolean ) ;
93+ }
0 commit comments