-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (99 loc) · 3.4 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
const fs = require('fs');
const path = require('path');
const fileActions = {};
/** *********************************************************
* readDir: Get a list of files in a given directory
* @param {string} dir
* @param {boool} flat whether to flatten array or not
* @returns {array}
* *********************************************************/
fileActions.readDir = (dir = __dirname, flat = false) => {
try {
return (
fs.statSync(dir).isDirectory() &&
fs.promises
.readdir(dir)
.then((files) =>
Promise.all(
files.reduce((list, file) => {
const name = path.join(dir, file);
const isDir = fs.statSync(name).isDirectory();
return list.concat(isDir ? fileActions.readDir(name) : [name]);
}, [])
)
)
.then((files) => (flat ? files.flat(Infinity) : files))
);
} catch (err) {
/* istanbul ignore else */
if (err.code === 'ENOTDIR' || err.code === 'ENOENT') {
console.error(`"${dir}" is not a directory`);
return;
}
/* istanbul ignore next */
throw err;
}
};
/** *********************************************************
* readFile: read contents of file
* @param {string} dir
* @returns {buffer} contents of the file
* *********************************************************/
fileActions.readFile = (dir) => {
try {
return fs.statSync(dir).isFile() && fs.promises.readFile(dir);
} catch (err) {
/* istanbul ignore else */
if (err.code === 'ENOENT') {
console.error(`"${dir}" is not a file`);
return;
}
/* istanbul ignore next */
throw err;
}
};
/** *********************************************************
* writeFile: write contents to a file
* @param {string} dir fule path including file name and ext.
* @param {buffer} data contents of the file
* @returns {string | boolean} file path if successful. Otherwise false
* *********************************************************/
fileActions.writeFile = (dir, data) =>
fs.promises.writeFile(dir, data).catch((err) => {
console.error(
`Something went wrong writing the file at ${dir}. `,
err.message
);
return false;
});
/** *********************************************************
* fileOfType: Get a list of files with a certain extension
* @param {array} files
* @param {string} ext extension type (ie. `js`, `html`)
* @returns {array}
* *********************************************************/
fileActions.fileOfType = (files, ext = 'js') =>
Array.isArray(files) &&
files.reduce((list, file) => {
let arrayFiles;
/* istanbul ignore else */
if (Array.isArray(file)) {
arrayFiles = fileActions.fileOfType(file);
return list.concat([arrayFiles]);
}
const hasExt = file.split('.').pop().toLowerCase() === ext.toLowerCase();
return hasExt ? list.concat(file) : list;
}, []);
// fileActions
// .readDir('./__test__/myDir')
// .then((e) => fileActions.fileOfType(e))
// .then((e) => console.log(e));
// fileActions.writeFile('./otherMarkdown.md', undefined);
// fileActions
// .readFile('__test__/myDir/myMarkdown.md')
// .then((files) => console.log(files));
// fileActions.readDir('./__test__/myDir').then((e) => console.log(e));
// .then(buff => fileActions.writeFile('./otherMarkdown.md', buff))
// .readDir('./', true)
// .then(files => fileActions.fileOfType(files))
module.exports = fileActions;