diff --git a/functions.js b/functions.js index fa68f74..d44ba02 100644 --- a/functions.js +++ b/functions.js @@ -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")