-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.js
35 lines (27 loc) · 861 Bytes
/
io.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
import fs from "fs";
import { decrypt, encrypt } from "./encrypt.js";
export function readJSON({ path, shouldDecrypt = false }) {
if (!fs.existsSync(path)) {
throw new Error(`${path} does not exist.`);
}
const file = JSON.parse(fs.readFileSync(path));
if (shouldDecrypt) {
return JSON.parse(decrypt(file));
}
return file;
}
export function writeJSON({ fileName, data, shouldEncrypt = false }) {
if (shouldEncrypt) {
data = encrypt(JSON.stringify(data));
}
if (!fs.existsSync("./output")) {
fs.mkdirSync("./output");
}
fs.writeFileSync(`./output/${fileName}.json`, JSON.stringify(data, null, 2));
}
export function writePlainText({ folder, fileName, data }) {
if (!fs.existsSync(`./output/${folder}`)) {
fs.mkdirSync(`./output/${folder}`);
}
fs.writeFileSync(`./output/${folder}/${fileName}.txt`, data);
}