2022-02-28 01:53:13 +01:00
|
|
|
const logger = require('./logger.js');
|
2022-02-21 00:48:29 +01:00
|
|
|
const realpath = require('fs').realpath;
|
|
|
|
const stat = require('fs').stat;
|
|
|
|
|
2022-02-28 01:53:13 +01:00
|
|
|
function exit(err, code) {
|
|
|
|
if (code === undefined) {
|
|
|
|
code = 0;
|
|
|
|
if (err !== undefined) {
|
|
|
|
code = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
logger.error(err);
|
|
|
|
logger.error(global.appName + ' ' + global.appVersion + ' ended due to an error');
|
|
|
|
} else {
|
|
|
|
logger.info(global.appName + ' ' + global.appVersion + ' shutting down gracefully')
|
|
|
|
}
|
|
|
|
process.exit(code);
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
}
|
2022-02-24 10:50:31 +01:00
|
|
|
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);
|
|
|
|
}
|
2022-02-28 01:53:13 +01:00
|
|
|
resolve({ path, stats });
|
2022-02-24 10:31:01 +01:00
|
|
|
})
|
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);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-28 01:53:13 +01:00
|
|
|
async function sleep(milliseconds) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
|
|
|
}
|
|
|
|
|
2022-02-21 00:48:29 +01:00
|
|
|
module.exports = {
|
2022-02-28 01:53:13 +01:00
|
|
|
exit,
|
2022-02-21 00:48:29 +01:00
|
|
|
fileExists,
|
2022-02-28 01:53:13 +01:00
|
|
|
resolvePath,
|
|
|
|
sleep
|
2022-02-21 11:56:49 +01:00
|
|
|
};
|