Gruntfile.coffee 11 KB

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