This repository was archived by the owner on Nov 21, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
272 lines (243 loc) · 6.9 KB
/
index.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
'use strict';
/**
* @module file-utils-easy
* @typicalname fue
*/
const fs = require('fs');
const https = require('https');
const http = require('http');
const path = require('path');
/**
* Write a string to a file
* @param {string} fileContent the payload of the file
* @param {string} filePath path and filename: where store the file
* @returns {Promise<string>} resolve with the filePath received in input
*/
function writeToFile(fileContent, filePath) {
return new Promise((resolve, reject) => {
fs.writeFile(filePath, fileContent, 'utf8', (err) => {
if (err) {
reject(err);
return;
}
resolve(filePath);
});
});
}
/**
* Append a string to a file, if the file doesn't exist it is created
* @param {string} fileContent the payload of the file
* @param {string} filePath path and filename: where store the file
* @returns {Promise<string>} resolve with the filePath received in input
*/
function appendToFile(fileContent, filePath) {
return new Promise((resolve, reject) => {
fs.appendFile(filePath, fileContent, 'utf8', (err) => {
if (err) {
reject(err);
} else {
resolve(filePath);
}
});
});
}
/**
* Write a stream to a file
* @param {stream} fileStream the stream payload
* @param {string} filePath path and filename: where store the file
* @returns {Promise} resolve with the filePath when the stream finish
*/
function writeToFileStream(fileStream, filePath) {
return new Promise((resolve, reject) => {
const writerStream = fs.createWriteStream(filePath)
.on('finish', () => resolve(filePath))
.on('error', err => reject(err));
fileStream.pipe(writerStream);
});
}
/**
* Read the metadata of the file
* @param {string} filePath path and filename: the file to read
* @return {Promise<fs.Stats>} a node fs.Stats that provides information about a file
* @see https://nodejs.org/api/fs.html#fs_class_fs_stats
*/
function readFileStats(filePath) {
return new Promise((resolve, reject) => {
fs.stat(filePath, (err, fstat) => {
if (err) {
reject(err);
return;
}
resolve(fstat);
});
});
}
/**
* List the files names of a directory, ignoring directories
* @param {string} directory path of the directory to read
* @returns {Promise<array>} strings names of the files in the input directory
*/
function readDirectoryFiles(directory) {
return new Promise((resolve, reject) => {
fs.readdir(directory, (err, files) => {
if (err) {
reject(err);
return;
}
const isFile = fileName => readFileStats(path.join(directory, fileName))
.then((fstat) => {
if (fstat.isFile()) {
return fileName;
}
return null;
});
Promise.all(files.map(isFile))
.then(fileList => resolve(fileList.filter(f => f !== null)))
.catch(reject);
});
});
}
/**
* Read the content of a file
* @param {string} filePath path and filename: the file to read
* @param {string} [encoding='utf8'] the encoding file
* @returns {Promise<string>} resolve with the string content of the file
*/
function readFile(filePath, encoding = 'utf8') {
return new Promise((resolve, reject) => {
fs.readFile(filePath, encoding, (err, data) => {
if (err) {
reject(err);
return;
}
resolve(data);
});
});
}
/**
* Read the content of a file as a UTF8 string and then parse it as a JSON
* @param {string} filePath path and filename: the file to read
* @returns {Promise<object>} resolve with the JSON content of the file
*/
function readJsonFile(filePath) {
return readFile(filePath).then(content => JSON.parse(content));
}
/**
* Save the content of a url to a file
* @param {string} url where will be done an HTTP/GET to get the content
* @param {string} filePath path and filename where store the output of url
* @returns {Promise<string>} resolve with the filePath saved
*/
function saveUrlToFile(url, filePath) {
return new Promise((resolve, reject) => {
const outFile = fs.createWriteStream(filePath);
const protocol = url.startsWith('https') ? https : http;
protocol.get(url, (response) => {
response.pipe(outFile);
response.on('end', () => resolve(filePath));
response.on('error', (err) => {
// outFile.destroy();
// fs.unlinkSync(filePath);
reject(err);
});
})
.on('error', (err) => {
reject(err);
});
});
}
/**
* Delete a file from the file system
* @param {string} filePath path and filename: the file to delete
* @returns {Promise<string>} resolve with the filePath deleted
*/
function deleteFile(filePath) {
return new Promise((resolve, reject) => {
fs.unlink(filePath, (err) => {
if (err) {
reject(err);
return;
}
resolve(filePath);
});
});
}
/**
* Delete all the files in a directory, applying an optional filter
* @param {string} directory path of the directory to clean
* @returns {Promise<array>} resolve with all the files deleted succesfully
*/
function deleteDirectoryFiles(directory, filter = () => true) {
return readDirectoryFiles(directory)
.then((files) => {
const quietDelete = f => deleteFile(path.join(directory, f)).catch(() => null);
const deletingFiles = files.filter(filter).map(quietDelete);
return Promise.all(deletingFiles).then(deleted => deleted.filter(d => d !== null));
});
}
/**
* Rename a file to another path
* @param {string} from origin path and filename
* @param {string} to destination path and filename
* @returns {Promise<string>} resolve with the destination filePath
*/
function renameFile(from, to) {
return new Promise((resolve, reject) => {
fs.rename(from, to, (err) => {
if (err) {
reject(err);
return;
}
resolve(to);
});
});
}
/**
* Copy a file to another path
* @param {string} from origin path and filename
* @param {string} to destination path and filename
* @returns {Promise<string>} resolve with the destination filePath
*/
function copyFile(from, to) {
return new Promise((resolve, reject) => {
fs.copyFile(from, to, (err) => {
if (err) {
reject(err);
return;
}
resolve(to);
});
});
}
/**
* Check if a file exists
* @param {string} filePath path and filename: the file to control
* @returns {Promise<string>} resolve with the filePath that exists
* @throws {error} if the file doesn't exist
*/
function existFile(filePath, mode = fs.constants.W_OK) {
return new Promise((resolve, reject) => {
fs.access(filePath, mode, (err) => {
if (err) {
reject(err);
return;
}
resolve(filePath);
});
});
}
module.exports = Object.freeze({
writeToFile,
appendToFile,
writeToFileStream,
readDirectoryFiles,
readFile,
readJsonFile,
saveUrlToFile,
deleteFile,
deleteDirectoryFiles,
readFileStats,
existFile,
renameFile,
copyFile,
});