Skip to content

Commit eef1d5d

Browse files
committed
fix: prevent unnecessary execution of sed
Use lazy, memoized getter to prevent unnecessary execution of `sed` every time the Gruntfile.js file is loaded.
1 parent 13d776f commit eef1d5d

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

Gruntfile.js

+19-12
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@ const cp = require("node:child_process");
99

1010
const nodeFlags = "--experimental-modules --experimental-json-modules --experimental-specifier-resolution=node --no-warnings --no-deprecation";
1111

12-
// Prepare platform-dependent commands
12+
// Define platform-dependent commands
13+
const pcmd = {
14+
// Older MacOS versions don't come with `sha256sum`, but they all come with `shasum`
15+
sha256sum: process.platform === "darwin" ? "shasum -a 256" : "sha256sum",
1316

14-
// Older MacOS versions don't come with `sha256sum`, but they all come with `shasum`
15-
const sha256sumCmd = process.platform === "darwin" ? "shasum -a 256" : "sha256sum";
16-
17-
// MacOS (and FreeBSD, but not OpenBSD) require an argument to `-i`.
18-
// However, users may have installed GNU sed, so let's check what `sed` says itself.
19-
const sedCmd = cp.execSync("sed -i 2>&1 | head -n1").toString().includes("option requires an argument") ? "sed -i ''" : "sed -i";
17+
// MacOS (and FreeBSD, but not OpenBSD) require an argument to `-i`.
18+
// However, users may have installed GNU sed, so let's check what `sed` says itself.
19+
// NB: using a lazy, memoized getter so this is only executed at most once, upon first access.
20+
get sed() {
21+
delete this.sed;
22+
return this.sed = (
23+
cp.execSync("sed -i 2>&1 | head -n1").toString().includes("option requires an argument") ? "sed -i ''" : "sed -i"
24+
);
25+
}
26+
};
2027

2128
/**
2229
* Grunt configuration for building the app in various formats.
@@ -340,8 +347,8 @@ module.exports = function (grunt) {
340347
exec: {
341348
calcDownloadHash: {
342349
command: chainCommands([
343-
`${sha256sumCmd} build/prod/CyberChef_v${pkg.version}.zip | awk '{print $1;}' > build/prod/sha256digest.txt`,
344-
`${sedCmd} -e "s/DOWNLOAD_HASH_PLACEHOLDER/$(cat build/prod/sha256digest.txt)/" build/prod/index.html`,
350+
`${pcmd.sha256sum} build/prod/CyberChef_v${pkg.version}.zip | awk '{print $1;}' > build/prod/sha256digest.txt`,
351+
`${pcmd.sed} -e "s/DOWNLOAD_HASH_PLACEHOLDER/$(cat build/prod/sha256digest.txt)/" build/prod/index.html`,
345352
]),
346353
},
347354
repoSize: {
@@ -411,15 +418,15 @@ module.exports = function (grunt) {
411418
stdout: false,
412419
},
413420
fixCryptoApiImports: {
414-
command: `find ./node_modules/crypto-api/src/ \\( -type d -name .git -prune \\) -o -type f -print0 | xargs -0 ${sedCmd} -e '/\\.mjs/!s/\\(from "\\.[^"]*\\)";/\\1.mjs";/g'`,
421+
command: `find ./node_modules/crypto-api/src/ \\( -type d -name .git -prune \\) -o -type f -print0 | xargs -0 ${pcmd.sed} -e '/\\.mjs/!s/\\(from "\\.[^"]*\\)";/\\1.mjs";/g'`,
415422
stdout: false
416423
},
417424
fixSnackbarMarkup: {
418-
command: `${sedCmd} 's/<div id=snackbar-container\\/>/<div id=snackbar-container>/g' ./node_modules/snackbarjs/src/snackbar.js`,
425+
command: `${pcmd.sed} 's/<div id=snackbar-container\\/>/<div id=snackbar-container>/g' ./node_modules/snackbarjs/src/snackbar.js`,
419426
stdout: false
420427
},
421428
fixJimpModule: {
422-
command: `${sedCmd} 's/"es\\/index.js",/"es\\/index.js" ,\\n "type": "module",/' ./node_modules/jimp/package.json`,
429+
command: `${pcmd.sed} 's/"es\\/index.js",/"es\\/index.js" ,\\n "type": "module",/' ./node_modules/jimp/package.json`,
423430
stdout: false
424431
}
425432
},

0 commit comments

Comments
 (0)