Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wp-scripts watch mode failing when block.json contains malformed JSON #51971

Merged
merged 4 commits into from Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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))
fabiankaegy marked this conversation as resolved.
Show resolved Hide resolved

## 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