@@ -13,8 +13,8 @@ const englishRoot = `${root}/en`
1313const { deprecated } = require ( '../lib/enterprise-server-releases' )
1414const got = require ( 'got' )
1515
16- // Links with these codes may or may not really be broken
17- const retryStatusCodes = [ 429 , 503 , 'Undefined' ]
16+ // Links with these codes may or may not really be broken.
17+ const retryStatusCodes = [ 429 , 503 ]
1818
1919// [start-readme]
2020//
6262async function main ( ) {
6363 // Clear and recreate a directory for logs.
6464 const logFile = path . join ( __dirname , '../.linkinator/full.log' )
65- rimraf ( logFile )
66- mkdirp ( logFile )
65+ rimraf ( path . dirname ( logFile ) )
66+ mkdirp ( path . dirname ( logFile ) )
6767
6868 // Update CLI output and append to logfile after each checked link.
6969 checker . on ( 'link' , result => {
@@ -76,15 +76,10 @@ async function main () {
7676 // Scan is complete! Filter the results for broken links.
7777 const brokenLinks = result
7878 . filter ( link => link . state === 'BROKEN' )
79- // Coerce undefined status codes into strings so we can filter and display them (otherwise they stringify as 0)
80- . map ( link => {
81- if ( ! link . status ) link . status = 'Undefined'
82- return link
83- } )
8479
8580 // Links to retry individually.
8681 const linksToRetry = brokenLinks
87- . filter ( link => retryStatusCodes . includes ( link . status ) )
82+ . filter ( link => ! link . status || retryStatusCodes . includes ( link . status ) )
8883
8984 await Promise . all ( linksToRetry
9085 . map ( async ( link ) => {
@@ -115,12 +110,20 @@ async function main () {
115110
116111function displayBrokenLinks ( brokenLinks ) {
117112 // Sort results by status code.
118- const allStatusCodes = uniq ( brokenLinks . map ( x => x . status ) )
113+ const allStatusCodes = uniq ( brokenLinks
114+ // Coerce undefined status codes into `Invalid` strings so we can display them.
115+ // Without this, undefined codes get JSON.stringified as `0`, which is not useful output.
116+ . map ( link => {
117+ if ( ! link . status ) link . status = 'Invalid'
118+ return link
119+ } )
120+ . map ( link => link . status )
121+ )
119122
120123 allStatusCodes . forEach ( statusCode => {
121124 const brokenLinksForStatus = brokenLinks . filter ( x => x . status === statusCode )
122125
123- console . log ( `## Status code ${ statusCode } : Found ${ brokenLinksForStatus . length } broken links` )
126+ console . log ( `## Status ${ statusCode } : Found ${ brokenLinksForStatus . length } broken links` )
124127 console . log ( '```' )
125128 brokenLinksForStatus . forEach ( brokenLinkObj => {
126129 console . log ( JSON . stringify ( brokenLinkObj , null , 2 ) )
0 commit comments