12
12
// Write:
13
13
// node scripts/convertRelativeLinksToHardcoded.js scripts/fixtures/input.md --write
14
14
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" ) ;
18
18
const { execSync } = require ( "child_process" ) ;
19
19
const escapeRegex = require ( "escape-regex-string" ) ;
20
20
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" ) ;
22
22
23
23
// This can be anything
24
- const write = process . argv [ 3 ] !== undefined
24
+ const write = process . argv [ 3 ] !== undefined ;
25
25
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" ) ) ) ;
28
28
if ( ! repoPath ) throw new Error ( "Could not find a TypeScript repo" ) ;
29
29
30
30
const 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" ) ;
32
32
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 = [ ] ;
35
37
36
38
files . forEach ( file => {
37
- let content = readFileSync ( file , "utf8" )
39
+ if ( file === "README.md" ) return ;
40
+
41
+ let content = readFileSync ( file , "utf8" ) ;
38
42
// 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 ] ;
44
48
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
+ }
53
59
}
54
60
55
61
if ( write ) {
56
- writeFileSync ( file , content )
62
+ writeFileSync ( file , content ) ;
57
63
} else {
58
- console . log ( content )
64
+ console . log ( content ) ;
59
65
}
60
66
} ) ;
61
67
68
+ if ( failed . length ) {
69
+ console . error ( "Could not find:" ) ;
70
+ console . error ( failed ) ;
71
+ process . exit ( 1 ) ;
72
+ }
62
73
63
74
/*!
64
75
* line-number <https://github.com/jonschlinkert/line-number>
@@ -67,13 +78,16 @@ files.forEach(file => {
67
78
* Licensed under the MIT License
68
79
*/
69
80
function 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