overleaf/services/clsi/app/js/OutputFileFinder.js
Jakob Ackermann 5aeb1f1459 Merge pull request #16438 from overleaf/jpa-em-replace-find-subprocess
[clsi] replace find subprocess for listing compile dir contents

GitOrigin-RevId: 36c8230ea6d787b1d948407d6473c14af8d6b5f6
2024-01-11 09:04:52 +00:00

54 lines
1.3 KiB
JavaScript

const Path = require('path')
const fs = require('fs')
const { callbackifyMultiResult } = require('@overleaf/promise-utils')
async function walkFolder(compileDir, d, files, allEntries) {
const dirents = await fs.promises.readdir(Path.join(compileDir, d), {
withFileTypes: true,
})
for (const dirent of dirents) {
const p = Path.join(d, dirent.name)
if (dirent.isDirectory()) {
await walkFolder(compileDir, p, files, allEntries)
allEntries.push(p + '/')
} else if (dirent.isFile()) {
files.push(p)
allEntries.push(p)
} else {
allEntries.push(p)
}
}
}
async function findOutputFiles(resources, directory) {
const files = []
const allEntries = []
await walkFolder(directory, '', files, allEntries)
const incomingResources = new Set(resources.map(resource => resource.path))
const outputFiles = []
for (const path of files) {
if (incomingResources.has(path)) continue
if (path === '.project-sync-state') continue
if (path === '.project-lock') continue
outputFiles.push({
path,
type: Path.extname(path).replace(/^\./, '') || undefined,
})
}
return {
outputFiles,
allEntries,
}
}
module.exports = {
findOutputFiles: callbackifyMultiResult(findOutputFiles, [
'outputFiles',
'allEntries',
]),
promises: {
findOutputFiles,
},
}