-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileserver.js
More file actions
252 lines (229 loc) · 6.38 KB
/
Copy pathfileserver.js
File metadata and controls
252 lines (229 loc) · 6.38 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import { readdir, rename, mkdir, stat, rmdir, readFile, writeFileSync, unlink } from 'fs';
import { join, dirname } from 'path';
import EventEmitter from 'events';
import { promisify } from 'util.promisify';
/**
* An instance of [Promise]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise}.
* @typedef {Promise} Promise
*/
/**
* Promisified fs.mkdir.
* @return {Promise} promise - return a promise
*/
const pmMkdir = promisify(mkdir);
/**
* Promisified fs.stat.
* @return {Promise} promise - return a promise
*/
const pmStat = promisify(stat);
/**
* Promisified fs.rename.
* @return {Promise} promise - return a promise
*/
const pmRename = promisify(rename);
/**
* Promisified fs.rmdir.
* @return {Promise} promise - return a promise
*/
const pmRmdir = promisify(rmdir);
/**
* Promisified fs.readdir
* @return {Promise} promise - return a promise
*/
const pmReaddir = promisify(readdir);
/**
* Promisified fs.readFile
* @return {Promise} promise - return a promise
*/
const pmReadFile = promisify(readFile);
/**
* Promisified fs.unlink
* @return {Promise} promise - return a promise
*/
const pmUnlink = promisify(unlink);
/**
* @module fs
*/
/**
* Creates a file server to list, read, create, update or delete file.
* @class
*/
class FileServer extends EventEmitter {
/**
* Create an instance of file server.
* @param {string} dirpath - the root directory path, that belongs to db
*/
constructor(dirpath) {
super();
if (typeof dirpath !== 'string' || !dirpath.length) {
throw new Error('The `dirpath` parameter is missing.');
}
this.dirpath = dirpath;
}
/**
* list all the files in a directory.
* @param {string} dpath - the directory path, that should be read
* @return {Promise} promise - return a promise
*/
async list(dpath) {
return (await pmReaddir(join(this.dirpath, dpath)))
.filter(a => !a.startsWith('.') && (a.endsWith('.json') || a.endsWith('.js')));
}
/**
* list all the directories in a directory.
* @param {string} dpath - the directory path, that should be read
* @return {Promise} promise - return a promise
*/
async listdir(dpath) {
return (await pmReaddir(join(this.dirpath, dpath))).filter(a => a.indexOf('.') === -1);
}
/**
* read a file in a directory.
* @param {string} fpath - the file path, that should be read
* @return {Promise} promise - return a promise
*/
async read(fpath) {
let filejson = String(await pmReadFile(join(this.dirpath, fpath)));
try {
filejson = JSON.parse(filejson);
} catch (er) {
// not a json
}
this.emit('read', fpath, filejson);
return filejson;
}
/**
* create or update a file in a directory.
* @param {string} fpath - the file path, that should be created/updated
* @param {object} data - the file content in json format
* @return {Promise} promise - return a promise
*/
write(fpath, data) {
this.emit('write', fpath, data);
return new Promise((res, rej) => {
try {
const written = writeFileSync(join(this.dirpath, fpath),
fpath.endsWith('.json') ? JSON.stringify(data, undefined, 2) : data);
res(written);
} catch (er) {
rej(er);
}
});
}
/**
* create if a file does not exists at a path
* @param {string} fpath - the file path, that should be created/updated
* @param {object} data={} - the file content in json format
* @return {Promise} promise - return a promise
*/
async writep(fpath, data = {}) {
await this.mkdirp(dirname(fpath));
let st;
try {
st = await this.stat(fpath);
} catch (er) {
switch (er.code) {
case 'ENOENT':
try {
return this.write(fpath, data);
} catch (ert) {
return ert;
}
default:
return er;
}
}
if (st.isFile()) {
return 1;
}
return this.write(fpath, data);
}
/**
* rename a file in a directory.
* @param {string} fpath - the file path, that should be rename
* @param {string} fpath - the new file path
* @return {Promise} promise - return a promise
*/
rename(fpath, newpath) {
return pmRename(join(this.dirpath, fpath), join(this.dirpath, newpath));
}
/**
* delete a file in a directory.
* @param {string} fpath - the file path, that should be deleted
* @return {Promise} promise - return a promise
*/
del(fpath) {
this.emit('delete', fpath);
return pmUnlink(join(this.dirpath, fpath));
}
/**
* stat a path
* @param {string} fdpath - the file or directory path, that should be stated
* @return {Promise} promise - return a promise
*/
stat(fdpath) {
return pmStat(join(this.dirpath, fdpath));
}
/**
* create a dir.
* @param {string} dpath - the directory path, that should be created
* @return {Promise} promise - return a promise
*/
mkdir(dpath) {
return pmMkdir(join(this.dirpath, dpath));
}
/**
* remove a directory recursively
* @param {string} dpath - the directory path, that should be deleted
* @return {Promise} promise - return a promise
*/
async rmrdir(dpath) {
const stats = await this.stat(dpath);
if(stats.isFile()){
return this.del(dpath);
} else if(stats.isDirectory()){
await Promise.all((await pmReaddir(join(this.dirpath, dpath)))
.map(dpth => this.rmrdir(join(dpath, dpth))));
return this.rmdir(dpath);
} else {
return 1;
}
}
/**
* create a dir upto the path to create directories.
* @param {string} dpath - the directory path, that should be created
* @return {Promise} promise - return a promise
*/
async mkdirp(dpath) {
try {
await this.mkdir(dpath);
return 1;
} catch (er) {
switch (er.code) {
case 'ENOENT':
try {
await this.mkdirp(dirname(dpath));
return this.mkdirp(dpath);
} catch (ert) {
return ert;
}
default:
try {
const stt = await this.stat(dpath);
return stt.isDirectory() ? 1 : er;
} catch (er2) {
return er2;
}
}
}
}
/**
* delete a dir.
* @param {string} dpath - the directory path, that should be deleted
* @return {Promise} promise - return a promise
*/
rmdir(dpath) {
return pmRmdir(join(this.dirpath, dpath));
}
}
export default FileServer;