-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreader_writer.js
189 lines (150 loc) · 4 KB
/
reader_writer.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const fs = require("fs");
const path = require("path");
const fsp = require("fs").promises;
const log = require("./logger.js");
module.exports.copyFile = function(source, target)
{
return exports.checkAndCreateFilePath(target)
.then(() => fsp.readFile(source))
.then((buffer) => fsp.writeFile(target, buffer))
.catch((err) => Promise.reject(err));
};
module.exports.walkDir = function(dir)
{
const results = [];
const path = require('path');
return _walk(dir);
function _walk(dir)
{
return new Promise((resolve, reject) =>
{
fsp.readdir(dir)
.then((list) =>
{
let pending = list.length;
if (pending <= 0)
return resolve(results);
list.forEach((file) =>
{
file = path.resolve(dir, file);
fsp.stat(file)
.then((stat) =>
{
if (stat.isDirectory() === true)
{
_walk(file)
.then((res) =>
{
results.concat(res);
pending--;
if (pending <= 0)
return resolve(results);
});
}
else
{
results.push(file);
pending--;
if (pending <= 0)
return resolve(results);
}
});
})
})
.catch((err) => reject(err));
});
}
};
//If the dir path up to a filename does not exist, this will create it
module.exports.checkAndCreateFilePath = async function(filePath)
{
let directories = [];
let currentPath = path.dirname(filePath);
if (fs.existsSync(currentPath) === true)
return;
while (currentPath !== path.dirname(currentPath))
{
directories.unshift(currentPath);
currentPath = path.dirname(currentPath);
}
for (const dir of directories) {
if (fs.existsSync(dir) === false)
{
await fsp.mkdir(dir);
}
}
};
module.exports.getDirFilenames = async function(dirPath, extensionFilter = "")
{
let readFilenames = [];
let filenames;
if (fs.existsSync(dirPath) === false)
return Promise.reject(new Error(`The directory ${dirPath} was not found on the server.`));
filenames = await fsp.readdir(dirPath, "utf8");
filenames.forEach((filename) =>
{
if (extensionFilter === "" || path.extname(filename) === extensionFilter)
readFilenames.push(filename);
});
return readFilenames;
};
//gets an array with all the filenames inside a directory, NOT including folders
exports.getOnlyDirFilenamesSync = (dirPath) =>
{
let filenames = fs.readdirSync(dirPath);
let onlyFilenames = [];
filenames.forEach((filename) =>
{
let stat = fs.statSync(`${dirPath}/${filename}`);
let isDirectory = stat.isDirectory();
if (isDirectory === false)
onlyFilenames.push(filename);
});
return onlyFilenames;
};
//gets an array with all the folder names inside a directory
exports.getDirSubfolderNamesSync = (dirPath) =>
{
let filenames = fs.readdirSync(dirPath);
let subfolderNames = [];
filenames.forEach((filename) =>
{
let stat = fs.statSync(path.join(dirPath, filename));
let isDirectory = stat.isDirectory();
if (isDirectory === true)
subfolderNames.push(filename);
});
return subfolderNames;
};
module.exports.append = async (filePath, stringData) =>
{
const dirPath = filePath.replace(/\/.+$/, "");
if (fs.existsSync(dirPath) === false)
throw new Error(`Directory ${dirPath} does not exist.`);
if (fs.existsSync(filePath) === false)
await fsp.writeFile(filePath, stringData);
else await fsp.appendFile(filePath, stringData);
};
//Stringify that prevents circular references taken from https://antony.fyi/pretty-printing-javascript-objects-as-json/
module.exports.JSONStringify = function(obj, spacing = 2)
{
let cache = [];
//custom replacer function gets around the circular reference errors by discarding them
let str = JSON.stringify(obj, function(key, value)
{
if (typeof value === "object" && value != null)
{
//value already found before, discard it
if (cache.indexOf(value) !== -1)
{
return;
}
//not found before, store this value for reference
cache.push(value);
}
return value;
}, spacing);
//enable garbage collection
cache = null;
return str;
};