Gruntfile.coffee 14 KB

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