2014-02-12 05:23:40 -05:00
|
|
|
ProjectEntityHandler = require "./ProjectEntityHandler"
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler = require "./ProjectEntityUpdateHandler"
|
2018-10-29 10:48:23 -04:00
|
|
|
ProjectGetter = require "./ProjectGetter"
|
2014-02-12 05:23:40 -05:00
|
|
|
Path = require "path"
|
2014-11-26 12:19:21 -05:00
|
|
|
async = require("async")
|
|
|
|
_ = require("underscore")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = ProjectRootDocManager =
|
2014-10-15 09:11:02 -04:00
|
|
|
setRootDocAutomatically: (project_id, callback = (error) ->) ->
|
2014-11-30 20:07:03 -05:00
|
|
|
|
2014-10-15 09:11:02 -04:00
|
|
|
ProjectEntityHandler.getAllDocs project_id, (error, docs) ->
|
2014-02-12 05:23:40 -05:00
|
|
|
return callback(error) if error?
|
2014-11-26 12:19:21 -05:00
|
|
|
|
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
root_doc_id = null
|
2014-11-26 12:19:21 -05:00
|
|
|
jobs = _.map docs, (doc, path)->
|
|
|
|
return (cb)->
|
2014-11-30 20:07:03 -05:00
|
|
|
rootDocId = null
|
2014-11-26 12:19:21 -05:00
|
|
|
for line in doc.lines || []
|
2015-08-28 11:51:52 -04:00
|
|
|
# We've had problems with this regex locking up CPU.
|
|
|
|
# Previously /.*\\documentclass/ would totally lock up on lines of 500kb (data text files :()
|
|
|
|
# This regex will only look from the start of the line, including whitespace so will return quickly
|
|
|
|
# regardless of line length.
|
2018-08-27 14:25:01 -04:00
|
|
|
match = /^\s*\\documentclass/.test(line)
|
|
|
|
isRootDoc = /\.R?tex$/.test(Path.extname(path)) and match
|
2014-11-26 12:19:21 -05:00
|
|
|
if isRootDoc
|
2014-11-30 20:07:03 -05:00
|
|
|
rootDocId = doc?._id
|
|
|
|
cb(rootDocId)
|
|
|
|
|
2014-11-26 16:53:57 -05:00
|
|
|
async.series jobs, (root_doc_id)->
|
2014-11-26 12:19:21 -05:00
|
|
|
if root_doc_id?
|
2018-02-01 10:31:42 -05:00
|
|
|
ProjectEntityUpdateHandler.setRootDoc project_id, root_doc_id, callback
|
2014-11-26 12:19:21 -05:00
|
|
|
else
|
|
|
|
callback()
|
2018-09-24 06:59:55 -04:00
|
|
|
|
|
|
|
setRootDocFromName: (project_id, rootDocName, callback = (error) ->) ->
|
2018-09-24 11:04:23 -04:00
|
|
|
ProjectEntityHandler.getAllDocPathsFromProjectById project_id, (error, docPaths) ->
|
2018-09-24 06:59:55 -04:00
|
|
|
return callback(error) if error?
|
2018-09-25 04:04:56 -04:00
|
|
|
# strip off leading and trailing quotes from rootDocName
|
|
|
|
rootDocName = rootDocName.replace(/^\'|\'$/g,"")
|
|
|
|
# prepend a slash for the root folder if not present
|
|
|
|
rootDocName = "/#{rootDocName}" if rootDocName[0] isnt '/'
|
2018-09-24 06:59:55 -04:00
|
|
|
# find the root doc from the filename
|
|
|
|
root_doc_id = null
|
|
|
|
for doc_id, path of docPaths
|
|
|
|
# docpaths have a leading / so allow matching "folder/filename" and "/folder/filename"
|
2018-09-25 04:04:56 -04:00
|
|
|
if path == rootDocName
|
2018-09-24 06:59:55 -04:00
|
|
|
root_doc_id = doc_id
|
2018-10-29 10:48:23 -04:00
|
|
|
# try a basename match if there was no match
|
2018-09-24 06:59:55 -04:00
|
|
|
if !root_doc_id
|
|
|
|
for doc_id, path of docPaths
|
2018-10-29 10:48:23 -04:00
|
|
|
if Path.basename(path) == Path.basename(rootDocName)
|
2018-09-24 06:59:55 -04:00
|
|
|
root_doc_id = doc_id
|
|
|
|
# set the root doc id if we found a match
|
|
|
|
if root_doc_id?
|
|
|
|
ProjectEntityUpdateHandler.setRootDoc project_id, root_doc_id, callback
|
|
|
|
else
|
2018-10-29 10:48:23 -04:00
|
|
|
callback()
|
|
|
|
|
|
|
|
ensureRootDocumentIsSet: (project_id, callback = (error) ->) ->
|
|
|
|
ProjectGetter.getProject project_id, rootDoc_id: 1, (error, project) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if !project?
|
|
|
|
return callback new Error("project not found")
|
|
|
|
|
|
|
|
if project.rootDoc_id?
|
|
|
|
callback()
|
|
|
|
else
|
|
|
|
ProjectRootDocManager.setRootDocAutomatically project_id, callback
|
2018-11-09 03:09:13 -05:00
|
|
|
|
|
|
|
ensureRootDocumentIsValid: (project_id, callback = (error) ->) ->
|
|
|
|
ProjectGetter.getProject project_id, rootDoc_id: 1, (error, project) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
if !project?
|
|
|
|
return callback new Error("project not found")
|
|
|
|
|
|
|
|
if project.rootDoc_id?
|
|
|
|
ProjectEntityHandler.getAllDocPathsFromProjectById project_id, (error, docPaths) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
rootDocValid = false
|
|
|
|
for doc_id, _path of docPaths
|
|
|
|
if doc_id == project.rootDoc_id
|
|
|
|
rootDocValid = true
|
|
|
|
if rootDocValid
|
|
|
|
callback()
|
|
|
|
else
|
|
|
|
ProjectEntityUpdateHandler.setRootDoc project_id, null, ->
|
|
|
|
ProjectRootDocManager.setRootDocAutomatically project_id, callback
|
|
|
|
else
|
|
|
|
ProjectRootDocManager.setRootDocAutomatically project_id, callback
|