From ab062094ae86efec3636c9ff2f135ad1fe79b097 Mon Sep 17 00:00:00 2001 From: Valeri Date: Mon, 17 May 2021 19:31:04 +0300 Subject: [PATCH] checking if file exist and if it does not create it (#113) * checking if file exist and if it does not create it * await on write manifest to avoid race-condition Co-authored-by: Joel Denning Co-authored-by: Joel Denning --- src/io-methods/filesystem.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/io-methods/filesystem.js b/src/io-methods/filesystem.js index 73b2973..c329185 100644 --- a/src/io-methods/filesystem.js +++ b/src/io-methods/filesystem.js @@ -1,10 +1,19 @@ "use strict"; const fs = require("fs/promises"); const { getConfig } = require("../config"); +const { getEmptyManifest } = require("../modify"); const jsHelpers = require("./js-file-helpers.js"); -exports.readManifest = function (filePath) { - return fs.readFile(filePath, "utf-8"); +exports.readManifest = async function (filePath) { + try { + await fs.access(filePath, fs.F_OK); + + return fs.readFile(filePath, "utf-8"); + } catch (missingFileErr) { + await exports.writeManifest(filePath, JSON.stringify(getEmptyManifest())); + + return fs.readFile(filePath, "utf-8"); + } }; exports.writeManifest = function (filePath, data) {