-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile.js
More file actions
72 lines (63 loc) · 1.85 KB
/
file.js
File metadata and controls
72 lines (63 loc) · 1.85 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const os = require("os");
const fs = require("fs");
const path = require("path");
const dayjs = require("dayjs");
const config = require("./config");
const initConfig = async function (filePath) {
const isFileExist = await checkFileExist(filePath);
if (!isFileExist) {
await createFile(filePath, JSON.stringify(config.defaultConfig, null, 4));
return { isNew: true };
}
const userConfig = require(filePath);
if (userConfig.filePath && userConfig.filePath[0] === "~") {
userConfig.filePath = userConfig.filePath.replace(/^~/, os.homedir());
}
return userConfig;
};
const checkFileExist = function (filePath) {
return new Promise(function (resolve, reject) {
fs.access(filePath, fs.F_OK, function (err) {
if (err) {
return resolve(false);
}
resolve(true);
});
});
};
const createFile = async function (filePath, content) {
if (!fs.existsSync(path.dirname(filePath))) {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
}
fs.writeFileSync(filePath, content, "utf8", function (err) {
console.log(err);
});
};
const appendToLedger = async function (filePath, output) {
filePath = path.resolve(filePath);
const isFileExist = await checkFileExist(filePath);
if (!isFileExist) {
await createFile(filePath, "; Costflow CLI https://costflow.io\n\n");
}
fs.appendFileSync(filePath, output + "\n\n", "utf8", function (err) {
console.log(err);
});
};
const parseLedgerPath = function (filePath, date) {
const dateArr = filePath.match(/%(.*)%/g);
if (dateArr && dateArr.length) {
const dateStr = dateArr[0];
const dateInPath = dayjs(date).format(
dateStr.substr(1, dateStr.length - 2)
);
filePath = filePath.replace(dateStr, dateInPath);
}
return filePath;
};
module.exports = {
initConfig,
checkFileExist,
createFile,
parseLedgerPath,
appendToLedger,
};