Skip to content

Commit

Permalink
Fix wp-scripts watch mode failing when block.json contains malforme…
Browse files Browse the repository at this point in the history
…d JSON (#51971)

* 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 <grzegorz@gziolo.pl>

---------

Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>
  • Loading branch information
fabiankaegy and gziolo committed Aug 7, 2023
1 parent 1f3cc31 commit 680bc06
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 55 deletions.
4 changes: 4 additions & 0 deletions packages/scripts/CHANGELOG.md
Expand Up @@ -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)
Expand Down
125 changes: 70 additions & 55 deletions packages/scripts/utils/config.js
Expand Up @@ -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;
}
},
{}
);
Expand Down

0 comments on commit 680bc06

Please sign in to comment.