forked from lgwebdream/fe-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput2File.js
More file actions
33 lines (30 loc) · 880 Bytes
/
output2File.js
File metadata and controls
33 lines (30 loc) · 880 Bytes
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
const fs = require('promise-fs');
const path = require('path');
const output2File = (dir, filename = '', data) =>
new Promise((resolve, reject) => {
const handleError = err => {
reject(err);
console.error(err);
};
const completeDir = path.join(dir, filename);
console.log(completeDir);
fs.readFile(completeDir, {
encoding: 'utf8',
})
.then(res => {
fs.writeFile(completeDir, `${res}\n${data}`)
.then(() => resolve())
.catch(handleError);
})
.catch(err => {
const { errno } = err;
// no directory or filename
if (errno === -2 || errno === -4058) {
fs.mkdirSync(dir, { recursive: true });
fs.writeFile(completeDir, data)
.then(() => resolve())
.catch(handleError);
}
});
});
module.exports = output2File;