Skip to content

Commit 6630fef

Browse files
Added exceptions for synchronous i/o operations
1 parent 456cc20 commit 6630fef

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

‎bin/plugin/commands/build-plugins.js‎

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ exports.handler = async () => {
5454
} catch ( copyError ) {
5555
log(
5656
formats.error(
57-
`Error copying plugin file: "${ copyError }"`
57+
`Error copying files for plugin "${ pluginSlug }": "${ copyError }"`
5858
)
5959
);
6060
continue;
6161
}
6262

6363
// Update file content.
6464
updatePluginHeader( {
65-
originalModulePath: moduleDir,
65+
modulePath: moduleDir,
6666
slug: pluginSlug,
6767
version: pluginVersion,
6868
pluginPath: buildModulePath,
@@ -105,20 +105,37 @@ exports.handler = async () => {
105105
* @param {Object} settings Plugin settings.
106106
*/
107107
async function updatePluginHeader( settings ) {
108-
const { originalModulePath, version, slug, pluginPath } = settings;
108+
const { modulePath, version, slug, pluginPath } = settings;
109109
// Specific module `load.php` file content.
110110
const buildLoadFile = path.join( pluginPath, 'load.php' );
111-
const buildLoadFileContent = fs.readFileSync( buildLoadFile, 'utf-8' );
111+
let buildLoadFileContent = '';
112+
try {
113+
buildLoadFileContent = fs.readFileSync( buildLoadFile, 'utf-8' );
114+
} catch ( err ) {
115+
log(
116+
formats.error(
117+
`Error reading the file "${ buildLoadFile }": "${ err }"`
118+
)
119+
);
120+
}
112121

113122
const moduleHeader = await getModuleHeader( buildLoadFileContent );
114123

115124
// Get module header data.
116125
const { name, description } = await getModuleDataFromHeader( moduleHeader );
117126

118-
const pluginHeader = `/**\n * Plugin Name: ${ name }\n * Plugin URI: https://github.com/WordPress/performance/tree/trunk/modules/${ originalModulePath }\n * Description: ${ description }\n * Requires at least: 6.1\n * Requires PHP: 5.6\n * Version: ${ version }\n * Author: WordPress Performance Team\n * Author URI: https://make.wordpress.org/performance/\n * License: GPLv2 or later\n * License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html\n * Text Domain: ${ slug }\n *\n * @package ${ slug }\n `;
119-
120-
// Replace the module file header.
121-
fs.writeFileSync( buildLoadFile, buildLoadFileContent.replace( moduleHeader, pluginHeader ) );
127+
const pluginHeader = `/**\n * Plugin Name: ${ name }\n * Plugin URI: https://github.com/WordPress/performance/tree/trunk/modules/${ modulePath }\n * Description: ${ description }\n * Requires at least: 6.1\n * Requires PHP: 5.6\n * Version: ${ version }\n * Author: WordPress Performance Team\n * Author URI: https://make.wordpress.org/performance/\n * License: GPLv2 or later\n * License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html\n * Text Domain: ${ slug }\n *\n * @package ${ slug }\n `;
128+
129+
try {
130+
// Replace the module file header.
131+
fs.writeFileSync( buildLoadFile, buildLoadFileContent.replace( moduleHeader, pluginHeader ) );
132+
} catch ( error ) {
133+
log(
134+
formats.error(
135+
`Error replacing module file header: "${ error }"`
136+
)
137+
);
138+
}
122139
}
123140

124141
/**
@@ -138,12 +155,29 @@ async function updateModuleDetails( settings ) {
138155
const regexp = new RegExp( settings.regex, 'gm' );
139156

140157
files.forEach( ( file ) => {
141-
const content = fs.readFileSync( file, 'utf-8' );
142-
if ( regexp.test( content ) ) {
143-
fs.writeFileSync(
144-
file,
145-
content.replace( regexp, `${ settings.result }` )
158+
let content = '';
159+
try {
160+
content = fs.readFileSync( file, 'utf-8' );
161+
} catch ( err ) {
162+
log(
163+
formats.error(
164+
`Error reading the file "${ file }": "${ err }"`
165+
)
146166
);
147167
}
168+
if ( regexp.test( content ) ) {
169+
try {
170+
fs.writeFileSync(
171+
file,
172+
content.replace( regexp, `${ settings.result }` )
173+
);
174+
} catch ( error ) {
175+
log(
176+
formats.error(
177+
`Error replacing content for regex "${ settings.regex }": "${ error }"`
178+
)
179+
);
180+
}
181+
}
148182
} );
149183
}

‎bin/plugin/commands/common.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@ exports.getModuleData = async ( modulesDir ) => {
4040

4141
return moduleFiles
4242
.map( ( moduleFile ) => {
43-
const moduleFileContent = fs.readFileSync( moduleFile, 'utf8' );
43+
let moduleFileContent = '';
44+
try {
45+
moduleFileContent = fs.readFileSync( moduleFile, 'utf-8' );
46+
} catch ( error ) {
47+
log(
48+
formats.error(
49+
`Error reading the file "${ moduleFile }": "${ error }"`
50+
)
51+
);
52+
}
4453
const moduleHeader = exports.getModuleHeader( moduleFileContent );
4554

4655
// Populate slug and focus based on file path.

0 commit comments

Comments
 (0)