2019-03-09 03:31:29 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const { createLogger, format, transports } = require('winston')
|
2019-03-21 19:54:55 +00:00
|
|
|
const { combine, timestamp, label, colorize, simple } = format
|
2019-03-09 03:31:29 +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 : any
|
|
|
|
|
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:29 +00:00
|
|
|
async function logFile () {
|
2019-03-18 20:50:40 +00:00
|
|
|
|
|
|
|
const homeDir : string = os.homedir();
|
|
|
|
const linuxDir : string = `/.config/mcopy-cli/`;
|
|
|
|
const macDir : string = `/Library/Logs/mcopy-cli/`;
|
|
|
|
const winDir : string = `/AppData/Roaming/mcopy-cli/`;
|
|
|
|
let logPath : string = path.join(homeDir, linuxDir);
|
|
|
|
let exists : boolean;
|
|
|
|
|
2019-03-09 03:31:29 +00:00
|
|
|
if (process.platform === 'darwin') {
|
2019-03-18 20:50:40 +00:00
|
|
|
logPath = path.join(homeDir, macDir);
|
2019-03-09 03:31:29 +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:29 +00:00
|
|
|
}
|
2019-03-18 20:50:40 +00:00
|
|
|
exists = await fs.exists(logPath);
|
2019-03-09 03:31:29 +00:00
|
|
|
if (!exists) {
|
2019-03-18 20:50:40 +00:00
|
|
|
await fs.mkdir(logPath);
|
2019-03-09 03:31:29 +00:00
|
|
|
}
|
2019-03-18 20:50:40 +00:00
|
|
|
return path.join(logPath, 'mcopy-cli.log');
|
2019-03-09 03:31:29 +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:29 +00:00
|
|
|
module.exports = async function (arg : any) {
|
2019-03-21 19:54:55 +00:00
|
|
|
let format;
|
|
|
|
if (arg && arg.quiet) {
|
2019-03-09 03:31:29 +00:00
|
|
|
transport = {
|
|
|
|
info : function () {},
|
|
|
|
warn : function () {},
|
|
|
|
error : function () {}
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-21 19:54:55 +00:00
|
|
|
if (arg && arg.label) {
|
|
|
|
format = combine(
|
|
|
|
label({ label : arg.label }),
|
|
|
|
timestamp(),
|
|
|
|
colorize(),
|
|
|
|
simple()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
format = combine(
|
|
|
|
timestamp(),
|
|
|
|
colorize(),
|
|
|
|
simple()
|
|
|
|
);
|
|
|
|
}
|
2019-03-09 03:31:29 +00:00
|
|
|
transport = createLogger({
|
|
|
|
transports: [
|
|
|
|
new (transports.Console)({
|
2019-03-21 19:54:55 +00:00
|
|
|
format
|
2019-03-09 03:31:29 +00:00
|
|
|
}),
|
|
|
|
new (transports.File)({
|
|
|
|
filename: await logFile()
|
|
|
|
})
|
|
|
|
]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return transport
|
|
|
|
}
|