Gruntfile.coffee 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. fs = require "fs"
  2. module.exports = (grunt) ->
  3. grunt.loadNpmTasks 'grunt-contrib-coffee'
  4. grunt.loadNpmTasks 'grunt-contrib-less'
  5. grunt.loadNpmTasks 'grunt-contrib-clean'
  6. grunt.loadNpmTasks 'grunt-mocha-test'
  7. grunt.loadNpmTasks 'grunt-available-tasks'
  8. grunt.loadNpmTasks 'grunt-contrib-requirejs'
  9. grunt.loadNpmTasks 'grunt-bunyan'
  10. grunt.loadNpmTasks 'grunt-sed'
  11. grunt.loadNpmTasks 'grunt-git-rev-parse'
  12. grunt.loadNpmTasks 'grunt-file-append'
  13. grunt.loadNpmTasks 'grunt-file-append'
  14. grunt.loadNpmTasks 'grunt-env'
  15. grunt.loadNpmTasks 'grunt-newer'
  16. grunt.loadNpmTasks 'grunt-contrib-watch'
  17. grunt.loadNpmTasks 'grunt-parallel'
  18. grunt.loadNpmTasks 'grunt-exec'
  19. grunt.loadNpmTasks 'grunt-contrib-imagemin'
  20. grunt.loadNpmTasks 'grunt-contrib-cssmin'
  21. config =
  22. exec:
  23. run:
  24. command:"node app.js | ./node_modules/logger-sharelatex/node_modules/bunyan/bin/bunyan --color"
  25. watch:
  26. coffee:
  27. files: 'public/**/*.coffee'
  28. tasks: ['quickcompile:coffee']
  29. options: {}
  30. less:
  31. files: '**/*.less'
  32. tasks: ['compile:css']
  33. options: {}
  34. parallel:
  35. run:
  36. tasks:['exec', 'watch']
  37. options:
  38. grunt:true
  39. stream:true
  40. imagemin:
  41. dynamic:
  42. files: [{
  43. expand: true
  44. cwd: 'public/img/'
  45. src: ['**/*.{png,jpg,gif}']
  46. dest: 'public/img/'
  47. }]
  48. options:
  49. interlaced:false
  50. optimizationLevel: 7
  51. coffee:
  52. app_dir:
  53. expand: true,
  54. flatten: false,
  55. cwd: 'app/coffee',
  56. src: ['**/*.coffee'],
  57. dest: 'app/js/',
  58. ext: '.js'
  59. app:
  60. src: 'app.coffee'
  61. dest: 'app.js'
  62. sharejs:
  63. options:
  64. join: true
  65. files:
  66. "public/js/libs/sharejs.js": [
  67. "public/coffee/ide/editor/sharejs/header.coffee"
  68. "public/coffee/ide/editor/sharejs/vendor/types/helpers.coffee"
  69. "public/coffee/ide/editor/sharejs/vendor/types/text.coffee"
  70. "public/coffee/ide/editor/sharejs/vendor/types/text-api.coffee"
  71. "public/coffee/ide/editor/sharejs/vendor/client/microevent.coffee"
  72. "public/coffee/ide/editor/sharejs/vendor/client/doc.coffee"
  73. "public/coffee/ide/editor/sharejs/vendor/client/ace.coffee"
  74. ]
  75. client:
  76. expand: true,
  77. flatten: false,
  78. cwd: 'public/coffee',
  79. src: ['**/*.coffee'],
  80. dest: 'public/js/',
  81. ext: '.js'
  82. smoke_tests:
  83. expand: true,
  84. flatten: false,
  85. cwd: 'test/smoke/coffee',
  86. src: ['**/*.coffee'],
  87. dest: 'test/smoke/js/',
  88. ext: '.js'
  89. unit_tests:
  90. expand: true,
  91. flatten: false,
  92. cwd: 'test/UnitTests/coffee',
  93. src: ['**/*.coffee'],
  94. dest: 'test/UnitTests/js/',
  95. ext: '.js'
  96. acceptance_tests:
  97. expand: true,
  98. flatten: false,
  99. cwd: 'test/acceptance/coffee',
  100. src: ['**/*.coffee'],
  101. dest: 'test/acceptance/js/',
  102. ext: '.js'
  103. less:
  104. app:
  105. files:
  106. "public/stylesheets/style.css": "public/stylesheets/style.less"
  107. cssmin:
  108. target:
  109. files:
  110. "public/stylesheets/style.css": "public/stylesheets/style.css"
  111. env:
  112. run:
  113. add:
  114. NODE_TLS_REJECT_UNAUTHORIZED:0
  115. requirejs:
  116. compile:
  117. options:
  118. optimize:"uglify2"
  119. uglify2:
  120. mangle: false
  121. appDir: "public/js"
  122. baseUrl: "./"
  123. dir: "public/minjs"
  124. inlineText: false
  125. preserveLicenseComments: false
  126. paths:
  127. "moment": "libs/moment-2.9.0"
  128. "mathjax": "/js/libs/mathjax/MathJax.js?config=TeX-AMS_HTML"
  129. "libs/pdf": "libs/pdfjs-1.3.91/pdf"
  130. shim:
  131. "libs/pdf":
  132. deps: ["libs/pdfjs-1.3.91/compatibility"]
  133. skipDirOptimize: true
  134. modules: [
  135. {
  136. name: "main",
  137. exclude: ["libs"]
  138. }, {
  139. name: "ide",
  140. exclude: ["libs", "libs/pdf"]
  141. }, {
  142. name: "libs"
  143. },{
  144. name: "ace/mode-latex"
  145. }
  146. ]
  147. clean:
  148. app: ["app/js"]
  149. unit_tests: ["test/UnitTests/js"]
  150. acceptance_tests: ["test/acceptance/js"]
  151. mochaTest:
  152. unit:
  153. src: ["test/UnitTests/js/#{grunt.option('feature') or '**'}/*.js"]
  154. options:
  155. reporter: grunt.option('reporter') or 'spec'
  156. grep: grunt.option("grep")
  157. smoke:
  158. src: ['test/smoke/js/**/*.js']
  159. options:
  160. reporter: grunt.option('reporter') or 'spec'
  161. grep: grunt.option("grep")
  162. acceptance:
  163. src: ["test/acceptance/js/#{grunt.option('feature') or '**'}/*.js"]
  164. options:
  165. timeout: 10000
  166. reporter: grunt.option('reporter') or 'spec'
  167. grep: grunt.option("grep")
  168. "git-rev-parse":
  169. version:
  170. options:
  171. prop: 'commit'
  172. file_append:
  173. default_options: files: [ {
  174. append: '\n//ide.js is complete - used for automated testing'
  175. input: 'public/minjs/ide.js'
  176. output: 'public/minjs/ide.js'
  177. }]
  178. sed:
  179. version:
  180. path: "app/views/sentry.jade"
  181. pattern: '@@COMMIT@@',
  182. replacement: '<%= commit %>',
  183. release:
  184. path: "app/views/sentry.jade"
  185. pattern: "@@RELEASE@@"
  186. replacement: process.env.BUILD_NUMBER || "(unknown build)"
  187. availabletasks:
  188. tasks:
  189. options:
  190. filter: 'exclude',
  191. tasks: [
  192. 'coffee'
  193. 'less'
  194. 'clean'
  195. 'mochaTest'
  196. 'availabletasks'
  197. 'wrap_sharejs'
  198. 'requirejs'
  199. 'execute'
  200. 'bunyan'
  201. ]
  202. groups:
  203. "Compile tasks": [
  204. "compile:server"
  205. "compile:client"
  206. "compile:tests"
  207. "compile"
  208. "compile:unit_tests"
  209. "compile:smoke_tests"
  210. "compile:css"
  211. "compile:minify"
  212. "install"
  213. ]
  214. "Test tasks": [
  215. "test:unit"
  216. "test:acceptance"
  217. ]
  218. "Run tasks": [
  219. "run"
  220. "default"
  221. ]
  222. "Misc": [
  223. "help"
  224. ]
  225. moduleCompileServerTasks = []
  226. moduleCompileUnitTestTasks = []
  227. moduleUnitTestTasks = []
  228. moduleCompileClientTasks = []
  229. moduleIdeClientSideIncludes = []
  230. moduleMainClientSideIncludes = []
  231. if fs.existsSync "./modules"
  232. for module in fs.readdirSync "./modules"
  233. if fs.existsSync "./modules/#{module}/index.coffee"
  234. config.coffee["module_#{module}_server"] = {
  235. expand: true,
  236. flatten: false,
  237. cwd: "modules/#{module}/app/coffee",
  238. src: ['**/*.coffee'],
  239. dest: "modules/#{module}/app/js",
  240. ext: '.js'
  241. }
  242. config.coffee["module_#{module}_index"] = {
  243. src: "modules/#{module}/index.coffee",
  244. dest: "modules/#{module}/index.js"
  245. }
  246. moduleCompileServerTasks.push "coffee:module_#{module}_server"
  247. moduleCompileServerTasks.push "coffee:module_#{module}_index"
  248. config.coffee["module_#{module}_unit_tests"] = {
  249. expand: true,
  250. flatten: false,
  251. cwd: "modules/#{module}/test/unit/coffee",
  252. src: ['**/*.coffee'],
  253. dest: "modules/#{module}/test/unit/js",
  254. ext: '.js'
  255. }
  256. config.mochaTest["module_#{module}_unit"] = {
  257. src: ["modules/#{module}/test/unit/js/*.js"]
  258. options:
  259. reporter: grunt.option('reporter') or 'spec'
  260. grep: grunt.option("grep")
  261. }
  262. moduleCompileUnitTestTasks.push "coffee:module_#{module}_unit_tests"
  263. moduleUnitTestTasks.push "mochaTest:module_#{module}_unit"
  264. if fs.existsSync "./modules/#{module}/public/coffee/ide/index.coffee"
  265. config.coffee["module_#{module}_client_ide"] = {
  266. expand: true,
  267. flatten: false,
  268. cwd: "modules/#{module}/public/coffee/ide",
  269. src: ['**/*.coffee'],
  270. dest: "public/js/ide/#{module}",
  271. ext: '.js'
  272. }
  273. moduleCompileClientTasks.push "coffee:module_#{module}_client_ide"
  274. moduleIdeClientSideIncludes.push "ide/#{module}/index"
  275. if fs.existsSync "./modules/#{module}/public/coffee/main/index.coffee"
  276. config.coffee["module_#{module}_client_main"] = {
  277. expand: true,
  278. flatten: false,
  279. cwd: "modules/#{module}/public/coffee/main",
  280. src: ['**/*.coffee'],
  281. dest: "public/js/main/#{module}",
  282. ext: '.js'
  283. }
  284. moduleCompileClientTasks.push "coffee:module_#{module}_client_main"
  285. moduleMainClientSideIncludes.push "main/#{module}/index"
  286. grunt.initConfig config
  287. grunt.registerTask 'wrap_sharejs', 'Wrap the compiled ShareJS code for AMD module loading', () ->
  288. content = fs.readFileSync "public/js/libs/sharejs.js"
  289. fs.writeFileSync "public/js/libs/sharejs.js", """
  290. define(["ace/ace"], function() {
  291. #{content}
  292. return window.sharejs;
  293. });
  294. """
  295. grunt.registerTask 'help', 'Display this help list', 'availabletasks'
  296. grunt.registerTask 'compile:modules:server', 'Compile all the modules', moduleCompileServerTasks
  297. grunt.registerTask 'compile:modules:unit_tests', 'Compile all the modules unit tests', moduleCompileUnitTestTasks
  298. grunt.registerTask 'compile:modules:client', 'Compile all the module client side code', moduleCompileClientTasks
  299. grunt.registerTask 'compile:modules:inject_clientside_includes', () ->
  300. content = fs.readFileSync("public/js/ide.js").toString()
  301. content = content.replace(/, "__IDE_CLIENTSIDE_INCLUDES__"/g, moduleIdeClientSideIncludes.map((i) -> ", \"#{i}\"").join(""))
  302. fs.writeFileSync "public/js/ide.js", content
  303. content = fs.readFileSync("public/js/main.js").toString()
  304. content = content.replace(/, "__MAIN_CLIENTSIDE_INCLUDES__"/g, moduleMainClientSideIncludes.map((i) -> ", \"#{i}\"").join(""))
  305. fs.writeFileSync "public/js/main.js", content
  306. grunt.registerTask 'compile:server', 'Compile the server side coffee script', ['clean:app', 'coffee:app', 'coffee:app_dir', 'compile:modules:server']
  307. grunt.registerTask 'compile:client', 'Compile the client side coffee script', ['coffee:client', 'coffee:sharejs', 'wrap_sharejs', "compile:modules:client", 'compile:modules:inject_clientside_includes']
  308. grunt.registerTask 'compile:css', 'Compile the less files to css', ['less']
  309. grunt.registerTask 'compile:minify', 'Concat and minify the client side js', ['requirejs', "file_append", "cssmin"]
  310. grunt.registerTask 'compile:unit_tests', 'Compile the unit tests', ['clean:unit_tests', 'coffee:unit_tests']
  311. grunt.registerTask 'compile:acceptance_tests', 'Compile the acceptance tests', ['clean:acceptance_tests', 'coffee:acceptance_tests']
  312. grunt.registerTask 'compile:smoke_tests', 'Compile the smoke tests', ['coffee:smoke_tests']
  313. grunt.registerTask 'compile:tests', 'Compile all the tests', ['compile:smoke_tests', 'compile:unit_tests']
  314. grunt.registerTask 'compile', 'Compiles everything need to run web-sharelatex', ['compile:server', 'compile:client', 'compile:css']
  315. grunt.registerTask 'quickcompile:coffee', 'Compiles only changed coffee files',['newer:coffee']
  316. grunt.registerTask 'install', "Compile everything when installing as an npm module", ['compile']
  317. 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)
  318. grunt.registerTask 'test:acceptance', 'Run the acceptance tests (use --grep=<regex> or --feature=<feature> for individual tests)', ['compile:acceptance_tests', 'mochaTest:acceptance']
  319. grunt.registerTask 'test:smoke', 'Run the smoke tests', ['compile:smoke_tests', 'mochaTest:smoke']
  320. grunt.registerTask 'test:modules:unit', 'Run the unit tests for the modules', ['compile:modules:server', 'compile:modules:unit_tests'].concat(moduleUnitTestTasks)
  321. grunt.registerTask 'run', "Compile and run the web-sharelatex server", ['compile', 'env:run', 'parallel']
  322. grunt.registerTask 'default', 'run'
  323. grunt.registerTask 'version', "Write the version number into sentry.jade", ['git-rev-parse', 'sed']
  324. grunt.registerTask 'create-admin-user', "Create a user with the given email address and make them an admin. Update in place if the user already exists", () ->
  325. done = @async()
  326. email = grunt.option("email")
  327. if !email?
  328. console.error "Usage: grunt create-admin-user --email joe@example.com"
  329. process.exit(1)
  330. settings = require "settings-sharelatex"
  331. UserRegistrationHandler = require "./app/js/Features/User/UserRegistrationHandler"
  332. OneTimeTokenHandler = require "./app/js/Features/Security/OneTimeTokenHandler"
  333. UserRegistrationHandler.registerNewUser {
  334. email: email
  335. password: require("crypto").randomBytes(32).toString("hex")
  336. }, (error, user) ->
  337. if error? and error?.message != "EmailAlreadyRegistered"
  338. throw error
  339. user.isAdmin = true
  340. user.save (error) ->
  341. throw error if error?
  342. ONE_WEEK = 7 * 24 * 60 * 60 # seconds
  343. OneTimeTokenHandler.getNewToken user._id, { expiresIn: ONE_WEEK }, (err, token)->
  344. return next(err) if err?
  345. console.log ""
  346. console.log """
  347. Successfully created #{email} as an admin user.
  348. Please visit the following URL to set a password for #{email} and log in:
  349. #{settings.siteUrl}/user/password/set?passwordResetToken=#{token}
  350. """
  351. done()
  352. grunt.registerTask 'delete-user', "deletes a user and all their data", () ->
  353. done = @async()
  354. email = grunt.option("email")
  355. if !email?
  356. console.error "Usage: grunt delete-user --email joe@example.com"
  357. process.exit(1)
  358. settings = require "settings-sharelatex"
  359. UserGetter = require "./app/js/Features/User/UserGetter"
  360. UserDeleter = require "./app/js/Features/User/UserDeleter"
  361. UserGetter.getUser email:email, (error, user) ->
  362. if error?
  363. throw error
  364. if !user?
  365. console.log("user #{email} not in database, potentially already deleted")
  366. return done()
  367. UserDeleter.deleteUser user._id, (err)->
  368. if err?
  369. throw err
  370. done()