2019-03-09 03:31:51 +00:00
|
|
|
'use strict';
|
2019-08-04 23:38:45 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const winston_1 = require("winston");
|
|
|
|
const path_1 = require("path");
|
|
|
|
const fs_extra_1 = require("fs-extra");
|
|
|
|
const os_1 = require("os");
|
2019-03-09 03:31:51 +00:00
|
|
|
const logTime = 'MM/DD/YY-HH:mm:ss';
|
|
|
|
let transport;
|
2019-03-18 20:50:40 +00:00
|
|
|
/**
|
|
|
|
* Determine the location of the log file based on the operating system
|
|
|
|
* and return as an absolute string from os.homedir()
|
|
|
|
*
|
|
|
|
* @returns {string} Path to log file
|
|
|
|
**/
|
2019-03-09 03:31:51 +00:00
|
|
|
async function logFile() {
|
2019-08-04 23:38:45 +00:00
|
|
|
const homeDir = os_1.homedir();
|
2020-07-27 15:43:55 +00:00
|
|
|
const linuxDir = `/.mcopy/`;
|
2019-03-21 22:01:41 +00:00
|
|
|
const macDir = `/Library/Logs/mcopy/`;
|
|
|
|
const winDir = `/AppData/Roaming/mcopy/`;
|
2020-07-27 15:43:55 +00:00
|
|
|
let logPath = path_1.normalize(path_1.join(homeDir, linuxDir));
|
2019-08-04 23:38:45 +00:00
|
|
|
let dirExists;
|
2019-03-09 03:31:51 +00:00
|
|
|
if (process.platform === 'darwin') {
|
2020-07-27 15:43:55 +00:00
|
|
|
logPath = path_1.normalize(path_1.join(homeDir, macDir));
|
2019-03-09 03:31:51 +00:00
|
|
|
}
|
|
|
|
else if (process.platform === 'win32') {
|
2020-07-27 15:43:55 +00:00
|
|
|
logPath = path_1.normalize(path_1.join(homeDir, winDir));
|
2019-03-09 03:31:51 +00:00
|
|
|
}
|
2020-07-27 15:43:55 +00:00
|
|
|
console.log(logPath);
|
2019-08-04 23:38:45 +00:00
|
|
|
dirExists = await fs_extra_1.exists(logPath);
|
|
|
|
if (!dirExists) {
|
2020-04-24 18:33:58 +00:00
|
|
|
try {
|
|
|
|
await fs_extra_1.mkdir(logPath);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.error(`Error creating directory for mcopy log file, ${logPath}`);
|
|
|
|
}
|
2019-03-09 03:31:51 +00:00
|
|
|
}
|
2019-08-04 23:38:45 +00:00
|
|
|
return path_1.join(logPath, 'mcopy.log');
|
2019-02-24 13:13:47 +00:00
|
|
|
}
|
2019-03-18 20:50:40 +00:00
|
|
|
/**
|
|
|
|
* Create and return the logger transport based on settings determined in
|
|
|
|
* arguments object
|
|
|
|
*
|
|
|
|
* @param {object} arg Arguments from process
|
|
|
|
*
|
|
|
|
* @returns {object} Logger transport
|
|
|
|
**/
|
2019-03-09 03:31:51 +00:00
|
|
|
module.exports = async function (arg) {
|
2019-08-04 23:38:45 +00:00
|
|
|
let consoleFormat = {
|
|
|
|
colorize: true
|
|
|
|
};
|
|
|
|
let fileFormat = {
|
|
|
|
filename: await logFile(),
|
|
|
|
json: true
|
|
|
|
};
|
2019-03-21 19:54:55 +00:00
|
|
|
if (arg && arg.quiet) {
|
2019-03-09 03:31:51 +00:00
|
|
|
transport = {
|
2019-04-15 20:11:18 +00:00
|
|
|
info: function () { return false; },
|
|
|
|
warn: function () { return false; },
|
|
|
|
error: function () { return false; }
|
2019-03-09 03:31:51 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
2019-03-21 19:54:55 +00:00
|
|
|
if (arg && arg.label) {
|
2019-08-04 23:38:45 +00:00
|
|
|
consoleFormat.label = arg.label;
|
|
|
|
fileFormat.label = arg.label;
|
2019-03-21 19:54:55 +00:00
|
|
|
}
|
2019-12-18 21:05:58 +00:00
|
|
|
transport = winston_1.createLogger({
|
2020-01-14 16:51:25 +00:00
|
|
|
format: winston_1.format.combine(winston_1.format.label({ label: arg.label || 'mcopy' }), winston_1.format.timestamp({
|
|
|
|
format: 'YYYY-MM-DD HH:mm:ss'
|
|
|
|
}), winston_1.format.printf((info) => `${info.timestamp} [${info.label}] ${info.level}: ${info.message}` + (info.splat !== undefined ? `${info.splat}` : " "))),
|
2019-03-09 03:31:51 +00:00
|
|
|
transports: [
|
2019-08-04 23:38:45 +00:00
|
|
|
new (winston_1.transports.Console)(consoleFormat),
|
|
|
|
new (winston_1.transports.File)(fileFormat)
|
2019-03-09 03:31:51 +00:00
|
|
|
]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return transport;
|
|
|
|
};
|
|
|
|
//# sourceMappingURL=index.js.map
|