blinky/libs/util.js

36 lines
No EOL
967 B
JavaScript

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