Skip to content

Commit

Permalink
checking if file exist and if it does not create it (#113)
Browse files Browse the repository at this point in the history
* checking if file exist and if it does not create it

* await on write manifest to avoid race-condition

Co-authored-by: Joel Denning <joeldenning@gmail.com>

Co-authored-by: Joel Denning <joeldenning@gmail.com>
  • Loading branch information
tungurlakachakcak and joeldenning authored May 17, 2021
1 parent 5b55093 commit ab06209
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/io-methods/filesystem.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit ab06209

Please sign in to comment.