Skip to content

Commit 40cf295

Browse files
FNDmoonglum
authored andcommitted
Guard against disappearing files
temporary files can lead to a race condition, eventually causing an exception - now we're warning, but don't crash
1 parent 602b229 commit 40cf295

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

index.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1+
let { readFile, stat } = require("fs").promises;
12
let path = require("path");
2-
let { promisify } = require("faucet-pipeline-core/lib/util");
33
let FileFinder = require("faucet-pipeline-core/lib/util/files/finder");
44

5-
let readFile = promisify(require("fs").readFile);
6-
let stat = promisify(require("fs").stat);
7-
85
module.exports = {
96
key: "static",
107
bucket: "static",
@@ -69,24 +66,32 @@ function processFiles(fileNames, config) {
6966
return Promise.all(fileNames.map(fileName => processFile(fileName, config)));
7067
}
7168

72-
function processFile(fileName,
69+
async function processFile(fileName,
7370
{ source, target, targetDir, fingerprint, assetManager, plugins }) {
7471
let sourcePath = path.join(source, fileName);
7572
let targetPath = path.join(target, fileName);
7673

77-
return readFile(sourcePath).
78-
then(content => {
79-
let type = determineFileType(sourcePath);
80-
let plugin = type && plugins[type];
81-
return plugin ? plugin(content) : content;
82-
}).
83-
then(content => {
84-
let options = { targetDir };
85-
if(fingerprint !== undefined) {
86-
options.fingerprint = fingerprint;
87-
}
88-
return assetManager.writeFile(targetPath, content, options);
89-
});
74+
try {
75+
var content = await readFile(sourcePath); // eslint-disable-line no-var
76+
} catch(err) {
77+
if(err.code !== "ENOENT") {
78+
throw err;
79+
}
80+
console.error(`WARNING: \`${sourcePath}\` no longer exists`);
81+
return;
82+
}
83+
84+
let type = determineFileType(sourcePath);
85+
if(type && plugins[type]) {
86+
let plugin = plugins[type];
87+
content = await plugin(content);
88+
}
89+
90+
let options = { targetDir };
91+
if(fingerprint !== undefined) {
92+
options.fingerprint = fingerprint;
93+
}
94+
return assetManager.writeFile(targetPath, content, options);
9095
}
9196

9297
function determineFileType(sourcePath) {

0 commit comments

Comments
 (0)