Gruntfile.coffee 11 KB

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