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:
parent
198f576123
commit
0d8c6c6adc
1 changed files with 10 additions and 6 deletions
16
functions.js
16
functions.js
|
@ -91,7 +91,7 @@ class Seance {
|
||||||
})
|
})
|
||||||
|
|
||||||
// write metadata to output folder
|
// 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
|
return post
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -142,7 +142,7 @@ class Seance {
|
||||||
console.debug('Loading metadata')
|
console.debug('Loading metadata')
|
||||||
|
|
||||||
var postMetaFile = path.join(postFolder, 'metadata.json')
|
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
|
// Process lines
|
||||||
const readInterface = readline.createInterface({
|
const readInterface = readline.createInterface({
|
||||||
|
@ -238,7 +238,7 @@ class Seance {
|
||||||
custom_excerpt: postMeta.subtitle || null,
|
custom_excerpt: postMeta.subtitle || null,
|
||||||
tags: postMeta.tags,
|
tags: postMeta.tags,
|
||||||
authors: users,
|
authors: users,
|
||||||
html: markdown.toHTML(fs.readFileSync(postOutput, 'utf-8')),
|
html: markdown.toHTML(await fs.promises.readFile(postOutput, 'utf-8')),
|
||||||
feature_image: featuredImagePath
|
feature_image: featuredImagePath
|
||||||
}, {source: 'html'})
|
}, {source: 'html'})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
|
@ -271,12 +271,16 @@ class Seance {
|
||||||
const response = await fetch(mediumUrl)
|
const response = await fetch(mediumUrl)
|
||||||
text = await response.text()
|
text = await response.text()
|
||||||
} else if (fs.existsSync(mediumUrl)) {
|
} else if (fs.existsSync(mediumUrl)) {
|
||||||
text = fs.readFileSync(mediumUrl).toString()
|
text = (await fs.promises.readFile(mediumUrl)).toString()
|
||||||
} else {
|
} else {
|
||||||
throw { error: 'URL must be a Medium URL or existing JSON file' }
|
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;
|
return json;
|
||||||
}
|
}
|
||||||
|
@ -346,7 +350,7 @@ class Seance {
|
||||||
console.log(`Fetching: ${imagePath}`)
|
console.log(`Fetching: ${imagePath}`)
|
||||||
|
|
||||||
const response = await (await r2.get(imagePath).response).buffer()
|
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")
|
console.log("Uploading to server")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue