CyberChef/Gruntfile.js

435 lines
16 KiB
JavaScript
Raw Normal View History

"use strict";
2017-04-13 20:00:55 +02:00
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const NodeExternals = require("webpack-node-externals");
2018-03-27 00:14:23 +02:00
const glob = require("glob");
const path = require("path");
2016-12-14 17:39:17 +01:00
2017-07-04 00:15:57 +02:00
/**
* Grunt configuration for building the app in various formats.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2017
* @license Apache-2.0
*/
const NODE_PROD = process.env.NODE_ENV === "production";
module.exports = function (grunt) {
2016-11-28 11:42:58 +01:00
grunt.file.defaultEncoding = "utf8";
grunt.file.preserveBOM = false;
2016-12-14 17:39:17 +01:00
2016-11-28 11:42:58 +01:00
// Tasks
grunt.registerTask("dev",
"A persistent task which creates a development build whenever source files are modified.",
["clean:dev", "clean:config", "exec:generateConfig", "concurrent:dev"]);
2019-07-09 15:31:18 +02:00
grunt.registerTask("prod",
"Creates a production-ready build. Use the --msg flag to add a compile message.",
[
"eslint", "clean:prod", "clean:config", "exec:generateConfig", "webpack:web",
"copy:standalone", "zip:standalone", "clean:standalone", "chmod"
]);
grunt.registerTask("node",
"Compiles CyberChef into a single NodeJS module.",
2019-07-09 15:31:18 +02:00
[
"clean:node", "clean:config", "clean:nodeConfig", "exec:generateConfig",
"exec:generateNodeIndex", "webpack:node", "webpack:nodeRepl", "chmod:build"
]);
2016-12-14 17:39:17 +01:00
grunt.registerTask("test",
"A task which runs all the operation tests in the tests directory.",
2019-07-09 15:31:18 +02:00
[
"clean:config", "clean:nodeConfig", "exec:generateConfig", "exec:generateNodeIndex",
"exec:nodeTests", "exec:opTests"
]);
grunt.registerTask("testui",
2018-12-30 01:26:28 +01:00
"A task which runs all the UI tests in the tests directory. The prod task must already have been run.",
["connect:prod", "exec:browserTests"]);
2016-12-14 17:39:17 +01:00
2016-11-28 11:42:58 +01:00
grunt.registerTask("docs",
"Compiles documentation in the /docs directory.",
["clean:docs", "jsdoc", "chmod:docs"]);
2016-12-14 17:39:17 +01:00
2016-11-28 11:42:58 +01:00
grunt.registerTask("default",
"Lints the code base",
["eslint", "exec:repoSize"]);
2016-12-14 17:39:17 +01:00
2017-03-27 17:08:36 +02:00
2016-11-28 11:42:58 +01:00
grunt.registerTask("doc", "docs");
grunt.registerTask("tests", "test");
2016-12-14 17:39:17 +01:00
grunt.registerTask("lint", "eslint");
2016-11-28 11:42:58 +01:00
// Load tasks provided by each plugin
2016-12-14 17:39:17 +01:00
grunt.loadNpmTasks("grunt-eslint");
2017-03-27 17:08:36 +02:00
grunt.loadNpmTasks("grunt-webpack");
2016-11-28 11:42:58 +01:00
grunt.loadNpmTasks("grunt-jsdoc");
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-contrib-copy");
grunt.loadNpmTasks("grunt-contrib-watch");
2016-11-28 11:42:58 +01:00
grunt.loadNpmTasks("grunt-chmod");
grunt.loadNpmTasks("grunt-exec");
grunt.loadNpmTasks("grunt-accessibility");
grunt.loadNpmTasks("grunt-concurrent");
grunt.loadNpmTasks("grunt-contrib-connect");
grunt.loadNpmTasks("grunt-zip");
2016-12-14 17:39:17 +01:00
2017-03-27 17:08:36 +02:00
// Project configuration
2017-05-03 00:03:28 +02:00
const compileTime = grunt.template.today("UTC:dd/mm/yyyy HH:MM:ss") + " UTC",
2017-07-04 00:15:57 +02:00
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") || ""),
PKG_VERSION: JSON.stringify(pkg.version),
2017-08-09 21:09:23 +02:00
},
moduleEntryPoints = listEntryModules();
2016-11-28 11:42:58 +01:00
2016-12-14 17:39:17 +01:00
2017-08-09 21:09:23 +02:00
/**
* Generates an entry list for all the modules.
*/
function listEntryModules() {
const entryModules = {};
2017-08-09 21:09:23 +02:00
2018-03-27 00:14:23 +02:00
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);
2017-08-09 21:09:23 +02:00
});
return entryModules;
}
2016-11-28 11:42:58 +01:00
grunt.initConfig({
2017-03-27 17:08:36 +02:00
clean: {
dev: ["build/dev/*"],
2018-03-27 00:14:23 +02:00
prod: ["build/prod/*"],
node: ["build/node/*"],
2019-07-09 15:31:18 +02:00
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"],
2017-09-13 17:21:31 +02:00
docs: ["docs/*", "!docs/*.conf.json", "!docs/*.ico", "!docs/*.png"],
standalone: ["build/prod/CyberChef*.html"]
2017-03-27 17:08:36 +02:00
},
2016-12-14 17:39:17 +01:00
eslint: {
2016-11-28 11:42:58 +01:00
options: {
2017-04-28 17:51:57 +02:00
configFile: "./.eslintrc.json"
2016-11-28 11:42:58 +01:00
},
configs: ["*.{js,mjs}"],
2018-04-11 19:29:02 +02:00
core: ["src/core/**/*.{js,mjs}", "!src/core/vendor/**/*", "!src/core/operations/legacy/**/*"],
2019-03-28 00:02:10 +01:00
web: ["src/web/**/*.{js,mjs}", "!src/web/static/**/*"],
2018-03-27 00:14:23 +02:00
node: ["src/node/**/*.{js,mjs}"],
2018-12-28 22:49:40 +01:00
tests: ["tests/**/*.{js,mjs}"],
2016-11-28 11:42:58 +01:00
},
jsdoc: {
options: {
destination: "docs",
template: "node_modules/ink-docstrap/template",
recurse: true,
readme: "./README.md",
configure: "docs/jsdoc.conf.json"
},
all: {
src: [
"src/**/*.js",
2018-03-27 00:14:23 +02:00
"src/**/*.mjs",
"!src/core/vendor/**/*"
2016-11-28 11:42:58 +01:00
],
}
},
accessibility: {
options: {
accessibilityLevel: "WCAG2A",
verbose: false,
ignore: [
"WCAG2A.Principle1.Guideline1_3.1_3_1.H42.2"
]
},
test: {
src: ["build/**/*.html"]
}
},
webpack: {
2017-07-04 00:15:57 +02:00
options: webpackConfig,
web: () => {
return {
mode: "production",
target: "web",
entry: Object.assign({
main: "./src/web/index.js",
sitemap: "./src/web/static/sitemap.js"
}, moduleEntryPoints),
output: {
path: __dirname + "/build/prod",
2019-04-14 22:55:52 +02:00
filename: chunkData => {
return chunkData.chunk.name === "main" ? "assets/[name].js": "[name].js";
},
globalObject: "this"
},
resolve: {
alias: {
2019-07-09 14:42:42 +02:00
"./config/modules/OpModules.mjs": "./config/modules/Default.mjs"
2017-03-27 17:08:36 +02:00
}
},
plugins: [
new webpack.DefinePlugin(BUILD_CONSTANTS),
new HtmlWebpackPlugin({
filename: "index.html",
template: "./src/web/html/index.html",
chunks: ["main"],
compileTime: compileTime,
version: pkg.version,
minify: {
removeComments: true,
collapseWhitespace: true,
minifyJS: true,
minifyCSS: true
}
}),
new BundleAnalyzerPlugin({
analyzerMode: "static",
reportFilename: "BundleAnalyzerReport.html",
openAnalyzer: false
}),
]
};
},
node: {
mode: NODE_PROD ? "production" : "development",
target: "node",
2018-03-27 00:14:23 +02:00
entry: "./src/node/index.mjs",
externals: [NodeExternals({
whitelist: ["crypto-api/src/crypto-api"]
})],
output: {
filename: "CyberChef.js",
path: __dirname + "/build/node",
library: "CyberChef",
libraryTarget: "commonjs2"
},
plugins: [
new webpack.DefinePlugin(BUILD_CONSTANTS),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1
})
],
},
nodeRepl: {
mode: NODE_PROD ? "production" : "development",
target: "node",
entry: "./src/node/repl-index.mjs",
externals: [NodeExternals({
whitelist: ["crypto-api/src/crypto-api"]
})],
output: {
filename: "CyberChef-repl.js",
path: __dirname + "/build/node",
library: "CyberChef",
libraryTarget: "commonjs2"
},
plugins: [
new webpack.DefinePlugin(BUILD_CONSTANTS),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1
})
],
}
},
2017-07-04 00:15:57 +02:00
"webpack-dev-server": {
options: {
webpack: webpackConfig,
host: "0.0.0.0",
disableHostCheck: true,
overlay: true,
inline: false,
clientLogLevel: "error",
2017-07-04 00:15:57 +02:00
stats: {
children: false,
chunks: false,
modules: false,
entrypoints: false,
2018-10-10 17:49:07 +02:00
warningsFilter: [
/source-map/,
/dependency is an expression/,
2019-03-30 15:56:43 +01:00
/export 'default'/,
/Can't resolve 'sodium'/
2018-10-10 17:49:07 +02:00
],
}
2017-07-04 00:15:57 +02:00
},
start: {
webpack: {
mode: "development",
2017-07-04 00:15:57 +02:00
target: "web",
2017-08-09 21:09:23 +02:00
entry: Object.assign({
main: "./src/web/index.js"
}, moduleEntryPoints),
resolve: {
alias: {
2019-07-09 14:42:42 +02:00
"./config/modules/OpModules.mjs": "./config/modules/Default.mjs"
}
},
2017-07-04 00:15:57 +02:00
plugins: [
new webpack.DefinePlugin(BUILD_CONSTANTS),
new HtmlWebpackPlugin({
filename: "index.html",
template: "./src/web/html/index.html",
2017-08-09 21:09:23 +02:00
chunks: ["main"],
2017-07-04 00:15:57 +02:00
compileTime: compileTime,
version: pkg.version,
})
]
}
}
},
zip: {
standalone: {
cwd: "build/prod/",
src: [
"build/prod/**/*",
"!build/prod/index.html",
"!build/prod/BundleAnalyzerReport.html",
"!build/prod/sitemap.js"
],
dest: `build/prod/CyberChef_v${pkg.version}.zip`
}
},
connect: {
prod: {
options: {
port: 8000,
base: "build/prod/"
}
}
},
2016-11-28 11:42:58 +01:00
copy: {
ghPages: {
options: {
2017-09-13 17:21:31 +02:00
process: function (content, srcpath) {
if (srcpath.indexOf("index.html") >= 0) {
// Add Google Analytics code to index.html
2017-09-13 17:21:31 +02:00
content = content.replace("</body></html>",
grunt.file.read("src/web/static/ga.html") + "</body></html>");
// Add Structured Data for SEO
content = content.replace("</head>",
"<script type='application/ld+json'>" +
2019-04-14 22:55:52 +02:00
JSON.stringify(JSON.parse(grunt.file.read("src/web/static/structuredData.json"))) +
"</script></head>");
2017-09-13 17:21:31 +02:00
return grunt.template.process(content, srcpath);
} else {
return content;
}
},
noProcess: ["**", "!**/*.html"]
},
2017-09-13 17:21:31 +02:00
files: [
{
src: "build/prod/index.html",
dest: "build/prod/index.html"
},
{
expand: true,
src: "docs/**",
dest: "build/prod/"
},
2017-09-13 17:21:31 +02:00
]
},
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>`);
return grunt.template.process(content, srcpath);
} else {
return content;
}
},
noProcess: ["**", "!**/*.html"]
},
files: [
{
src: "build/prod/index.html",
dest: `build/prod/CyberChef_v${pkg.version}.html`
}
]
2016-11-28 11:42:58 +01:00
}
},
chmod: {
build: {
options: {
mode: "755",
},
2017-03-27 17:08:36 +02:00
src: ["build/**/*", "build/"]
2016-11-28 11:42:58 +01:00
},
docs: {
options: {
mode: "755",
},
src: ["docs/**/*", "docs/"]
}
},
watch: {
config: {
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
}
},
2016-11-28 11:42:58 +01:00
exec: {
repoSize: {
2016-11-28 11:42:58 +01:00
command: [
2016-12-14 17:39:17 +01:00
"git ls-files | wc -l | xargs printf '\n%b\ttracked files\n'",
"du -hs | egrep -o '^[^\t]*' | xargs printf '%b\trepository size\n'"
].join(";"),
2016-11-28 11:42:58 +01:00
stderr: false
},
cleanGit: {
2016-11-28 11:42:58 +01:00
command: "git gc --prune=now --aggressive"
},
sitemap: {
command: "node build/prod/sitemap.js > build/prod/sitemap.xml"
2018-03-27 00:14:23 +02:00
},
generateConfig: {
command: [
"echo '\n--- Regenerating config files. ---'",
"echo [] > src/core/config/OperationConfig.json",
"node --experimental-modules --no-warnings --no-deprecation src/core/config/scripts/generateOpsIndex.mjs",
"node --experimental-modules --no-warnings --no-deprecation src/core/config/scripts/generateConfig.mjs",
"echo '--- Config scripts finished. ---\n'"
].join(";")
},
generateNodeIndex: {
command: [
"echo '\n--- Regenerating node index ---'",
"node --experimental-modules --no-warnings --no-deprecation src/node/config/scripts/generateNodeIndex.mjs",
2018-06-19 09:53:29 +02:00
"echo '--- Node index generated. ---\n'"
].join(";"),
},
opTests: {
command: "node --experimental-modules --no-warnings --no-deprecation tests/operations/index.mjs"
},
browserTests: {
command: "./node_modules/.bin/nightwatch --env prod"
},
nodeTests: {
command: "node --experimental-modules --no-warnings --no-deprecation tests/node/index.mjs"
}
},
2016-11-28 11:42:58 +01:00
});
};