39 lines
No EOL
1 KiB
JavaScript
39 lines
No EOL
1 KiB
JavaScript
const realpath = require('fs').realpath;
|
|
const stat = require('fs').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, (err, stats) => {
|
|
if (err) {
|
|
reject(err);
|
|
}
|
|
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, (err, resolvedPath) => {
|
|
if (err) {
|
|
reject('resolving path \'' + file + '\' encountered an error >>> ' + err);
|
|
}
|
|
resolve(resolvedPath);
|
|
})
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
fileExists,
|
|
resolvePath
|
|
}; |