2019-03-09 03:31:51 +00:00
|
|
|
'use strict';
|
|
|
|
const { createLogger, format, transports } = require('winston');
|
2019-03-21 22:01:41 +00:00
|
|
|
const { combine, timestamp, label, colorize, simple, json } = format;
|
2019-03-09 03:31:51 +00:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const os = require('os');
|
|
|
|
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-03-18 20:50:40 +00:00
|
|
|
const homeDir = os.homedir();
|
2019-03-21 22:01:41 +00:00
|
|
|
const linuxDir = `/.config/mcopy/`;
|
|
|
|
const macDir = `/Library/Logs/mcopy/`;
|
|
|
|
const winDir = `/AppData/Roaming/mcopy/`;
|
2019-03-18 20:50:40 +00:00
|
|
|
let logPath = path.join(homeDir, linuxDir);
|
2019-03-09 03:31:51 +00:00
|
|
|
let exists;
|
|
|
|
if (process.platform === 'darwin') {
|
2019-03-18 20:50:40 +00:00
|
|
|
logPath = path.join(homeDir, macDir);
|
2019-03-09 03:31:51 +00:00
|
|
|
}
|
|
|
|
else if (process.platform === 'win32') {
|
2019-03-18 20:50:40 +00:00
|
|
|
logPath = path.join(homeDir, winDir);
|
2019-03-09 03:31:51 +00:00
|
|
|
}
|
|
|
|
exists = await fs.exists(logPath);
|
|
|
|
if (!exists) {
|
|
|
|
await fs.mkdir(logPath);
|
|
|
|
}
|
2019-03-21 22:01:41 +00:00
|
|
|
return path.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-03-21 19:54:55 +00:00
|
|
|
let format;
|
|
|
|
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-03-21 22:01:41 +00:00
|
|
|
format = combine(label({ label: arg.label }),
|
|
|
|
//timestamp(),
|
|
|
|
colorize(), simple());
|
2019-03-21 19:54:55 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-03-21 22:01:41 +00:00
|
|
|
format = combine(
|
|
|
|
//timestamp(),
|
|
|
|
colorize(), simple());
|
2019-03-21 19:54:55 +00:00
|
|
|
}
|
2019-03-09 03:31:51 +00:00
|
|
|
transport = createLogger({
|
|
|
|
transports: [
|
|
|
|
new (transports.Console)({
|
2019-03-21 19:54:55 +00:00
|
|
|
format
|
2019-03-09 03:31:51 +00:00
|
|
|
}),
|
|
|
|
new (transports.File)({
|
2019-03-21 22:01:41 +00:00
|
|
|
filename: await logFile(),
|
|
|
|
format: combine(label({ label: arg.label }),
|
|
|
|
//timestamp(),
|
|
|
|
json())
|
2019-03-09 03:31:51 +00:00
|
|
|
})
|
|
|
|
]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return transport;
|
|
|
|
};
|
|
|
|
//# sourceMappingURL=index.js.map
|