Gruntfile.coffee 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. fs = require "fs"
  2. PackageVersions = require "./app/coffee/infrastructure/PackageVersions"
  3. Settings = require "settings-sharelatex"
  4. require('es6-promise').polyfill()
  5. module.exports = (grunt) ->
  6. grunt.loadNpmTasks 'grunt-contrib-coffee'
  7. grunt.loadNpmTasks 'grunt-contrib-less'
  8. grunt.loadNpmTasks 'grunt-contrib-clean'
  9. grunt.loadNpmTasks 'grunt-mocha-test'
  10. grunt.loadNpmTasks 'grunt-available-tasks'
  11. grunt.loadNpmTasks 'grunt-contrib-requirejs'
  12. grunt.loadNpmTasks 'grunt-bunyan'
  13. grunt.loadNpmTasks 'grunt-sed'
  14. grunt.loadNpmTasks 'grunt-git-rev-parse'
  15. grunt.loadNpmTasks 'grunt-file-append'
  16. grunt.loadNpmTasks 'grunt-file-append'
  17. grunt.loadNpmTasks 'grunt-env'
  18. grunt.loadNpmTasks 'grunt-newer'
  19. grunt.loadNpmTasks 'grunt-contrib-watch'
  20. grunt.loadNpmTasks 'grunt-parallel'
  21. grunt.loadNpmTasks 'grunt-exec'
  22. grunt.loadNpmTasks 'grunt-postcss'
  23. grunt.loadNpmTasks 'grunt-forever'
  24. grunt.loadNpmTasks 'grunt-shell'
  25. # grunt.loadNpmTasks 'grunt-contrib-imagemin'
  26. # grunt.loadNpmTasks 'grunt-sprity'
  27. config =
  28. exec:
  29. run:
  30. command:"node app.js | ./node_modules/logger-sharelatex/node_modules/bunyan/bin/bunyan --color"
  31. cssmin_sl:
  32. command:"node_modules/clean-css/bin/cleancss --s0 --source-map -o public/stylesheets/style.css public/stylesheets/style.css"
  33. cssmin_ol:
  34. command:"node_modules/clean-css/bin/cleancss --s0 --source-map -o public/stylesheets/ol-style.css public/stylesheets/ol-style.css"
  35. cssmin_ol_light:
  36. command:"node_modules/clean-css/bin/cleancss --s0 --source-map -o public/stylesheets/ol-light-style.css public/stylesheets/ol-light-style.css"
  37. forever:
  38. app:
  39. options:
  40. index: "app.js"
  41. logFile: "app.log"
  42. watch:
  43. coffee:
  44. files: 'public/**/*.coffee'
  45. tasks: ['quickcompile:coffee']
  46. options: {}
  47. less:
  48. files: '**/*.less'
  49. tasks: ['compile:css']
  50. options: {}
  51. parallel:
  52. run:
  53. tasks:['exec', 'watch']
  54. options:
  55. grunt:true
  56. stream:true
  57. # imagemin:
  58. # dynamic:
  59. # files: [{
  60. # expand: true
  61. # cwd: 'public/img/'
  62. # src: ['**/*.{png,jpg,gif}']
  63. # dest: 'public/img/'
  64. # }]
  65. # options:
  66. # interlaced:false
  67. # optimizationLevel: 7
  68. # sprity:
  69. # sprite:
  70. # options:
  71. # cssPath:"/img/"
  72. # 'style': '../../public/stylesheets/app/sprites.less'
  73. # margin: 0
  74. # src: ['./public/img/flags/24/*.png']
  75. # dest: './public/img/sprite'
  76. coffee:
  77. app_dir:
  78. expand: true,
  79. flatten: false,
  80. cwd: 'app/coffee',
  81. src: ['**/*.coffee'],
  82. dest: 'app/js/',
  83. ext: '.js'
  84. app:
  85. src: 'app.coffee'
  86. dest: 'app.js'
  87. sharejs:
  88. options:
  89. join: true
  90. files:
  91. "public/js/libs/sharejs.js": [
  92. "public/coffee/ide/editor/sharejs/header.coffee"
  93. "public/coffee/ide/editor/sharejs/vendor/types/helpers.coffee"
  94. "public/coffee/ide/editor/sharejs/vendor/types/text.coffee"
  95. "public/coffee/ide/editor/sharejs/vendor/types/text-api.coffee"
  96. "public/coffee/ide/editor/sharejs/vendor/client/microevent.coffee"
  97. "public/coffee/ide/editor/sharejs/vendor/client/doc.coffee"
  98. "public/coffee/ide/editor/sharejs/vendor/client/ace.coffee"
  99. "public/coffee/ide/editor/sharejs/vendor/client/cm.coffee"
  100. ]
  101. client:
  102. expand: true,
  103. flatten: false,
  104. cwd: 'public/coffee',
  105. src: ['**/*.coffee'],
  106. dest: 'public/js/',
  107. ext: '.js',
  108. options:
  109. sourceMap: true
  110. smoke_tests:
  111. expand: true,
  112. flatten: false,
  113. cwd: 'test/smoke/coffee',
  114. src: ['**/*.coffee'],
  115. dest: 'test/smoke/js/',
  116. ext: '.js'
  117. unit_tests:
  118. expand: true,
  119. flatten: false,
  120. cwd: 'test/unit/coffee',
  121. src: ['**/*.coffee'],
  122. dest: 'test/unit/js/',
  123. ext: '.js'
  124. acceptance_tests:
  125. expand: true,
  126. flatten: false,
  127. cwd: 'test/acceptance/coffee',
  128. src: ['**/*.coffee'],
  129. dest: 'test/acceptance/js/',
  130. ext: '.js'
  131. less:
  132. app:
  133. options:
  134. sourceMap: true
  135. sourceMapFilename: "public/stylesheets/style.css.map"
  136. sourceMapBasepath: "public/stylesheets"
  137. globalVars:
  138. 'is-overleaf': false
  139. 'show-rich-text': Settings.showRichText
  140. files:
  141. "public/stylesheets/style.css": "public/stylesheets/style.less"
  142. ol:
  143. options:
  144. sourceMap: true
  145. sourceMapFilename: "public/stylesheets/ol-style.css.map"
  146. sourceMapBasepath: "public/stylesheets"
  147. globalVars:
  148. 'is-overleaf': true
  149. 'show-rich-text': Settings.showRichText
  150. files:
  151. "public/stylesheets/ol-style.css": "public/stylesheets/ol-style.less"
  152. 'ol-light':
  153. options:
  154. sourceMap: true
  155. sourceMapFilename: "public/stylesheets/ol-light-style.css.map"
  156. sourceMapBasepath: "public/stylesheets"
  157. globalVars:
  158. 'is-overleaf': true
  159. 'show-rich-text': Settings.showRichText
  160. files:
  161. "public/stylesheets/ol-light-style.css": "public/stylesheets/ol-light-style.less"
  162. postcss:
  163. options:
  164. map:
  165. prev: "public/stylesheets/"
  166. inline: false
  167. sourcesContent: true
  168. processors: [
  169. require('autoprefixer')({browsers: [ 'last 2 versions', 'ie >= 10' ]})
  170. ]
  171. dist:
  172. src: [ "public/stylesheets/style.css", "public/stylesheets/ol-style.css", "public/stylesheets/ol-light-style.css" ]
  173. env:
  174. run:
  175. add:
  176. NODE_TLS_REJECT_UNAUTHORIZED:0
  177. requirejs:
  178. compile:
  179. options:
  180. optimize:"uglify2"
  181. uglify2:
  182. mangle: false
  183. appDir: "public/js"
  184. baseUrl: "./"
  185. dir: "public/minjs"
  186. inlineText: false
  187. generateSourceMaps: true
  188. preserveLicenseComments: false
  189. paths:
  190. "moment": "libs/#{PackageVersions.lib('moment')}"
  191. "mathjax": "/js/libs/mathjax/MathJax.js?config=TeX-AMS_HTML"
  192. "pdfjs-dist/build/pdf": "libs/#{PackageVersions.lib('pdfjs')}/pdf"
  193. "ace": "#{PackageVersions.lib('ace')}"
  194. "fineuploader": "libs/#{PackageVersions.lib('fineuploader')}"
  195. shim:
  196. "pdfjs-dist/build/pdf":
  197. deps: ["libs/#{PackageVersions.lib('pdfjs')}/compatibility"]
  198. skipDirOptimize: true
  199. modules: [
  200. {
  201. name: "main",
  202. exclude: ["libraries"]
  203. }, {
  204. name: "ide",
  205. exclude: ["pdfjs-dist/build/pdf", "libraries"]
  206. },{
  207. name: "libraries"
  208. },{
  209. name: "ace/mode-latex"
  210. },{
  211. name: "ace/worker-latex"
  212. }
  213. ]
  214. clean:
  215. app: ["app/js"]
  216. unit_tests: ["test/unit/js"]
  217. acceptance_tests: ["test/acceptance/js"]
  218. mochaTest:
  219. unit:
  220. src: ["test/unit/js/#{grunt.option('feature') or '**'}/*.js"]
  221. options:
  222. reporter: grunt.option('reporter') or 'spec'
  223. grep: grunt.option("grep")
  224. smoke:
  225. src: ['test/smoke/js/**/*.js']
  226. options:
  227. reporter: grunt.option('reporter') or 'spec'
  228. grep: grunt.option("grep")
  229. acceptance:
  230. src: ["test/acceptance/js/#{grunt.option('feature') or '**'}/*.js"]
  231. options:
  232. timeout: 40000
  233. reporter: grunt.option('reporter') or 'spec'
  234. grep: grunt.option("grep")
  235. "git-rev-parse":
  236. version:
  237. options:
  238. prop: 'commit'
  239. file_append:
  240. default_options: files: [ {
  241. append: '\n//ide.js is complete - used for automated testing'
  242. input: 'public/minjs/ide.js'
  243. output: 'public/minjs/ide.js'
  244. }]
  245. sed:
  246. version:
  247. path: "app/views/sentry.pug"
  248. pattern: '@@COMMIT@@',
  249. replacement: '<%= commit %>',
  250. release:
  251. path: "app/views/sentry.pug"
  252. pattern: "@@RELEASE@@"
  253. replacement: process.env.BUILD_NUMBER || "(unknown build)"
  254. shell:
  255. fullAcceptanceTests:
  256. command: "bash ./test/acceptance/scripts/full-test.sh"
  257. dockerTests:
  258. command: 'docker run -v "$(pwd):/app" --env SHARELATEX_ALLOW_PUBLIC_ACCESS=true --rm sharelatex/acceptance-test-runner'
  259. availabletasks:
  260. tasks:
  261. options:
  262. filter: 'exclude',
  263. tasks: [
  264. 'coffee'
  265. 'less'
  266. 'clean'
  267. 'mochaTest'
  268. 'availabletasks'
  269. 'wrap_sharejs'
  270. 'requirejs'
  271. 'execute'
  272. 'bunyan'
  273. ]
  274. groups:
  275. "Compile tasks": [
  276. "compile:server"
  277. "compile:client"
  278. "compile:tests"
  279. "compile"
  280. "compile:unit_tests"
  281. "compile:smoke_tests"
  282. "compile:css"
  283. "compile:minify"
  284. "install"
  285. ]
  286. "Test tasks": [
  287. "test:unit"
  288. "test:acceptance"
  289. ]
  290. "Run tasks": [
  291. "run"
  292. "default"
  293. ]
  294. "Misc": [
  295. "help"
  296. ]
  297. moduleCompileServerTasks = []
  298. moduleCompileUnitTestTasks = []
  299. moduleUnitTestTasks = []
  300. moduleCompileClientTasks = []
  301. moduleIdeClientSideIncludes = []
  302. moduleMainClientSideIncludes = []
  303. if fs.existsSync "./modules"
  304. for module in fs.readdirSync "./modules"
  305. if fs.existsSync "./modules/#{module}/index.coffee"
  306. config.coffee["module_#{module}_server"] = {
  307. expand: true,
  308. flatten: false,
  309. cwd: "modules/#{module}/app/coffee",
  310. src: ['**/*.coffee'],
  311. dest: "modules/#{module}/app/js",
  312. ext: '.js'
  313. }
  314. config.coffee["module_#{module}_index"] = {
  315. src: "modules/#{module}/index.coffee",
  316. dest: "modules/#{module}/index.js"
  317. }
  318. moduleCompileServerTasks.push "coffee:module_#{module}_server"
  319. moduleCompileServerTasks.push "coffee:module_#{module}_index"
  320. config.coffee["module_#{module}_unit_tests"] = {
  321. expand: true,
  322. flatten: false,
  323. cwd: "modules/#{module}/test/unit/coffee",
  324. src: ['**/*.coffee'],
  325. dest: "modules/#{module}/test/unit/js",
  326. ext: '.js'
  327. }
  328. config.mochaTest["module_#{module}_unit"] = {
  329. src: ["modules/#{module}/test/unit/js/**/*.js"]
  330. options:
  331. reporter: grunt.option('reporter') or 'spec'
  332. grep: grunt.option("grep")
  333. }
  334. moduleCompileUnitTestTasks.push "coffee:module_#{module}_unit_tests"
  335. moduleUnitTestTasks.push "mochaTest:module_#{module}_unit"
  336. if fs.existsSync "./modules/#{module}/public/coffee/ide/index.coffee"
  337. config.coffee["module_#{module}_client_ide"] = {
  338. expand: true,
  339. flatten: false,
  340. cwd: "modules/#{module}/public/coffee/ide",
  341. src: ['**/*.coffee'],
  342. dest: "public/js/ide/#{module}",
  343. ext: '.js'
  344. }
  345. moduleCompileClientTasks.push "coffee:module_#{module}_client_ide"
  346. moduleIdeClientSideIncludes.push "ide/#{module}/index"
  347. if fs.existsSync "./modules/#{module}/public/coffee/main/index.coffee"
  348. config.coffee["module_#{module}_client_main"] = {
  349. expand: true,
  350. flatten: false,
  351. cwd: "modules/#{module}/public/coffee/main",
  352. src: ['**/*.coffee'],
  353. dest: "public/js/main/#{module}",
  354. ext: '.js'
  355. }
  356. moduleCompileClientTasks.push "coffee:module_#{module}_client_main"
  357. moduleMainClientSideIncludes.push "main/#{module}/index"
  358. grunt.initConfig config
  359. grunt.registerTask 'wrap_sharejs', 'Wrap the compiled ShareJS code for AMD module loading', () ->
  360. content = fs.readFileSync "public/js/libs/sharejs.js"
  361. fs.writeFileSync "public/js/libs/sharejs.js", """
  362. define(["ace/ace"], function() {
  363. #{content}
  364. return window.sharejs;
  365. });
  366. """
  367. grunt.registerTask 'help', 'Display this help list', 'availabletasks'
  368. grunt.registerTask 'compile:modules:server', 'Compile all the modules', moduleCompileServerTasks
  369. grunt.registerTask 'compile:modules:unit_tests', 'Compile all the modules unit tests', moduleCompileUnitTestTasks
  370. grunt.registerTask 'compile:modules:client', 'Compile all the module client side code', moduleCompileClientTasks
  371. grunt.registerTask 'compile:modules:inject_clientside_includes', () ->
  372. content = fs.readFileSync("public/js/ide.js").toString()
  373. content = content.replace(/, "__IDE_CLIENTSIDE_INCLUDES__"/g, moduleIdeClientSideIncludes.map((i) -> ", \"#{i}\"").join(""))
  374. fs.writeFileSync "public/js/ide.js", content
  375. content = fs.readFileSync("public/js/main.js").toString()
  376. content = content.replace(/, "__MAIN_CLIENTSIDE_INCLUDES__"/g, moduleMainClientSideIncludes.map((i) -> ", \"#{i}\"").join(""))
  377. fs.writeFileSync "public/js/main.js", content
  378. grunt.registerTask 'compile:server', 'Compile the server side coffee script', ['clean:app', 'coffee:app', 'coffee:app_dir', 'compile:modules:server']
  379. grunt.registerTask 'compile:client', 'Compile the client side coffee script', ['coffee:client', 'coffee:sharejs', 'wrap_sharejs', "compile:modules:client", 'compile:modules:inject_clientside_includes']
  380. grunt.registerTask 'compile:css', 'Compile the less files to css', ['less', 'postcss:dist']
  381. grunt.registerTask 'compile:minify', 'Concat and minify the client side js and css', ['requirejs', "file_append", "exec:cssmin_sl", "exec:cssmin_ol", "exec:cssmin_ol_light"]
  382. grunt.registerTask 'compile:unit_tests', 'Compile the unit tests', ['clean:unit_tests', 'coffee:unit_tests']
  383. grunt.registerTask 'compile:acceptance_tests', 'Compile the acceptance tests', ['clean:acceptance_tests', 'coffee:acceptance_tests']
  384. grunt.registerTask 'compile:smoke_tests', 'Compile the smoke tests', ['coffee:smoke_tests']
  385. grunt.registerTask 'compile:tests', 'Compile all the tests', ['compile:smoke_tests', 'compile:unit_tests', 'compile:acceptance_tests']
  386. grunt.registerTask 'compile', 'Compiles everything need to run web-sharelatex', ['compile:server', 'compile:client', 'compile:css']
  387. grunt.registerTask 'quickcompile:coffee', 'Compiles only changed coffee files',['newer:coffee']
  388. grunt.registerTask 'install', "Compile everything when installing as an npm module", ['compile']
  389. grunt.registerTask 'test:unit', 'Run the unit tests (use --grep=<regex> or --feature=<feature> for individual tests)', ['compile:server', 'compile:modules:server', 'compile:unit_tests', 'compile:modules:unit_tests', 'mochaTest:unit'].concat(moduleUnitTestTasks)
  390. grunt.registerTask 'test:acceptance', 'Run the acceptance tests (use --grep=<regex> or --feature=<feature> for individual tests)', ['compile:acceptance_tests', 'mochaTest:acceptance']
  391. grunt.registerTask 'test:smoke', 'Run the smoke tests', ['compile:smoke_tests', 'mochaTest:smoke']
  392. grunt.registerTask(
  393. 'test:acceptance:full',
  394. "Start server and run acceptance tests",
  395. ['shell:fullAcceptanceTests']
  396. )
  397. grunt.registerTask(
  398. 'test:acceptance:docker',
  399. "Run acceptance tests inside docker container",
  400. ['compile:acceptance_tests', 'shell:dockerTests']
  401. )
  402. grunt.registerTask 'test:modules:unit', 'Run the unit tests for the modules', ['compile:modules:server', 'compile:modules:unit_tests'].concat(moduleUnitTestTasks)
  403. grunt.registerTask 'run:watch', "Compile and run the web-sharelatex server", ['compile', 'env:run', 'parallel']
  404. grunt.registerTask 'run', "Compile and run the web-sharelatex server", ['compile', 'env:run', 'exec']
  405. grunt.registerTask 'default', 'run'
  406. grunt.registerTask 'version', "Write the version number into sentry.pug", ['git-rev-parse', 'sed']