Gruntfile.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /* eslint-env node */
  2. module.exports = function(grunt) {
  3. grunt.file.defaultEncoding = "utf8";
  4. grunt.file.preserveBOM = false;
  5. // Tasks
  6. grunt.registerTask("dev",
  7. "A persistent task which creates a development build whenever source files are modified.",
  8. ["clean:dev", "concat:css", "concat:js", "copy:htmlDev", "copy:staticDev", "chmod:build", "watch"]);
  9. grunt.registerTask("prod",
  10. "Creates a production-ready build. Use the --msg flag to add a compile message.",
  11. ["eslint", "exec:stats", "clean", "jsdoc", "concat", "copy:htmlDev", "copy:htmlProd", "copy:htmlInline",
  12. "copy:staticDev", "copy:staticProd", "cssmin", "uglify:prod", "inline", "htmlmin", "chmod"]);
  13. grunt.registerTask("docs",
  14. "Compiles documentation in the /docs directory.",
  15. ["clean:docs", "jsdoc", "chmod:docs"]);
  16. grunt.registerTask("stats",
  17. "Provides statistics about the code base such as how many lines there are as well as details of file sizes before and after compression.",
  18. ["concat:js", "uglify:prod", "exec:stats", "exec:repoSize", "exec:displayStats"]);
  19. grunt.registerTask("release",
  20. "Prepares and deploys a production version of CyberChef to the gh-pages branch.",
  21. ["copy:ghPages", "exec:deployGhPages"]);
  22. grunt.registerTask("default",
  23. "Lints the code base and shows stats",
  24. ["eslint", "exec:stats", "exec:displayStats"]);
  25. grunt.registerTask("doc", "docs");
  26. grunt.registerTask("lint", "eslint");
  27. // Load tasks provided by each plugin
  28. grunt.loadNpmTasks("grunt-eslint");
  29. grunt.loadNpmTasks("grunt-jsdoc");
  30. grunt.loadNpmTasks("grunt-contrib-clean");
  31. grunt.loadNpmTasks("grunt-contrib-concat");
  32. grunt.loadNpmTasks("grunt-contrib-copy");
  33. grunt.loadNpmTasks("grunt-contrib-uglify");
  34. grunt.loadNpmTasks("grunt-contrib-cssmin");
  35. grunt.loadNpmTasks("grunt-contrib-htmlmin");
  36. grunt.loadNpmTasks("grunt-inline-alt");
  37. grunt.loadNpmTasks("grunt-chmod");
  38. grunt.loadNpmTasks("grunt-exec");
  39. grunt.loadNpmTasks("grunt-contrib-watch");
  40. // JS includes
  41. var jsFiles = [
  42. // Third party framework libraries
  43. "src/js/lib/jquery-2.1.1.js",
  44. "src/js/lib/bootstrap-3.3.6.js",
  45. "src/js/lib/split.js",
  46. "src/js/lib/bootstrap-switch.js",
  47. "src/js/lib/yahoo.js",
  48. "src/js/lib/snowfall.jquery.js",
  49. // Third party operation libraries
  50. "src/js/lib/cryptojs/core.js",
  51. "src/js/lib/cryptojs/x64-core.js",
  52. "src/js/lib/cryptojs/enc-base64.js",
  53. "src/js/lib/cryptojs/enc-utf16.js",
  54. "src/js/lib/cryptojs/md5.js",
  55. "src/js/lib/cryptojs/evpkdf.js",
  56. "src/js/lib/cryptojs/cipher-core.js",
  57. "src/js/lib/cryptojs/mode-cfb.js",
  58. "src/js/lib/cryptojs/mode-ctr-gladman.js",
  59. "src/js/lib/cryptojs/mode-ctr.js",
  60. "src/js/lib/cryptojs/mode-ecb.js",
  61. "src/js/lib/cryptojs/mode-ofb.js",
  62. "src/js/lib/cryptojs/format-hex.js",
  63. "src/js/lib/cryptojs/lib-typedarrays.js",
  64. "src/js/lib/cryptojs/pad-ansix923.js",
  65. "src/js/lib/cryptojs/pad-iso10126.js",
  66. "src/js/lib/cryptojs/pad-iso97971.js",
  67. "src/js/lib/cryptojs/pad-nopadding.js",
  68. "src/js/lib/cryptojs/pad-zeropadding.js",
  69. "src/js/lib/cryptojs/aes.js",
  70. "src/js/lib/cryptojs/hmac.js",
  71. "src/js/lib/cryptojs/rabbit-legacy.js",
  72. "src/js/lib/cryptojs/rabbit.js",
  73. "src/js/lib/cryptojs/ripemd160.js",
  74. "src/js/lib/cryptojs/sha1.js",
  75. "src/js/lib/cryptojs/sha256.js",
  76. "src/js/lib/cryptojs/sha224.js",
  77. "src/js/lib/cryptojs/sha512.js",
  78. "src/js/lib/cryptojs/sha384.js",
  79. "src/js/lib/cryptojs/sha3.js",
  80. "src/js/lib/cryptojs/tripledes.js",
  81. "src/js/lib/cryptojs/rc4.js",
  82. "src/js/lib/cryptojs/pbkdf2.js",
  83. "src/js/lib/cryptoapi/crypto-api.js",
  84. "src/js/lib/cryptoapi/hasher.md2.js",
  85. "src/js/lib/cryptoapi/hasher.md4.js",
  86. "src/js/lib/cryptoapi/hasher.sha0.js",
  87. "src/js/lib/jsbn/jsbn.js",
  88. "src/js/lib/jsbn/jsbn2.js",
  89. "src/js/lib/jsbn/base64.js",
  90. "src/js/lib/jsbn/ec.js",
  91. "src/js/lib/jsbn/prng4.js",
  92. "src/js/lib/jsbn/rng.js",
  93. "src/js/lib/jsbn/rsa.js",
  94. "src/js/lib/jsbn/sec.js",
  95. "src/js/lib/jsrasign/asn1-1.0.js",
  96. "src/js/lib/jsrasign/asn1hex-1.1.js",
  97. "src/js/lib/jsrasign/asn1x509-1.0.js",
  98. "src/js/lib/jsrasign/base64x-1.1.js",
  99. "src/js/lib/jsrasign/crypto-1.1.js",
  100. "src/js/lib/jsrasign/dsa-modified-1.0.js",
  101. "src/js/lib/jsrasign/ecdsa-modified-1.0.js",
  102. "src/js/lib/jsrasign/ecparam-1.0.js",
  103. "src/js/lib/jsrasign/keyutil-1.0.js",
  104. "src/js/lib/jsrasign/x509-1.1.js",
  105. "src/js/lib/blowfish.dojo.js",
  106. "src/js/lib/rawdeflate.js",
  107. "src/js/lib/rawinflate.js",
  108. "src/js/lib/zip.js",
  109. "src/js/lib/unzip.js",
  110. "src/js/lib/zlib_and_gzip.js",
  111. "src/js/lib/bzip2.js",
  112. "src/js/lib/punycode.js",
  113. "src/js/lib/uas_parser.js",
  114. "src/js/lib/esprima.js",
  115. "src/js/lib/escodegen.browser.js",
  116. "src/js/lib/esmangle.min.js",
  117. "src/js/lib/diff.js",
  118. "src/js/lib/moment.js",
  119. "src/js/lib/moment-timezone.js",
  120. "src/js/lib/prettify.js",
  121. "src/js/lib/vkbeautify.js",
  122. "src/js/lib/Sortable.js",
  123. "src/js/lib/bootstrap-colorpicker.js",
  124. "src/js/lib/xpath.js",
  125. // Custom libraries
  126. "src/js/lib/canvas_components.js",
  127. // Utility functions
  128. "src/js/core/Utils.js",
  129. // Operation objects
  130. "src/js/operations/*.js",
  131. // Core framework objects
  132. "src/js/core/*.js",
  133. "src/js/config/Categories.js",
  134. "src/js/config/OperationConfig.js",
  135. // HTML view objects
  136. "src/js/views/html/*.js",
  137. "!src/js/views/html/main.js",
  138. // Start the app!
  139. "src/js/views/html/main.js",
  140. ];
  141. var banner = '/**\n\
  142. * CyberChef - The Cyber Swiss Army Knife\n\
  143. *\n\
  144. * @copyright Crown Copyright 2016\n\
  145. * @license Apache-2.0\n\
  146. *\n\
  147. * Copyright 2016 Crown Copyright\n\
  148. *\n\
  149. * Licensed under the Apache License, Version 2.0 (the "License");\n\
  150. * you may not use this file except in compliance with the License.\n\
  151. * You may obtain a copy of the License at\n\
  152. *\n\
  153. * http://www.apache.org/licenses/LICENSE-2.0\n\
  154. *\n\
  155. * Unless required by applicable law or agreed to in writing, software\n\
  156. * distributed under the License is distributed on an "AS IS" BASIS,\n\
  157. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n\
  158. * See the License for the specific language governing permissions and\n\
  159. * limitations under the License.\n\
  160. */\n';
  161. var templateOptions = {
  162. data: {
  163. compileTime: grunt.template.today("dd/mm/yyyy HH:MM:ss") + " UTC",
  164. compileMsg: grunt.option("compile-msg") || grunt.option("msg") || "",
  165. codebaseStats: grunt.file.read("src/static/stats.txt").split("\n").join("<br>")
  166. }
  167. };
  168. // Project configuration
  169. grunt.initConfig({
  170. eslint: {
  171. options: {
  172. configFile: "src/js/.eslintrc.json"
  173. },
  174. gruntfile: ["Gruntfile.js"],
  175. core: ["src/js/core/**/*.js"],
  176. config: ["src/js/config/**/*.js"],
  177. views: ["src/js/views/**/*.js"],
  178. operations: ["src/js/operations/**/*.js"],
  179. },
  180. jsdoc: {
  181. options: {
  182. destination: "docs",
  183. template: "node_modules/ink-docstrap/template",
  184. recurse: true,
  185. readme: "./README.md",
  186. configure: "docs/jsdoc.conf.json"
  187. },
  188. all: {
  189. src: [
  190. "src/js/**/*.js",
  191. "!src/js/lib/**/*",
  192. ],
  193. }
  194. },
  195. clean: {
  196. dev: ["build/dev/*"],
  197. prod: ["build/prod/*"],
  198. docs: ["docs/*", "!docs/*.conf.json", "!docs/*.ico"],
  199. },
  200. concat: {
  201. options: {
  202. process: templateOptions
  203. },
  204. css: {
  205. options: {
  206. banner: banner.replace(/\/\*\*/g, "/*!"),
  207. process: function(content, srcpath) {
  208. // Change special comments from /** to /*! to comply with cssmin
  209. content = content.replace(/^\/\*\* /g, "/*! ");
  210. return grunt.template.process(content);
  211. }
  212. },
  213. src: [
  214. "src/css/lib/**/*.css",
  215. "src/css/structure/**/*.css",
  216. "src/css/themes/classic.css"
  217. ],
  218. dest: "build/dev/styles.css"
  219. },
  220. js: {
  221. options: {
  222. banner: '"use strict";\n'
  223. },
  224. src: jsFiles,
  225. dest: "build/dev/scripts.js"
  226. }
  227. },
  228. copy: {
  229. htmlDev: {
  230. options: {
  231. process: function(content, srcpath) {
  232. return grunt.template.process(content, templateOptions);
  233. }
  234. },
  235. src: "src/html/index.html",
  236. dest: "build/dev/index.html"
  237. },
  238. htmlProd: {
  239. options: {
  240. process: function(content, srcpath) {
  241. return grunt.template.process(content, templateOptions);
  242. }
  243. },
  244. src: "src/html/index.html",
  245. dest: "build/prod/index.html"
  246. },
  247. htmlInline: {
  248. options: {
  249. process: function(content, srcpath) {
  250. // TODO: Do all this in Jade
  251. content = content.replace(
  252. '<a href="cyberchef.htm" style="float: left; margin-left: 10px; margin-right: 80px;" download>Download CyberChef<img src="images/download-24x24.png" /></a>',
  253. '<span style="float: left; margin-left: 10px;">Compile time: ' + grunt.template.today("dd/mm/yyyy HH:MM:ss") + " UTC</span>");
  254. return grunt.template.process(content, templateOptions);
  255. }
  256. },
  257. src: "src/html/index.html",
  258. dest: "build/prod/cyberchef.htm"
  259. },
  260. staticDev: {
  261. files: [
  262. {
  263. expand: true,
  264. cwd: "src/static/",
  265. src: [
  266. "**/*",
  267. "**/.*",
  268. "!stats.txt",
  269. "!ga.html"
  270. ],
  271. dest: "build/dev/"
  272. }
  273. ]
  274. },
  275. staticProd: {
  276. files: [
  277. {
  278. expand: true,
  279. cwd: "src/static/",
  280. src: [
  281. "**/*",
  282. "**/.*",
  283. "!stats.txt",
  284. "!ga.html"
  285. ],
  286. dest: "build/prod/"
  287. }
  288. ]
  289. },
  290. ghPages: {
  291. options: {
  292. process: function(content, srcpath) {
  293. // Add Google Analytics code to index.html
  294. content = content.replace("</body></html>",
  295. grunt.file.read("src/static/ga.html") + "</body></html>");
  296. return grunt.template.process(content, templateOptions);
  297. }
  298. },
  299. src: "build/prod/index.html",
  300. dest: "build/prod/index.html"
  301. }
  302. },
  303. uglify: {
  304. options: {
  305. preserveComments: function(node, comment) {
  306. if (comment.value.indexOf("* @license") === 0) return true;
  307. return false;
  308. },
  309. screwIE8: true,
  310. ASCIIOnly: true,
  311. beautify: {
  312. beautify: false,
  313. inline_script: true, // eslint-disable-line camelcase
  314. ascii_only: true, // eslint-disable-line camelcase
  315. screw_ie8: true // eslint-disable-line camelcase
  316. },
  317. compress: {
  318. screw_ie8: true // eslint-disable-line camelcase
  319. },
  320. banner: banner
  321. },
  322. prod: {
  323. src: "build/dev/scripts.js",
  324. dest: "build/prod/scripts.js"
  325. }
  326. },
  327. cssmin: {
  328. prod: {
  329. src: "build/dev/styles.css",
  330. dest: "build/prod/styles.css"
  331. }
  332. },
  333. htmlmin: {
  334. prod: {
  335. options: {
  336. removeComments: true,
  337. collapseWhitespace: true,
  338. minifyJS: true,
  339. minifyCSS: true
  340. },
  341. src: "build/prod/index.html",
  342. dest: "build/prod/index.html"
  343. },
  344. inline: {
  345. options: {
  346. removeComments: true,
  347. collapseWhitespace: true,
  348. minifyJS: false,
  349. minifyCSS: false
  350. },
  351. src: "build/prod/cyberchef.htm",
  352. dest: "build/prod/cyberchef.htm"
  353. }
  354. },
  355. inline: {
  356. options: {
  357. tag: "",
  358. inlineTagAttributes: {
  359. js: "type='application/javascript'",
  360. css: "type='text/css'"
  361. }
  362. },
  363. compiled: {
  364. src: "build/prod/cyberchef.htm",
  365. dest: "build/prod/cyberchef.htm"
  366. },
  367. prod: {
  368. options: {
  369. tag: "__inline"
  370. },
  371. src: "build/prod/index.html",
  372. dest: "build/prod/index.html"
  373. }
  374. },
  375. chmod: {
  376. build: {
  377. options: {
  378. mode: "755",
  379. },
  380. src: ["build/**/*", "build/**/.htaccess", "build/"]
  381. },
  382. docs: {
  383. options: {
  384. mode: "755",
  385. },
  386. src: ["docs/**/*", "docs/"]
  387. }
  388. },
  389. exec: {
  390. repoSize: {
  391. command: [
  392. "git ls-files | wc -l | xargs printf '\n%b\ttracked files\n'",
  393. "du -hs | egrep -o '^[^\t]*' | xargs printf '%b\trepository size\n'"
  394. ].join(";"),
  395. stderr: false
  396. },
  397. stats: {
  398. command: "rm src/static/stats.txt;" +
  399. [
  400. "ls src/ -R1 | grep '^$' -v | grep ':$' -v | wc -l | xargs printf '%b\tsource files\n'",
  401. "find src/ -regex '.*\..*' -print | xargs cat | wc -l | xargs printf '%b\tlines\n'",
  402. "du -hs src/ | pcregrep -o '^[^\t]*' | xargs printf '%b\tsize\n'",
  403. "ls src/js/ -R1 | grep '\.js$' | wc -l | xargs printf '\n%b\tJavaScript source files\n'",
  404. "find src/js/ -regex '.*\.js' -print | xargs cat | wc -l | xargs printf '%b\tlines\n'",
  405. "find src/js/ -regex '.*\.js' -exec du -hcs {} \+ | tail -n1 | egrep -o '^[^\t]*' | xargs printf '%b\tsize\n'",
  406. "find src/js/ -regex '.*/lib/.*\.js' -print | wc -l | xargs printf '\n%b\tthird party JavaScript source files\n'",
  407. "find src/js/ -regex '.*/lib/.*\.js' -print | xargs cat | wc -l | xargs printf '%b\tlines\n'",
  408. "find src/js/ -regex '.*/lib/.*\.js' -exec du -hcs {} \+ | tail -n1 | egrep -o '^[^\t]*' | xargs printf '%b\tsize\n'",
  409. "find src/js/ -regex '.*\.js' -not -regex '.*/lib/.*' -print | wc -l | xargs printf '\n%b\tfirst party JavaScript source files\n'",
  410. "find src/js/ -regex '.*\.js' -not -regex '.*/lib/.*' -print | xargs cat | wc -l | xargs printf '%b\tlines\n'",
  411. "find src/js/ -regex '.*\.js' -not -regex '.*/lib/.*' -exec du -hcs {} \+ | tail -n1 | egrep -o '^[^\t]*' | xargs printf '%b\tsize\n'",
  412. "du build/dev/scripts.js -h | egrep -o '^[^\t]*' | xargs printf '\n%b\tuncompressed JavaScript size\n'",
  413. "du build/prod/scripts.js -h | egrep -o '^[^\t]*' | xargs printf '%b\tcompressed JavaScript size\n'",
  414. "grep -E '^\\s+name: ' src/js/config/Categories.js | wc -l | xargs printf '\n%b\tcategories\n'",
  415. "grep -E '^\\s+\"[A-Za-z0-9 \\-]+\": {' src/js/config/OperationConfig.js | wc -l | xargs printf '%b\toperations\n'",
  416. ].join(" >> src/static/stats.txt;") + " >> src/static/stats.txt;",
  417. stderr: false
  418. },
  419. displayStats: {
  420. command: "cat src/static/stats.txt"
  421. },
  422. cleanGit: {
  423. command: "git gc --prune=now --aggressive"
  424. },
  425. deployGhPages: {
  426. command: [
  427. "git add build/prod/index.html -v",
  428. "COMMIT_HASH=$(git rev-parse HEAD)",
  429. "git commit -m \"GitHub Pages release for ${COMMIT_HASH}\"",
  430. "git push origin `git subtree split --prefix build/prod master`:gh-pages --force",
  431. "git reset HEAD~",
  432. "git checkout build/prod/index.html"
  433. ].join(";")
  434. }
  435. },
  436. watch: {
  437. css: {
  438. files: "src/css/**/*.css",
  439. tasks: ["concat:css", "chmod:build"]
  440. },
  441. js: {
  442. files: "src/js/**/*.js",
  443. tasks: ["concat:js", "chmod:build"]
  444. },
  445. html: {
  446. files: "src/html/**/*.html",
  447. tasks: ["copy:htmlDev", "chmod:build"]
  448. },
  449. static: {
  450. files: ["src/static/**/*", "src/static/**/.*"],
  451. tasks: ["copy:staticDev", "chmod:build"]
  452. },
  453. grunt: {
  454. files: "Gruntfile.js",
  455. tasks: ["clean:dev", "concat:css", "concat:js", "copy:htmlDev", "copy:staticDev", "chmod:build"]
  456. }
  457. },
  458. });
  459. };