@@ -14,7 +14,7 @@ const { deprecated } = require('../lib/enterprise-server-releases')
1414const got = require ( 'got' )
1515
1616// Links with these codes may or may not really be broken.
17- const retryStatusCodes = [ 429 , 503 ]
17+ const retryStatusCodes = [ 429 , 503 , 'Invalid' ]
1818
1919// [start-readme]
2020//
@@ -27,7 +27,8 @@ const retryStatusCodes = [429, 503]
2727program
2828 . description ( 'Check all links in the English docs.' )
2929 . option ( '-d, --dry-run' , 'Turn off recursion to get a fast minimal report (useful for previewing output).' )
30- . option ( '-p, --path <PATH>' , 'Provide an optional path to check. Best used with --dry-run. If not provided, defaults to the homepage.' )
30+ . option ( '-r, --do-not-retry' , `Do not retry broken links with status codes ${ retryStatusCodes . join ( ', ' ) } .` )
31+ . option ( '-p, --path <PATH>' , `Provide an optional path to check. Best used with --dry-run. Default: ${ englishRoot } ` )
3132 . parse ( process . argv )
3233
3334// Skip excluded links defined in separate file.
@@ -79,23 +80,28 @@ async function main () {
7980 // Scan is complete! Filter the results for broken links.
8081 const brokenLinks = result
8182 . filter ( link => link . state === 'BROKEN' )
82-
83- // Links to retry individually.
84- const linksToRetry = brokenLinks
85- . filter ( link => ! link . status || retryStatusCodes . includes ( link . status ) )
86-
87- await Promise . all ( linksToRetry
88- . map ( async ( link ) => {
89- try {
90- // got throws an HTTPError if response code is not 2xx or 3xx.
91- // If got succeeds, we can remove the link from the list.
92- await got ( link . url )
93- pull ( brokenLinks , link )
94- // If got fails, do nothing. The link is already in the broken list.
95- } catch ( err ) {
96- // noop
97- }
98- } ) )
83+ // Coerce undefined status codes into `Invalid` strings so we can display them.
84+ // Without this, undefined codes get JSON.stringified as `0`, which is not useful output.
85+ . map ( link => { link . status = link . status || 'Invalid' ; return link } )
86+
87+ if ( ! program . doNotRetry ) {
88+ // Links to retry individually.
89+ const linksToRetry = brokenLinks
90+ . filter ( link => retryStatusCodes . includes ( link . status ) )
91+
92+ await Promise . all ( linksToRetry
93+ . map ( async ( link ) => {
94+ try {
95+ // got throws an HTTPError if response code is not 2xx or 3xx.
96+ // If got succeeds, we can remove the link from the list.
97+ await got ( link . url )
98+ pull ( brokenLinks , link )
99+ // If got fails, do nothing. The link is already in the broken list.
100+ } catch ( err ) {
101+ // noop
102+ }
103+ } ) )
104+ }
99105
100106 // Exit successfully if no broken links!
101107 if ( ! brokenLinks . length ) {
0 commit comments