From f21824e73096725239c6199ca514ac97761741d1 Mon Sep 17 00:00:00 2001 From: DeeDeeG Date: Tue, 29 Aug 2023 22:48:43 -0400 Subject: [PATCH] script: Use process.exit() in renaming script Gets rid of a lot of if-else that made the code harder to follow. This should be pretty safe and unproblematic in a standalone script like this, versus some more complex or nested Node process scenario, or scenarios with async input/output that might be ongoing when the process.exit() is handled. Also, update some of the code comments a bit. Co-authored-by: Andrew Dupont --- script/rename.js | 68 +++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/script/rename.js b/script/rename.js index 337d835c6..b9fa7331e 100644 --- a/script/rename.js +++ b/script/rename.js @@ -10,6 +10,7 @@ const pulsarVersion = require('../package.json').version; const versionSegments = pulsarVersion.split('.'); const lastSegment = versionSegments[versionSegments.length - 1]; +// Detecting Rolling release version strings if (lastSegment.length > 4) { // For example, '1-dev' is longer than 4 characters, // and the format like '2023081600' from CI is longer than 4 characters. @@ -17,42 +18,43 @@ if (lastSegment.length > 4) { console.log(`Based on the version string in package.json (${pulsarVersion}),`); console.log('we are *not* preparing a Regular release, so *not* renaming any binaries.'); console.log('Exiting the binary renaming script.'); -} else { + process.exit(0); +} - // Warn about required prefix as first argument. - // If prefix was not provided, print warning and do nothing else, exiting "cleanly" with status 0. - if (typeof prefix !== "string" || prefix.length === 0) { - console.log("A prefix is required. Pass the desired prefix as the first argument to this script."); - console.log('Example usage: "node script/rename.js Windows"'); - console.log('or: "node script/rename.js ARM.Linux"'); - console.log("Exiting the binary renaming script."); +// Warn about required prefix as first argument. +// If prefix was not provided, print warning and do nothing else, +// exiting "cleanly" with status 0. +if (typeof prefix !== "string" || prefix.length === 0) { + console.log("A prefix is required. Pass the desired prefix as the first argument to this script."); + console.log('Example usage: "node script/rename.js Windows"'); + console.log('or: "node script/rename.js ARM.Linux"'); + console.log("Exiting the binary renaming script."); + process.exit(0); +} + +console.log(`Based on the version string in package.json (${pulsarVersion}),`); +console.log('we *are* preparing a Regular release, so *renaming all binaries with prefixes*.'); + +console.log(`Prefix is: ${prefix}`); + +// Renaming files under ./binaries/* that haven't already been renamed... +const fileNames = fs.readdirSync('./binaries'); + +fileNames.forEach((file) => { + console.log(`Existing filename is: ${file}`); + + // We use 'starting with "p" or "P"' as the heuristic for whether or not + // the file has already been renamed (and thus prefixed) before. + if (file.startsWith('p') || file.startsWith('P')) { + let dest = `${prefix}.${file}` + // Replace any spaces with periods in the resulting filenames + dest = dest.replace(/ /g,"."); + console.log(`Renaming file from "${file}" to "${dest}"`); + fs.renameSync(`./binaries/${file}`, `./binaries/${dest}`); } else { - - console.log(`Based on the version string in package.json (${pulsarVersion}),`); - console.log('we *are* preparing a Regular release, so *renaming all binaries with prefixes*.'); - - console.log(`Prefix is: ${prefix}`); - - // Rename files under ./binaries/* that haven't already been renamed - const fileNames = fs.readdirSync('./binaries'); - - fileNames.forEach((file) => { - console.log(`Existing filename is: ${file}`); - - // We use 'starting with "p" or "P"' as the heuristic - // for whether or not the file has already been renamed (and thus prefixed) before. - if (file.startsWith('p') || file.startsWith('P')) { - let dest = `${prefix}.${file}` - // Replace any spaces with periods in the resulting filenames - dest = dest.replace(/ /g,"."); - console.log(`Renaming file from "${file}" to "${dest}"`); - fs.renameSync(`./binaries/${file}`, `./binaries/${dest}`); - } else { - console.log('Filename does not start with "p" or "P". Skipping...'); - }; - }); + console.log('Filename does not start with "p" or "P". Skipping...'); }; -}; +}); } catch (e) { console.error("There was a problem during the renaming script!:");