Handle creating folders for file write streams, and handle errors.

This commit is contained in:
Dillon Kearns 2024-04-17 18:16:25 -07:00
parent e5e1798459
commit d2bdf92e95
2 changed files with 19 additions and 3 deletions

View File

@ -190,6 +190,11 @@ b =
|> Stream.run
|> expectError "file not found error"
"Error: ENOENT: no such file or directory, open '/Users/dillonkearns/src/github.com/dillonkearns/elm-pages/examples/end-to-end/does-not-exist'"
, Stream.fromString "This is input..."
|> Stream.pipe (Stream.fileWrite "/this/is/invalid.txt")
|> Stream.run
|> expectError "invalid file write destination"
"Error: ENOENT: no such file or directory, mkdir '/this'"
]

View File

@ -724,11 +724,22 @@ function runStream(req, portsFile) {
stream: lastStream.pipe(zlib.createUnzip()),
};
} else if (part.name === "fileWrite") {
const destinationPath = path.resolve(part.path);
try {
await fsPromises.mkdir(path.dirname(destinationPath), {
recursive: true,
});
} catch (error) {
resolve({ error: error.toString() });
}
const newLocal = fs.createWriteStream(destinationPath);
newLocal.once("error", (error) => {
newLocal.close();
resolve({ error: error.toString() });
});
return {
metadata: null,
stream: lastStream.pipe(
fs.createWriteStream(path.resolve(part.path))
),
stream: lastStream.pipe(newLocal),
};
} else if (part.name === "httpWrite") {
const makeFetchHappen = makeFetchHappenOriginal.defaults({