forked from 0xSlot/erc721-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 6
/
file-helper.js
37 lines (29 loc) · 942 Bytes
/
file-helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"use strict";
const fs = require("fs");
const path = require("path");
const { promisify } = require("util");
const existsAsync = promisify(fs.exists);
const makeDirectoryAsync = promisify(fs.mkdir);
const readFileAsync = promisify(fs.readFile);
const writeFileAsync = promisify(fs.writeFile);
const ensureDirectoryExists = async directory => {
try {
await makeDirectoryAsync(directory, { recursive: true });
} catch (err) {
console.log(err);
}
};
module.exports.ensureDirectory = async directory => {
ensureDirectoryExists(directory);
};
module.exports.writeFile = async (filePath, data) => {
await ensureDirectoryExists(path.dirname(filePath));
await writeFileAsync(filePath, JSON.stringify(data, null, 2));
};
module.exports.parseFile = async filePath => {
if (await existsAsync(filePath)) {
const contents = await readFileAsync(filePath);
return JSON.parse(contents.toString());
}
return null;
};