ninwa/libs/util.js

36 lines
No EOL
964 B
JavaScript

const realpath = require('fs/promises').realpath;
const stat = require('fs/promises').stat;
function fileExists(file) {
return new Promise((resolve, reject) => {
if (file == undefined) {
reject('can not check the existence of an undefined file');
}
resolvePath(file)
.then((path) => {
stat(path)
.then((stats) => {
resolve({path, stats});
})
})
.catch(reject);
});
}
function resolvePath(file) {
return new Promise((resolve, reject) => {
if (file == undefined) {
reject('can not resolve a path to an undefined file');
}
realpath(file)
.then(resolve)
.catch((err) => {
reject('resolving path \'' + file + '\' encountered an error >>> ' + err);
});
});
}
module.exports = {
fileExists,
resolvePath
}