2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
2022-05-16 10:25:49 -04:00
|
|
|
n/handle-callback-err,
|
2019-05-29 05:21:06 -04:00
|
|
|
max-len,
|
|
|
|
no-unused-vars,
|
|
|
|
no-useless-escape,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
let ProjectRootDocManager
|
|
|
|
const ProjectEntityHandler = require('./ProjectEntityHandler')
|
|
|
|
const ProjectEntityUpdateHandler = require('./ProjectEntityUpdateHandler')
|
|
|
|
const ProjectGetter = require('./ProjectGetter')
|
|
|
|
const DocumentHelper = require('../Documents/DocumentHelper')
|
|
|
|
const Path = require('path')
|
|
|
|
const fs = require('fs')
|
2019-06-24 09:18:14 -04:00
|
|
|
const { promisify } = require('util')
|
2019-05-29 05:21:06 -04:00
|
|
|
const async = require('async')
|
|
|
|
const globby = require('globby')
|
|
|
|
const _ = require('underscore')
|
|
|
|
|
|
|
|
module.exports = ProjectRootDocManager = {
|
|
|
|
setRootDocAutomatically(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
return ProjectEntityHandler.getAllDocs(project_id, function (error, docs) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
const jobs = _.map(
|
|
|
|
docs,
|
|
|
|
(doc, path) =>
|
2021-04-14 09:17:21 -04:00
|
|
|
function (cb) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (
|
2019-10-03 10:10:00 -04:00
|
|
|
ProjectEntityUpdateHandler.isPathValidForRootDoc(path) &&
|
2019-05-29 05:21:06 -04:00
|
|
|
DocumentHelper.contentHasDocumentclass(doc.lines)
|
|
|
|
) {
|
2021-04-14 09:17:21 -04:00
|
|
|
async.setImmediate(function () {
|
2020-04-08 09:44:01 -04:00
|
|
|
cb(doc._id)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2021-04-14 09:17:21 -04:00
|
|
|
async.setImmediate(function () {
|
2020-04-08 09:44:01 -04:00
|
|
|
cb(null)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
return async.series(jobs, function (root_doc_id) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (root_doc_id != null) {
|
|
|
|
return ProjectEntityUpdateHandler.setRootDoc(
|
|
|
|
project_id,
|
|
|
|
root_doc_id,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
findRootDocFileFromDirectory(directoryPath, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
const filePathsPromise = globby(['**/*.{tex,Rtex}'], {
|
|
|
|
cwd: directoryPath,
|
|
|
|
followSymlinkedDirectories: false,
|
|
|
|
onlyFiles: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
case: false,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
// the search order is such that we prefer files closer to the project root, then
|
|
|
|
// we go by file size in ascending order, because people often have a main
|
|
|
|
// file that just includes a bunch of other files; then we go by name, in
|
|
|
|
// order to be deterministic
|
|
|
|
filePathsPromise.then(
|
|
|
|
unsortedFiles =>
|
|
|
|
ProjectRootDocManager._sortFileList(
|
|
|
|
unsortedFiles,
|
|
|
|
directoryPath,
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, files) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
let doc = null
|
|
|
|
|
|
|
|
return async.until(
|
2022-06-15 06:29:35 -04:00
|
|
|
() => doc != null || files.length === 0,
|
2021-04-14 09:17:21 -04:00
|
|
|
function (cb) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const file = files.shift()
|
|
|
|
return fs.readFile(
|
|
|
|
Path.join(directoryPath, file),
|
|
|
|
'utf8',
|
2021-04-14 09:17:21 -04:00
|
|
|
function (error, content) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return cb(error)
|
|
|
|
}
|
|
|
|
content = (content || '').replace(/\r/g, '')
|
|
|
|
if (DocumentHelper.contentHasDocumentclass(content)) {
|
|
|
|
doc = { path: file, content }
|
|
|
|
}
|
|
|
|
return cb(null)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
err =>
|
|
|
|
callback(
|
|
|
|
err,
|
|
|
|
doc != null ? doc.path : undefined,
|
|
|
|
doc != null ? doc.content : undefined
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
),
|
|
|
|
err => callback(err)
|
|
|
|
)
|
|
|
|
|
|
|
|
// coffeescript's implicit-return mechanism returns filePathsPromise from this method, which confuses mocha
|
|
|
|
return null
|
|
|
|
},
|
|
|
|
|
|
|
|
setRootDocFromName(project_id, rootDocName, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return ProjectEntityHandler.getAllDocPathsFromProjectById(
|
|
|
|
project_id,
|
2021-04-14 09:17:21 -04:00
|
|
|
function (error, docPaths) {
|
2019-05-29 05:21:06 -04:00
|
|
|
let doc_id, path
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
// strip off leading and trailing quotes from rootDocName
|
|
|
|
rootDocName = rootDocName.replace(/^\'|\'$/g, '')
|
|
|
|
// prepend a slash for the root folder if not present
|
|
|
|
if (rootDocName[0] !== '/') {
|
|
|
|
rootDocName = `/${rootDocName}`
|
|
|
|
}
|
|
|
|
// find the root doc from the filename
|
|
|
|
let root_doc_id = null
|
|
|
|
for (doc_id in docPaths) {
|
|
|
|
// docpaths have a leading / so allow matching "folder/filename" and "/folder/filename"
|
|
|
|
path = docPaths[doc_id]
|
|
|
|
if (path === rootDocName) {
|
|
|
|
root_doc_id = doc_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// try a basename match if there was no match
|
|
|
|
if (!root_doc_id) {
|
|
|
|
for (doc_id in docPaths) {
|
|
|
|
path = docPaths[doc_id]
|
|
|
|
if (Path.basename(path) === Path.basename(rootDocName)) {
|
|
|
|
root_doc_id = doc_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// set the root doc id if we found a match
|
|
|
|
if (root_doc_id != null) {
|
|
|
|
return ProjectEntityUpdateHandler.setRootDoc(
|
|
|
|
project_id,
|
|
|
|
root_doc_id,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
ensureRootDocumentIsSet(project_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
return ProjectGetter.getProject(
|
|
|
|
project_id,
|
|
|
|
{ rootDoc_id: 1 },
|
|
|
|
function (error, project) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (project == null) {
|
|
|
|
return callback(new Error('project not found'))
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
if (project.rootDoc_id != null) {
|
|
|
|
return callback()
|
|
|
|
} else {
|
|
|
|
return ProjectRootDocManager.setRootDocAutomatically(
|
|
|
|
project_id,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2021-06-23 09:13:49 -04:00
|
|
|
/**
|
|
|
|
* @param {ObjectId | string} project_id
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
2019-05-29 05:21:06 -04:00
|
|
|
ensureRootDocumentIsValid(project_id, callback) {
|
2021-06-23 09:13:49 -04:00
|
|
|
ProjectGetter.getProjectWithoutDocLines(
|
2021-04-14 09:17:21 -04:00
|
|
|
project_id,
|
|
|
|
function (error, project) {
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
if (project == null) {
|
|
|
|
return callback(new Error('project not found'))
|
|
|
|
}
|
2020-02-05 07:53:31 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
if (project.rootDoc_id != null) {
|
2021-06-23 09:13:49 -04:00
|
|
|
ProjectEntityHandler.getDocPathFromProjectByDocId(
|
|
|
|
project,
|
|
|
|
project.rootDoc_id,
|
|
|
|
(err, docPath) => {
|
|
|
|
if (docPath) return callback()
|
|
|
|
ProjectEntityUpdateHandler.unsetRootDoc(project_id, () =>
|
|
|
|
ProjectRootDocManager.setRootDocAutomatically(
|
|
|
|
project_id,
|
|
|
|
callback
|
2020-02-11 08:19:47 -05:00
|
|
|
)
|
2021-06-23 09:13:49 -04:00
|
|
|
)
|
2020-02-11 08:19:47 -05:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return ProjectRootDocManager.setRootDocAutomatically(
|
|
|
|
project_id,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
2020-02-11 08:19:47 -05:00
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_sortFileList(listToSort, rootDirectory, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return async.mapLimit(
|
|
|
|
listToSort,
|
|
|
|
5,
|
|
|
|
(filePath, cb) =>
|
2021-04-14 09:17:21 -04:00
|
|
|
fs.stat(Path.join(rootDirectory, filePath), function (err, stat) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return cb(err)
|
|
|
|
}
|
|
|
|
return cb(null, {
|
|
|
|
size: stat.size,
|
|
|
|
path: filePath,
|
|
|
|
elements: filePath.split(Path.sep).length,
|
2021-04-27 03:52:58 -04:00
|
|
|
name: Path.basename(filePath),
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
}),
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, files) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
_.map(
|
|
|
|
files.sort(ProjectRootDocManager._rootDocSort),
|
|
|
|
file => file.path
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
_rootDocSort(a, b) {
|
|
|
|
// sort first by folder depth
|
|
|
|
if (a.elements !== b.elements) {
|
|
|
|
return a.elements - b.elements
|
|
|
|
}
|
|
|
|
// ensure main.tex is at the start of each folder
|
|
|
|
if (a.name === 'main.tex' && b.name !== 'main.tex') {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if (a.name !== 'main.tex' && b.name === 'main.tex') {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
// prefer smaller files
|
|
|
|
if (a.size !== b.size) {
|
|
|
|
return a.size - b.size
|
|
|
|
}
|
|
|
|
// otherwise, use the full path name
|
|
|
|
return a.path.localeCompare(b.path)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-24 09:18:14 -04:00
|
|
|
|
|
|
|
const promises = {
|
|
|
|
setRootDocAutomatically: promisify(
|
|
|
|
ProjectRootDocManager.setRootDocAutomatically
|
|
|
|
),
|
|
|
|
|
|
|
|
findRootDocFileFromDirectory: directoryPath =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
ProjectRootDocManager.findRootDocFileFromDirectory(
|
|
|
|
directoryPath,
|
|
|
|
(error, path, content) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error)
|
|
|
|
} else {
|
|
|
|
resolve({ path, content })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}),
|
2021-04-27 03:52:58 -04:00
|
|
|
setRootDocFromName: promisify(ProjectRootDocManager.setRootDocFromName),
|
2019-06-24 09:18:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ProjectRootDocManager.promises = promises
|
|
|
|
|
|
|
|
module.exports = ProjectRootDocManager
|