Make fs read and write operations asynchronous

We're using the Promise/async version of file read/write commands,
so the script doesn't keep blocking or hanging around but can
keep doing other stuff till the file reading or writing is over
This commit is contained in:
Badri Sunderarajan 2020-05-04 18:18:31 +05:30
parent 198f576123
commit 0d8c6c6adc

View file

@ -91,7 +91,7 @@ class Seance {
})
// write metadata to output folder
fs.writeFileSync(path.join(outputFolder, 'metadata.json'), metadata)
await fs.promises.writeFile(path.join(outputFolder, 'metadata.json'), metadata)
return post
};
@ -142,7 +142,7 @@ class Seance {
console.debug('Loading metadata')
var postMetaFile = path.join(postFolder, 'metadata.json')
let postMeta = await JSON.parse(fs.readFileSync(postMetaFile))
let postMeta = await JSON.parse(await fs.promises.readFile(postMetaFile))
// Process lines
const readInterface = readline.createInterface({
@ -238,7 +238,7 @@ class Seance {
custom_excerpt: postMeta.subtitle || null,
tags: postMeta.tags,
authors: users,
html: markdown.toHTML(fs.readFileSync(postOutput, 'utf-8')),
html: markdown.toHTML(await fs.promises.readFile(postOutput, 'utf-8')),
feature_image: featuredImagePath
}, {source: 'html'})
.then((res) => {
@ -271,12 +271,16 @@ class Seance {
const response = await fetch(mediumUrl)
text = await response.text()
} else if (fs.existsSync(mediumUrl)) {
text = fs.readFileSync(mediumUrl).toString()
text = (await fs.promises.readFile(mediumUrl)).toString()
} else {
throw { error: 'URL must be a Medium URL or existing JSON file' }
}
json = await JSON.parse(text.substr(text.indexOf('{')))
try {
json = await JSON.parse(text.substr(text.indexOf('{')))
} catch(err) {
throw { error: 'You JSON seems to be malformed' }
}
return json;
}
@ -346,7 +350,7 @@ class Seance {
console.log(`Fetching: ${imagePath}`)
const response = await (await r2.get(imagePath).response).buffer()
await fs.writeFileSync(filePath, response, 'base64')
await await fs.promises.writeFile(filePath, response, 'base64')
console.log("Uploading to server")