@@ -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 */
107107async 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}
0 commit comments