blinky/libs/util.js

36 lines
967 B
JavaScript
Raw Normal View History

2022-02-21 00:48:29 +01:00
const realpath = require('fs').realpath;
const stat = require('fs').stat;
2022-02-24 10:31:01 +01:00
async function fileExists(file) {
if (file === undefined) {
throw new Error('can not check the existence of an undefined file');
}
let path = await resolvePath(file);
2022-02-24 10:31:01 +01:00
return await new Promise((resolve, reject) => {
stat(path, (err, stats) => {
if (err) {
reject(err);
}
resolve({path, stats});
})
2022-02-21 00:48:29 +01:00
});
}
2022-02-24 10:31:01 +01:00
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) => {
2022-02-21 00:48:29 +01:00
realpath(file, (err, resolvedPath) => {
if (err) {
2022-02-24 10:42:06 +01:00
reject(new Error('resolving path \'' + file + '\' encountered an error >>> ' + err));
2022-02-21 00:48:29 +01:00
}
resolve(resolvedPath);
})
});
}
module.exports = {
fileExists,
resolvePath
2022-02-21 11:56:49 +01:00
};