add formatter

This commit is contained in:
Hare Sudhan 2024-02-24 22:59:51 -05:00
parent c4e7c41a6e
commit ce30989adc
693 changed files with 51226 additions and 26671 deletions

View file

@ -2,11 +2,13 @@
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const BundleAnalyzerPlugin =
require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const glob = require("glob");
const path = require("path");
const nodeFlags = "--experimental-modules --experimental-json-modules --experimental-specifier-resolution=node --no-warnings --no-deprecation";
const nodeFlags =
"--experimental-modules --experimental-json-modules --experimental-specifier-resolution=node --no-warnings --no-deprecation";
/**
* Grunt configuration for building the app in various formats.
@ -21,56 +23,100 @@ module.exports = function (grunt) {
grunt.file.preserveBOM = false;
// Tasks
grunt.registerTask("dev",
grunt.registerTask(
"dev",
"A persistent task which creates a development build whenever source files are modified.",
["clean:dev", "clean:config", "exec:generateConfig", "concurrent:dev"]);
["clean:dev", "clean:config", "exec:generateConfig", "concurrent:dev"],
);
grunt.registerTask("prod",
grunt.registerTask(
"prod",
"Creates a production-ready build. Use the --msg flag to add a compile message.",
[
"eslint", "clean:prod", "clean:config", "exec:generateConfig", "findModules", "webpack:web",
"copy:standalone", "zip:standalone", "clean:standalone", "exec:calcDownloadHash", "chmod"
]);
"eslint",
"clean:prod",
"clean:config",
"exec:generateConfig",
"findModules",
"webpack:web",
"copy:standalone",
"zip:standalone",
"clean:standalone",
"exec:calcDownloadHash",
"chmod",
],
);
grunt.registerTask("node",
grunt.registerTask(
"node",
"Compiles CyberChef into a single NodeJS module.",
[
"clean:node", "clean:config", "clean:nodeConfig", "exec:generateConfig", "exec:generateNodeIndex"
]);
"clean:node",
"clean:config",
"clean:nodeConfig",
"exec:generateConfig",
"exec:generateNodeIndex",
],
);
grunt.registerTask("configTests",
grunt.registerTask(
"configTests",
"A task which configures config files in preparation for tests to be run. Use `npm test` to run tests.",
[
"clean:config", "clean:nodeConfig", "exec:generateConfig", "exec:generateNodeIndex"
]);
"clean:config",
"clean:nodeConfig",
"exec:generateConfig",
"exec:generateNodeIndex",
],
);
grunt.registerTask("testui",
grunt.registerTask(
"testui",
"A task which runs all the UI tests in the tests directory. The prod task must already have been run.",
["connect:prod", "exec:browserTests"]);
["connect:prod", "exec:browserTests"],
);
grunt.registerTask("testnodeconsumer",
grunt.registerTask(
"testnodeconsumer",
"A task which checks whether consuming CJS and ESM apps work with the CyberChef build",
["exec:setupNodeConsumers", "exec:testCJSNodeConsumer", "exec:testESMNodeConsumer", "exec:teardownNodeConsumers"]);
[
"exec:setupNodeConsumers",
"exec:testCJSNodeConsumer",
"exec:testESMNodeConsumer",
"exec:teardownNodeConsumers",
],
);
grunt.registerTask("default",
"Lints the code base",
["eslint", "exec:repoSize"]);
grunt.registerTask("default", "Lints the code base", [
"prettier",
"eslint",
"exec:repoSize",
]);
grunt.registerTask("lint", "eslint");
grunt.registerTask("format", ["prettier"]);
grunt.registerTask("findModules",
grunt.registerTask(
"findModules",
"Finds all generated modules and updates the entry point list for Webpack",
function(arg1, arg2) {
function (arg1, arg2) {
const moduleEntryPoints = listEntryModules();
grunt.log.writeln(`Found ${Object.keys(moduleEntryPoints).length} modules.`);
grunt.config.set("webpack.web.entry",
Object.assign({
main: "./src/web/index.js"
}, moduleEntryPoints));
});
grunt.log.writeln(
`Found ${Object.keys(moduleEntryPoints).length} modules.`,
);
grunt.config.set(
"webpack.web.entry",
Object.assign(
{
main: "./src/web/index.js",
},
moduleEntryPoints,
),
);
},
);
// Load tasks provided by each plugin
grunt.loadNpmTasks("grunt-eslint");
@ -83,15 +129,18 @@ module.exports = function (grunt) {
grunt.loadNpmTasks("grunt-concurrent");
grunt.loadNpmTasks("grunt-contrib-connect");
grunt.loadNpmTasks("grunt-zip");
grunt.loadNpmTasks("grunt-prettier");
// Project configuration
const compileTime = grunt.template.today("UTC:dd/mm/yyyy HH:MM:ss") + " UTC",
const compileTime =
grunt.template.today("UTC:dd/mm/yyyy HH:MM:ss") + " UTC",
pkg = grunt.file.readJSON("package.json"),
webpackConfig = require("./webpack.config.js"),
BUILD_CONSTANTS = {
COMPILE_TIME: JSON.stringify(compileTime),
COMPILE_MSG: JSON.stringify(grunt.option("compile-msg") || grunt.option("msg") || ""),
COMPILE_MSG: JSON.stringify(
grunt.option("compile-msg") || grunt.option("msg") || "",
),
PKG_VERSION: JSON.stringify(pkg.version),
},
moduleEntryPoints = listEntryModules(),
@ -104,20 +153,26 @@ module.exports = function (grunt) {
return {
mode: "production",
target: "web",
entry: Object.assign({
main: "./src/web/index.js"
}, moduleEntryPoints),
entry: Object.assign(
{
main: "./src/web/index.js",
},
moduleEntryPoints,
),
output: {
path: __dirname + "/build/prod",
filename: chunkData => {
return chunkData.chunk.name === "main" ? "assets/[name].js": "[name].js";
filename: (chunkData) => {
return chunkData.chunk.name === "main"
? "assets/[name].js"
: "[name].js";
},
globalObject: "this"
globalObject: "this",
},
resolve: {
alias: {
"./config/modules/OpModules.mjs": "./config/modules/Default.mjs"
}
"./config/modules/OpModules.mjs":
"./config/modules/Default.mjs",
},
},
plugins: [
new webpack.DefinePlugin(BUILD_CONSTANTS),
@ -131,29 +186,29 @@ module.exports = function (grunt) {
removeComments: true,
collapseWhitespace: true,
minifyJS: true,
minifyCSS: true
}
minifyCSS: true,
},
}),
new BundleAnalyzerPlugin({
analyzerMode: "static",
reportFilename: "BundleAnalyzerReport.html",
openAnalyzer: false
openAnalyzer: false,
}),
]
],
};
};
/**
* Generates an entry list for all the modules.
*/
function listEntryModules() {
const entryModules = {};
glob.sync("./src/core/config/modules/*.mjs").forEach(file => {
glob.sync("./src/core/config/modules/*.mjs").forEach((file) => {
const basename = path.basename(file);
if (basename !== "Default.mjs" && basename !== "OpModules.mjs")
entryModules["modules/" + basename.split(".mjs")[0]] = path.resolve(file);
entryModules["modules/" + basename.split(".mjs")[0]] =
path.resolve(file);
});
return entryModules;
@ -171,12 +226,14 @@ module.exports = function (grunt) {
if (!win) {
return cmds.join(";");
}
return cmds
// && means that subsequent commands will not be executed if the
// previous one fails. & would coninue on a fail
.join("&&")
// Windows does not support \n properly
.replace(/\n/g, "\\n");
return (
cmds
// && means that subsequent commands will not be executed if the
// previous one fails. & would coninue on a fail
.join("&&")
// Windows does not support \n properly
.replace(/\n/g, "\\n")
);
}
grunt.initConfig({
@ -184,17 +241,36 @@ module.exports = function (grunt) {
dev: ["build/dev/*"],
prod: ["build/prod/*"],
node: ["build/node/*"],
config: ["src/core/config/OperationConfig.json", "src/core/config/modules/*", "src/code/operations/index.mjs"],
nodeConfig: ["src/node/index.mjs", "src/node/config/OperationConfig.json"],
standalone: ["build/prod/CyberChef*.html"]
config: [
"src/core/config/OperationConfig.json",
"src/core/config/modules/*",
"src/code/operations/index.mjs",
],
nodeConfig: [
"src/node/index.mjs",
"src/node/config/OperationConfig.json",
],
standalone: ["build/prod/CyberChef*.html"],
},
eslint: {
configs: ["*.{js,mjs}"],
core: ["src/core/**/*.{js,mjs}", "!src/core/vendor/**/*", "!src/core/operations/legacy/**/*"],
core: [
"src/core/**/*.{js,mjs}",
"!src/core/vendor/**/*",
"!src/core/operations/legacy/**/*",
],
web: ["src/web/**/*.{js,mjs}", "!src/web/static/**/*"],
node: ["src/node/**/*.{js,mjs}"],
tests: ["tests/**/*.{js,mjs}"],
},
prettier: {
options: {
progress: false,
},
files: {
src: ["*.{js,mjs}", "src/**/*.{js,mjs}", "tests/**/*.{js,mjs}"],
},
},
webpack: {
options: webpackConfig,
myConfig: webpackConfig,
@ -205,21 +281,25 @@ module.exports = function (grunt) {
start: {
mode: "development",
target: "web",
entry: Object.assign({
main: "./src/web/index.js"
}, moduleEntryPoints),
entry: Object.assign(
{
main: "./src/web/index.js",
},
moduleEntryPoints,
),
resolve: {
alias: {
"./config/modules/OpModules.mjs": "./config/modules/Default.mjs"
}
"./config/modules/OpModules.mjs":
"./config/modules/Default.mjs",
},
},
devServer: {
port: grunt.option("port") || 8080,
client: {
logging: "error",
overlay: true
overlay: true,
},
hot: "only"
hot: "only",
},
plugins: [
new webpack.DefinePlugin(BUILD_CONSTANTS),
@ -229,9 +309,9 @@ module.exports = function (grunt) {
chunks: ["main"],
compileTime: compileTime,
version: pkg.version,
})
]
}
}),
],
},
},
zip: {
standalone: {
@ -241,16 +321,16 @@ module.exports = function (grunt) {
"!build/prod/index.html",
"!build/prod/BundleAnalyzerReport.html",
],
dest: `build/prod/CyberChef_v${pkg.version}.zip`
}
dest: `build/prod/CyberChef_v${pkg.version}.zip`,
},
},
connect: {
prod: {
options: {
port: grunt.option("port") || 8000,
base: "build/prod/"
}
}
base: "build/prod/",
},
},
},
copy: {
ghPages: {
@ -258,70 +338,86 @@ module.exports = function (grunt) {
process: function (content, srcpath) {
if (srcpath.indexOf("index.html") >= 0) {
// Add Google Analytics code to index.html
content = content.replace("</body></html>",
grunt.file.read("src/web/static/ga.html") + "</body></html>");
content = content.replace(
"</body></html>",
grunt.file.read("src/web/static/ga.html") +
"</body></html>",
);
// Add Structured Data for SEO
content = content.replace("</head>",
content = content.replace(
"</head>",
"<script type='application/ld+json'>" +
JSON.stringify(JSON.parse(grunt.file.read("src/web/static/structuredData.json"))) +
"</script></head>");
JSON.stringify(
JSON.parse(
grunt.file.read(
"src/web/static/structuredData.json",
),
),
) +
"</script></head>",
);
return grunt.template.process(content, srcpath);
} else {
return content;
}
},
noProcess: ["**", "!**/*.html"]
noProcess: ["**", "!**/*.html"],
},
files: [
{
src: ["build/prod/index.html"],
dest: "build/prod/index.html"
}
]
dest: "build/prod/index.html",
},
],
},
standalone: {
options: {
process: function (content, srcpath) {
if (srcpath.indexOf("index.html") >= 0) {
// Replace download link with version number
content = content.replace(/<a [^>]+>Download CyberChef.+?<\/a>/,
`<span>Version ${pkg.version}</span>`);
content = content.replace(
/<a [^>]+>Download CyberChef.+?<\/a>/,
`<span>Version ${pkg.version}</span>`,
);
return grunt.template.process(content, srcpath);
} else {
return content;
}
},
noProcess: ["**", "!**/*.html"]
noProcess: ["**", "!**/*.html"],
},
files: [
{
src: ["build/prod/index.html"],
dest: `build/prod/CyberChef_v${pkg.version}.html`
}
]
}
dest: `build/prod/CyberChef_v${pkg.version}.html`,
},
],
},
},
chmod: {
build: {
options: {
mode: "755",
},
src: ["build/**/*", "build/"]
}
src: ["build/**/*", "build/"],
},
},
watch: {
config: {
files: ["src/core/operations/**/*", "!src/core/operations/index.mjs"],
tasks: ["exec:generateNodeIndex", "exec:generateConfig"]
}
files: [
"src/core/operations/**/*",
"!src/core/operations/index.mjs",
],
tasks: ["exec:generateNodeIndex", "exec:generateConfig"],
},
},
concurrent: {
dev: ["watch:config", "webpack-dev-server:start"],
options: {
logConcurrentOutput: true
}
logConcurrentOutput: true,
},
},
exec: {
calcDownloadHash: {
@ -330,12 +426,12 @@ module.exports = function (grunt) {
case "darwin":
return chainCommands([
`shasum -a 256 build/prod/CyberChef_v${pkg.version}.zip | awk '{print $1;}' > build/prod/sha256digest.txt`,
`sed -i '' -e "s/DOWNLOAD_HASH_PLACEHOLDER/$(cat build/prod/sha256digest.txt)/" build/prod/index.html`
`sed -i '' -e "s/DOWNLOAD_HASH_PLACEHOLDER/$(cat build/prod/sha256digest.txt)/" build/prod/index.html`,
]);
default:
return chainCommands([
`sha256sum build/prod/CyberChef_v${pkg.version}.zip | awk '{print $1;}' > build/prod/sha256digest.txt`,
`sed -i -e "s/DOWNLOAD_HASH_PLACEHOLDER/$(cat build/prod/sha256digest.txt)/" build/prod/index.html`
`sed -i -e "s/DOWNLOAD_HASH_PLACEHOLDER/$(cat build/prod/sha256digest.txt)/" build/prod/index.html`,
]);
}
},
@ -343,16 +439,16 @@ module.exports = function (grunt) {
repoSize: {
command: chainCommands([
"git ls-files | wc -l | xargs printf '\n%b\ttracked files\n'",
"du -hs | egrep -o '^[^\t]*' | xargs printf '%b\trepository size\n'"
"du -hs | egrep -o '^[^\t]*' | xargs printf '%b\trepository size\n'",
]),
stderr: false
stderr: false,
},
cleanGit: {
command: "git gc --prune=now --aggressive"
command: "git gc --prune=now --aggressive",
},
sitemap: {
command: `node ${nodeFlags} src/web/static/sitemap.mjs > build/prod/sitemap.xml`,
sync: true
sync: true,
},
generateConfig: {
command: chainCommands([
@ -360,20 +456,20 @@ module.exports = function (grunt) {
"echo [] > src/core/config/OperationConfig.json",
`node ${nodeFlags} src/core/config/scripts/generateOpsIndex.mjs`,
`node ${nodeFlags} src/core/config/scripts/generateConfig.mjs`,
"echo '--- Config scripts finished. ---\n'"
"echo '--- Config scripts finished. ---\n'",
]),
sync: true
sync: true,
},
generateNodeIndex: {
command: chainCommands([
"echo '\n--- Regenerating node index ---'",
`node ${nodeFlags} src/node/config/scripts/generateNodeIndex.mjs`,
"echo '--- Node index generated. ---\n'"
"echo '--- Node index generated. ---\n'",
]),
sync: true
sync: true,
},
browserTests: {
command: "./node_modules/.bin/nightwatch --env prod"
command: "./node_modules/.bin/nightwatch --env prod",
},
setupNodeConsumers: {
command: chainCommands([
@ -382,14 +478,14 @@ module.exports = function (grunt) {
`mkdir ${nodeConsumerTestPath}`,
`cp tests/node/consumers/* ${nodeConsumerTestPath}`,
`cd ${nodeConsumerTestPath}`,
"npm link cyberchef"
"npm link cyberchef",
]),
sync: true
sync: true,
},
teardownNodeConsumers: {
command: chainCommands([
`rm -rf ${nodeConsumerTestPath}`,
"echo '\n--- Node consumer tests complete ---'"
"echo '\n--- Node consumer tests complete ---'",
]),
},
testCJSNodeConsumer: {
@ -415,7 +511,7 @@ module.exports = function (grunt) {
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'`;
}
},
stdout: false
stdout: false,
},
fixSnackbarMarkup: {
command: function () {
@ -426,8 +522,8 @@ module.exports = function (grunt) {
return `sed -i 's/<div id=snackbar-container\\/>/<div id=snackbar-container>/g' ./node_modules/snackbarjs/src/snackbar.js`;
}
},
stdout: false
}
stdout: false,
},
},
});
};