Gruntfile.coffee 14 KB

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