mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Merge pull request #201 from overleaf/bg-more-cleanup
decaff cleanup of OutputFileManager
This commit is contained in:
commit
90c0d931ad
2 changed files with 19 additions and 51 deletions
|
@ -1,66 +1,38 @@
|
|||
/* eslint-disable
|
||||
handle-callback-err,
|
||||
no-return-assign,
|
||||
no-unused-vars,
|
||||
no-useless-escape,
|
||||
*/
|
||||
// TODO: This file was created by bulk-decaffeinate.
|
||||
// Fix any style issues and re-enable lint.
|
||||
/*
|
||||
* decaffeinate suggestions:
|
||||
* DS101: Remove unnecessary use of Array.from
|
||||
* DS102: Remove unnecessary code created because of implicit returns
|
||||
* DS103: Rewrite code to no longer use __guard__
|
||||
* DS207: Consider shorter variations of null checks
|
||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||
*/
|
||||
let OutputFileFinder
|
||||
const async = require('async')
|
||||
const fs = require('fs')
|
||||
const Path = require('path')
|
||||
const _ = require('lodash')
|
||||
const { spawn } = require('child_process')
|
||||
const logger = require('logger-sharelatex')
|
||||
|
||||
module.exports = OutputFileFinder = {
|
||||
findOutputFiles(resources, directory, callback) {
|
||||
if (callback == null) {
|
||||
callback = function (error, outputFiles, allFiles) {}
|
||||
}
|
||||
const incomingResources = {}
|
||||
for (const resource of Array.from(resources)) {
|
||||
incomingResources[resource.path] = true
|
||||
}
|
||||
const incomingResources = new Set(
|
||||
resources.map((resource) => resource.path)
|
||||
)
|
||||
|
||||
return OutputFileFinder._getAllFiles(directory, function (error, allFiles) {
|
||||
OutputFileFinder._getAllFiles(directory, function (error, allFiles) {
|
||||
if (allFiles == null) {
|
||||
allFiles = []
|
||||
}
|
||||
if (error != null) {
|
||||
if (error) {
|
||||
logger.err({ err: error }, 'error finding all output files')
|
||||
return callback(error)
|
||||
}
|
||||
const outputFiles = []
|
||||
for (const file of Array.from(allFiles)) {
|
||||
if (!incomingResources[file]) {
|
||||
for (const file of allFiles) {
|
||||
if (!incomingResources.has(file)) {
|
||||
outputFiles.push({
|
||||
path: file,
|
||||
type: __guard__(file.match(/\.([^\.]+)$/), (x) => x[1])
|
||||
type: Path.extname(file).replace(/^\./, '') || undefined
|
||||
})
|
||||
}
|
||||
}
|
||||
return callback(null, outputFiles, allFiles)
|
||||
callback(null, outputFiles, allFiles)
|
||||
})
|
||||
},
|
||||
|
||||
_getAllFiles(directory, _callback) {
|
||||
if (_callback == null) {
|
||||
_callback = function (error, fileList) {}
|
||||
}
|
||||
const callback = function (error, fileList) {
|
||||
_callback(error, fileList)
|
||||
return (_callback = function () {})
|
||||
}
|
||||
|
||||
_getAllFiles(directory, callback) {
|
||||
callback = _.once(callback)
|
||||
// don't include clsi-specific files/directories in the output list
|
||||
const EXCLUDE_DIRS = [
|
||||
'-name',
|
||||
|
@ -75,7 +47,7 @@ module.exports = OutputFileFinder = {
|
|||
const args = [
|
||||
directory,
|
||||
'(',
|
||||
...Array.from(EXCLUDE_DIRS),
|
||||
...EXCLUDE_DIRS,
|
||||
')',
|
||||
'-prune',
|
||||
'-o',
|
||||
|
@ -89,7 +61,7 @@ module.exports = OutputFileFinder = {
|
|||
let stdout = ''
|
||||
proc.stdout.setEncoding('utf8').on('data', (chunk) => (stdout += chunk))
|
||||
proc.on('error', callback)
|
||||
return proc.on('close', function (code) {
|
||||
proc.on('close', function (code) {
|
||||
if (code !== 0) {
|
||||
logger.warn(
|
||||
{ directory, code },
|
||||
|
@ -100,16 +72,9 @@ module.exports = OutputFileFinder = {
|
|||
let fileList = stdout.trim().split('\n')
|
||||
fileList = fileList.map(function (file) {
|
||||
// Strip leading directory
|
||||
let path
|
||||
return (path = Path.relative(directory, file))
|
||||
return Path.relative(directory, file)
|
||||
})
|
||||
return callback(null, fileList)
|
||||
callback(null, fileList)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function __guard__(value, transform) {
|
||||
return typeof value !== 'undefined' && value !== null
|
||||
? transform(value)
|
||||
: undefined
|
||||
}
|
||||
|
|
|
@ -28,6 +28,9 @@ describe('OutputFileFinder', function () {
|
|||
fs: (this.fs = {}),
|
||||
child_process: { spawn: (this.spawn = sinon.stub()) },
|
||||
'logger-sharelatex': { log: sinon.stub(), warn: sinon.stub() }
|
||||
},
|
||||
globals: {
|
||||
Math // used by lodash
|
||||
}
|
||||
})
|
||||
this.directory = '/test/dir'
|
||||
|
|
Loading…
Reference in a new issue