From 680bc0634d65edb0b5a2492997a82e077846659a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20K=C3=A4gy?= Date: Mon, 7 Aug 2023 12:01:34 +0200 Subject: [PATCH] Fix wp-scripts watch mode failing when `block.json` contains malformed JSON (#51971) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix wrap entrypoint generation in try catch statement to prevent malformed JSON from stopping watch mode * add changelog * Update packages/scripts/CHANGELOG.md Co-authored-by: Greg Ziółkowski --------- Co-authored-by: Greg Ziółkowski --- packages/scripts/CHANGELOG.md | 4 + packages/scripts/utils/config.js | 125 +++++++++++++++++-------------- 2 files changed, 74 insertions(+), 55 deletions(-) diff --git a/packages/scripts/CHANGELOG.md b/packages/scripts/CHANGELOG.md index 4e50ebf02cf02..9e49fc056dfb7 100644 --- a/packages/scripts/CHANGELOG.md +++ b/packages/scripts/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Bug Fix + +- Fix prevent watch mode from aborting when encountering a `block.json` file that contains invalid JSON. ([#51971](https://github.com/WordPress/gutenberg/pull/51971)) + ## 26.9.0 (2023-07-20) ## 26.8.0 (2023-07-05) diff --git a/packages/scripts/utils/config.js b/packages/scripts/utils/config.js index 2c2125d65cee4..fc00a613d34ac 100644 --- a/packages/scripts/utils/config.js +++ b/packages/scripts/utils/config.js @@ -219,64 +219,79 @@ function getWebpackEntryPoints() { ); const entryPoints = blockMetadataFiles.reduce( ( accumulator, blockMetadataFile ) => { - const { editorScript, script, viewScript } = JSON.parse( - readFileSync( blockMetadataFile ) - ); - [ editorScript, script, viewScript ] - .flat() - .filter( ( value ) => value && value.startsWith( 'file:' ) ) - .forEach( ( value ) => { - // Removes the `file:` prefix. - const filepath = join( - dirname( blockMetadataFile ), - value.replace( 'file:', '' ) - ); - - // Takes the path without the file extension, and relative to the defined source directory. - if ( ! filepath.startsWith( srcDirectory ) ) { - log( - chalk.yellow( - `Skipping "${ value.replace( - 'file:', - '' - ) }" listed in "${ blockMetadataFile.replace( - fromProjectRoot( sep ), - '' - ) }". File is located outside of the "${ getWordPressSrcDirectory() }" directory.` - ) + // wrapping in try/catch in case the file is malformed + // this happens especially when new block.json files are added + // at which point they are completely empty and therefore not valid JSON + try { + const { editorScript, script, viewScript } = JSON.parse( + readFileSync( blockMetadataFile ) + ); + [ editorScript, script, viewScript ] + .flat() + .filter( + ( value ) => value && value.startsWith( 'file:' ) + ) + .forEach( ( value ) => { + // Removes the `file:` prefix. + const filepath = join( + dirname( blockMetadataFile ), + value.replace( 'file:', '' ) ); - return; - } - const entryName = filepath - .replace( extname( filepath ), '' ) - .replace( srcDirectory, '' ) - .replace( /\\/g, '/' ); - - // Detects the proper file extension used in the defined source directory. - const [ entryFilepath ] = glob( - `${ getWordPressSrcDirectory() }/${ entryName }.[jt]s?(x)`, - { - absolute: true, + + // Takes the path without the file extension, and relative to the defined source directory. + if ( ! filepath.startsWith( srcDirectory ) ) { + log( + chalk.yellow( + `Skipping "${ value.replace( + 'file:', + '' + ) }" listed in "${ blockMetadataFile.replace( + fromProjectRoot( sep ), + '' + ) }". File is located outside of the "${ getWordPressSrcDirectory() }" directory.` + ) + ); + return; } - ); - - if ( ! entryFilepath ) { - log( - chalk.yellow( - `Skipping "${ value.replace( - 'file:', - '' - ) }" listed in "${ blockMetadataFile.replace( - fromProjectRoot( sep ), - '' - ) }". File does not exist in the "${ getWordPressSrcDirectory() }" directory.` - ) + const entryName = filepath + .replace( extname( filepath ), '' ) + .replace( srcDirectory, '' ) + .replace( /\\/g, '/' ); + + // Detects the proper file extension used in the defined source directory. + const [ entryFilepath ] = glob( + `${ getWordPressSrcDirectory() }/${ entryName }.[jt]s?(x)`, + { + absolute: true, + } ); - return; - } - accumulator[ entryName ] = entryFilepath; - } ); - return accumulator; + + if ( ! entryFilepath ) { + log( + chalk.yellow( + `Skipping "${ value.replace( + 'file:', + '' + ) }" listed in "${ blockMetadataFile.replace( + fromProjectRoot( sep ), + '' + ) }". File does not exist in the "${ getWordPressSrcDirectory() }" directory.` + ) + ); + return; + } + accumulator[ entryName ] = entryFilepath; + } ); + return accumulator; + } catch ( error ) { + chalk.yellow( + `Skipping "${ blockMetadataFile.replace( + fromProjectRoot( sep ), + '' + ) }" due to malformed JSON.` + ); + return accumulator; + } }, {} );