CyberChef/Gruntfile.js

280 lines
9.2 KiB
JavaScript
Raw Normal View History

2017-04-13 20:00:55 +02:00
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const Inliner = require("web-resource-inliner");
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
*/
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.",
2017-07-04 00:15:57 +02:00
["clean:dev", "webpack-dev-server:start"]);
grunt.registerTask("node",
"Compiles CyberChef into a single NodeJS module.",
["clean:node", "webpack:node", "chmod:build"]);
2016-12-14 17:39:17 +01:00
grunt.registerTask("test",
2017-02-25 00:50:17 +01:00
"A task which runs all the tests in test/tests.",
2017-03-27 17:08:36 +02:00
["clean:test", "webpack:tests", "execute:test"]);
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
2017-03-27 17:08:36 +02:00
grunt.registerTask("prod",
"Creates a production-ready build. Use the --msg flag to add a compile message.",
2017-07-04 00:15:57 +02:00
["eslint", "clean:prod", "webpack:web", "inline", "chmod"]);
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
grunt.registerTask("inline",
"Compiles a production build of CyberChef into a single, portable web page.",
runInliner);
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-chmod");
grunt.loadNpmTasks("grunt-exec");
grunt.loadNpmTasks("grunt-execute");
grunt.loadNpmTasks("grunt-accessibility");
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)
};
2016-11-28 11:42:58 +01:00
2017-03-27 17:08:36 +02:00
/**
* Compiles a production build of CyberChef into a single, portable web page.
*/
function runInliner() {
2017-04-13 20:00:55 +02:00
const done = this.async();
2017-03-27 17:08:36 +02:00
Inliner.html({
relativeTo: "build/prod/",
fileContent: grunt.file.read("build/prod/cyberchef.htm"),
images: true,
svgs: true,
scripts: true,
links: true,
strict: true
}, function(error, result) {
if (error) {
2017-04-13 20:00:55 +02:00
if (error instanceof Error) {
2017-04-28 17:51:57 +02:00
done(error);
2017-04-13 20:00:55 +02:00
} else {
2017-04-28 17:51:57 +02:00
done(new Error(error));
2017-04-13 20:00:55 +02:00
}
} else {
2017-04-28 17:51:57 +02:00
grunt.file.write("build/prod/cyberchef.htm", result);
done(true);
2017-03-27 17:08:36 +02:00
}
});
}
2016-12-14 17:39:17 +01:00
2016-11-28 11:42:58 +01:00
grunt.initConfig({
2017-03-27 17:08:36 +02:00
clean: {
dev: ["build/dev/*"],
prod: ["build/prod/*"],
test: ["build/test/*"],
node: ["build/node/*"],
docs: ["docs/*", "!docs/*.conf.json", "!docs/*.ico"],
},
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
},
2017-03-27 20:39:04 +02:00
configs: ["Gruntfile.js"],
core: ["src/core/**/*.js", "!src/core/lib/**/*"],
web: ["src/web/**/*.js"],
node: ["src/node/**/*.js"],
tests: ["test/**/*.js"],
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",
"!src/core/lib/**/*",
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: {
target: "web",
entry: "./src/web/index.js",
output: {
filename: "scripts.js",
path: __dirname + "/build/prod"
},
plugins: [
2017-07-04 00:15:57 +02:00
new webpack.DefinePlugin(BUILD_CONSTANTS),
new webpack.optimize.UglifyJsPlugin({
compress: {
"screw_ie8": true,
"dead_code": true,
"unused": true,
"warnings": false
},
comments: false,
}),
2017-03-27 17:08:36 +02:00
new HtmlWebpackPlugin({ // Main version
filename: "index.html",
template: "./src/web/html/index.html",
compileTime: compileTime,
version: pkg.version,
2017-03-27 17:08:36 +02:00
minify: {
removeComments: true,
collapseWhitespace: true,
minifyJS: true,
minifyCSS: true
}
}),
new HtmlWebpackPlugin({ // Inline version
filename: "cyberchef.htm",
template: "./src/web/html/index.html",
compileTime: compileTime,
version: pkg.version,
2017-03-27 17:08:36 +02:00
inline: true,
minify: {
removeComments: true,
collapseWhitespace: true,
minifyJS: true,
minifyCSS: true
}
}),
]
},
tests: {
target: "node",
entry: "./test/index.js",
output: {
filename: "index.js",
path: __dirname + "/build/test"
}
},
node: {
target: "node",
entry: "./src/node/index.js",
output: {
filename: "CyberChef.js",
path: __dirname + "/build/node",
library: "CyberChef",
libraryTarget: "commonjs2"
}
}
},
2017-07-04 00:15:57 +02:00
"webpack-dev-server": {
options: {
webpack: webpackConfig,
stats: {
children: false,
warningsFilter: /source-map/
},
},
start: {
webpack: {
target: "web",
entry: "./src/web/index.js",
plugins: [
new webpack.DefinePlugin(BUILD_CONSTANTS),
new HtmlWebpackPlugin({
filename: "index.html",
template: "./src/web/html/index.html",
compileTime: compileTime,
version: pkg.version,
})
]
}
}
},
2016-11-28 11:42:58 +01:00
copy: {
ghPages: {
options: {
2017-04-13 20:00:55 +02:00
process: function (content) {
// Add Google Analytics code to index.html
content = content.replace("</body></html>",
grunt.file.read("src/web/static/ga.html") + "</body></html>");
2017-03-27 17:08:36 +02:00
return grunt.template.process(content);
}
},
src: "build/prod/index.html",
dest: "build/prod/index.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/"]
}
},
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"
},
},
execute: {
test: "build/test/index.js"
},
2016-11-28 11:42:58 +01:00
});
};