blinky/libs/util.js

41 lines
No EOL
1 KiB
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;
try {
path = await resolvePath(file);
} catch (err) {
throw err;
}
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
};