-
Notifications
You must be signed in to change notification settings - Fork 1
/
processFile.js
64 lines (60 loc) · 2.01 KB
/
processFile.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
const logger = require("./logger");
const fs = require("fs");
readFile = function(fileName) {
return new Promise((resolve,reject) => {
// let normalizedPath = require("path").resolve(__dirname, '/sourceCollections/' + fileName);
let path = "./sourceCollections/" + fileName;
if (fs.lstatSync(path).isDirectory()) {
logger.trace("file path represnt drirectory", normalizedPath);
return reject("invalid file name");
}
console.log(path, "path");
fs.readFile(path, "utf8", function(err, fileContents) {
logger.trace("fs.readFile(): BEGIN");
if (err) return reject(err);
resolve(fileContents);
});
});
}
writeFile = function(outputFileName,derivedKeyAsString) {
let filepath = __dirname + '/sourceCollections/';
ensureExists(filepath, 0744)
.then(res => {
fs.writeFile('sourceCollections/' + outputFileName, derivedKeyAsString, "utf8", err => {
logger.trace("fs.writeFile(): BEGIN");
// If error occurred, log it (err will be passed to callback later)
if (err) {
console.log(err, 'err while writing to file')
} else {
logger.debug(
"fs.writeFile()Wrote derived key '" +
derivedKeyAsString +
"' to file: " +
outputFileName
);
return outputFileName;
}
logger.trace("fs.writeFile(): END");
});
}).catch(error => {
console.log(error,'errr')
})
};
function ensureExists(path, mask, cb) {
return new Promise((resolve, reject) => {
if (typeof mask == "function") {
// allow the `mask` parameter to be optional
cb = mask;
mask = 0777;
}
fs.mkdir(path, mask, function(err) {
if (err) {
if (err.code == "EEXIST") resolve(null);
// ignore the error if the folder already exists
else reject(err); // something else went wrong
} else resolve(null); // successfully created folder
});
});
}
module.exports.writeFile = writeFile;
module.exports.readFile = readFile;