Gruntfile.coffee 11 KB

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