Skip to content

Commit d1a2b8a

Browse files
committed
fix: improve platform-dependent command execution in grunt tasks
Fixes #2010
1 parent 7c8be12 commit d1a2b8a

File tree

1 file changed

+21
-43
lines changed

1 file changed

+21
-43
lines changed

Gruntfile.js

+21-43
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@
33
const webpack = require("webpack");
44
const HtmlWebpackPlugin = require("html-webpack-plugin");
55
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
6-
const glob = require("glob");
7-
const path = require("path");
6+
const glob = require("node:glob");
7+
const path = require("node:path");
8+
const cp = require("node:child_process");
89

910
const nodeFlags = "--experimental-modules --experimental-json-modules --experimental-specifier-resolution=node --no-warnings --no-deprecation";
1011

12+
// Prepare platform-dependent commands
13+
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";
20+
1121
/**
1222
* Grunt configuration for building the app in various formats.
1323
*
@@ -60,7 +70,7 @@ module.exports = function (grunt) {
6070

6171
grunt.registerTask("findModules",
6272
"Finds all generated modules and updates the entry point list for Webpack",
63-
function(arg1, arg2) {
73+
function (arg1, arg2) {
6474
const moduleEntryPoints = listEntryModules();
6575

6676
grunt.log.writeln(`Found ${Object.keys(moduleEntryPoints).length} modules.`);
@@ -112,7 +122,7 @@ module.exports = function (grunt) {
112122
output: {
113123
path: __dirname + "/build/prod",
114124
filename: chunkData => {
115-
return chunkData.chunk.name === "main" ? "assets/[name].js": "[name].js";
125+
return chunkData.chunk.name === "main" ? "assets/[name].js" : "[name].js";
116126
},
117127
globalObject: "this"
118128
},
@@ -329,20 +339,10 @@ module.exports = function (grunt) {
329339
},
330340
exec: {
331341
calcDownloadHash: {
332-
command: function () {
333-
switch (process.platform) {
334-
case "darwin":
335-
return chainCommands([
336-
`shasum -a 256 build/prod/CyberChef_v${pkg.version}.zip | awk '{print $1;}' > build/prod/sha256digest.txt`,
337-
`sed -i '' -e "s/DOWNLOAD_HASH_PLACEHOLDER/$(cat build/prod/sha256digest.txt)/" build/prod/index.html`
338-
]);
339-
default:
340-
return chainCommands([
341-
`sha256sum build/prod/CyberChef_v${pkg.version}.zip | awk '{print $1;}' > build/prod/sha256digest.txt`,
342-
`sed -i -e "s/DOWNLOAD_HASH_PLACEHOLDER/$(cat build/prod/sha256digest.txt)/" build/prod/index.html`
343-
]);
344-
}
345-
},
342+
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`,
345+
]),
346346
},
347347
repoSize: {
348348
command: chainCommands([
@@ -411,37 +411,15 @@ module.exports = function (grunt) {
411411
stdout: false,
412412
},
413413
fixCryptoApiImports: {
414-
command: function () {
415-
switch (process.platform) {
416-
case "darwin":
417-
return `find ./node_modules/crypto-api/src/ \\( -type d -name .git -prune \\) -o -type f -print0 | xargs -0 sed -i '' -e '/\\.mjs/!s/\\(from "\\.[^"]*\\)";/\\1.mjs";/g'`;
418-
default:
419-
return `find ./node_modules/crypto-api/src/ \\( -type d -name .git -prune \\) -o -type f -print0 | xargs -0 sed -i -e '/\\.mjs/!s/\\(from "\\.[^"]*\\)";/\\1.mjs";/g'`;
420-
}
421-
},
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'`,
422415
stdout: false
423416
},
424417
fixSnackbarMarkup: {
425-
command: function () {
426-
switch (process.platform) {
427-
case "darwin":
428-
return `sed -i '' 's/<div id=snackbar-container\\/>/<div id=snackbar-container>/g' ./node_modules/snackbarjs/src/snackbar.js`;
429-
default:
430-
return `sed -i 's/<div id=snackbar-container\\/>/<div id=snackbar-container>/g' ./node_modules/snackbarjs/src/snackbar.js`;
431-
}
432-
},
418+
command: `${sedCmd} 's/<div id=snackbar-container\\/>/<div id=snackbar-container>/g' ./node_modules/snackbarjs/src/snackbar.js`,
433419
stdout: false
434420
},
435421
fixJimpModule: {
436-
command: function () {
437-
switch (process.platform) {
438-
case "darwin":
439-
// Space added before comma to prevent multiple modifications
440-
return `sed -i '' 's/"es\\/index.js",/"es\\/index.js" ,\\n "type": "module",/' ./node_modules/jimp/package.json`;
441-
default:
442-
return `sed -i 's/"es\\/index.js",/"es\\/index.js" ,\\n "type": "module",/' ./node_modules/jimp/package.json`;
443-
}
444-
},
422+
command: `${sedCmd} 's/"es\\/index.js",/"es\\/index.js" ,\\n "type": "module",/' ./node_modules/jimp/package.json`,
445423
stdout: false
446424
}
447425
},

0 commit comments

Comments
 (0)