Hi everybody!
I am trying to retrieve files from a remote host and write them to the dist
folder during the build process for Cloudflare Pages.
If the file already exists in the Git repository, e.g. ./dist/folder/file.html
, I can write to that file and change its content using the below code:
async function downloadFile (url, filepath) {
const res = await fetch(url);
const fileStream = fs.createWriteStream(filepath);
await new Promise((resolve, reject) => {
res.body.pipe(fileStream);
res.body.on("error", reject);
fileStream.on("finish", resolve);
});
};
However, unfortunately, I haven’t figured out how to create a new file in ./dist/
to then write fetched content to. So far, I have tried fs.writeFile
and fs-extra
with ensureFile
, but both quit with the same error:
ENOENT: no such file or directory
Is the dist
folder writeable during build?