Gruntfile.coffee 14 KB

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