Gruntfile.js 18 KB

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