2022-02-15 04:33:19 +01:00
|
|
|
const realpath = require('fs/promises').realpath;
|
|
|
|
const stat = require('fs/promises').stat;
|
|
|
|
|
|
|
|
function fileExists(file) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (file == undefined) {
|
2022-02-16 02:08:21 +01:00
|
|
|
reject('can not check the existence of an undefined file');
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
|
|
|
resolvePath(file)
|
|
|
|
.then((path) => {
|
|
|
|
stat(path)
|
|
|
|
.then((stats) => {
|
|
|
|
resolve({path, stats});
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolvePath(file) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (file == undefined) {
|
2022-02-16 02:08:21 +01:00
|
|
|
reject('can not resolve a path to an undefined file');
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
|
|
|
realpath(file)
|
|
|
|
.then(resolve)
|
|
|
|
.catch((err) => {
|
2022-02-16 02:08:21 +01:00
|
|
|
reject('resolving path \'' + file + '\' encountered an error >>> ' + err);
|
2022-02-15 04:33:19 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
fileExists,
|
|
|
|
resolvePath
|
|
|
|
}
|