-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathglobals.js
173 lines (150 loc) · 6.06 KB
/
globals.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* eslint-disable no-console */
const winston = require('winston');
require('winston-daily-rotate-file');
const upath = require('upath');
const { Command, Option } = require('commander');
const fs = require('fs-extra');
// Get app version from package.json file
const appVersion = require('./package.json').version;
function checkFileExistsSync(filepath) {
let flag = true;
try {
fs.accessSync(filepath, fs.constants.F_OK);
} catch (e) {
flag = false;
}
return flag;
}
// Command line parameters
const program = new Command();
program
.version(appVersion)
.name('butler-cw')
.description(
'Butler CW makes sure that the most important apps are always loaded in your Qlik Sense Enterprise on Windows environment.\nCW = Cache Warming, i.e. the process of proactively forcing Sense apps to be loaded into RAM memory.'
)
.option('-c, --config-file <file>', 'Path to config file', 'production.yaml')
.addOption(
new Option('-l, --log-level <level>', 'log level')
.choices(['error', 'warn', 'info', 'verbose', 'debug', 'silly'])
.default('info')
)
.option('-a, --app-config-file <file>', 'Path to config file with cache warming definitions');
// Parse command line params
program.parse(process.argv);
const options = program.opts();
// Is there a config file specified on the command line?
let configFileOption;
let configFileExpanded;
let configFilePath;
let configFileBasename;
let configFileExtension;
if (options.configFile && options.configFile.length > 0) {
configFileOption = options.configFile;
configFileExpanded = upath.resolve(options.configFile);
configFilePath = upath.dirname(configFileExpanded);
configFileExtension = upath.extname(configFileExpanded);
configFileBasename = upath.basename(configFileExpanded, configFileExtension);
console.log(`Config file option value: ${configFileExpanded}`);
console.log(`Config file, full path & file: ${configFileExpanded}`);
console.log(`Config file path: ${configFilePath}`);
console.log(`Config file name: ${configFileBasename}`);
console.log(`Config file extension: ${configFileExtension}`);
if (configFileExtension.toLowerCase() !== '.yaml') {
console.log('Error: Main config file extension must be yaml');
process.exit(1);
}
if (checkFileExistsSync(configFileExpanded)) {
process.env.NODE_CONFIG_DIR = configFilePath;
process.env.NODE_ENV = configFileBasename;
} else {
console.log(`Error: Specified config file "${configFileExpanded}" does not exist`);
process.exit(1);
}
}
// Load main config file
// eslint-disable-next-line import/order
const config = require('config');
// Is there an app cache warming config file specified on the command line?
let appConfigFileOption;
let appConfigFileExpanded;
let appConfigFilePath;
let appConfigFileBasename;
let appConfigFileExtension;
if (options.appConfigFile && options.appConfigFile.length > 0) {
appConfigFileOption = options.appConfigFile;
appConfigFileExpanded = upath.resolve(options.appConfigFile);
appConfigFilePath = upath.dirname(appConfigFileExpanded);
appConfigFileExtension = upath.extname(appConfigFileExpanded);
appConfigFileBasename = upath.basename(appConfigFileExpanded, appConfigFileExtension);
console.log(`App config file option value: ${appConfigFileExpanded}`);
console.log(`App config file, full path & file: ${appConfigFileExpanded}`);
console.log(`App config file path: ${appConfigFilePath}`);
console.log(`App config file name: ${appConfigFileBasename}`);
console.log(`App config file extension: ${appConfigFileExtension}`);
if (appConfigFileExtension.toLowerCase() !== '.yaml') {
console.log('Error: Cache warming config file extension must be yaml');
process.exit(1);
}
if (checkFileExistsSync(appConfigFileExpanded)) {
config.appConfig.diskConfigFile = appConfigFileExpanded;
} else {
console.log(`Error: Specified app config file "${appConfigFileExpanded}" does not exist`);
process.exit(1);
}
}
// Are we running as standalone app or not?
const isPkg = typeof process.pkg !== 'undefined';
if (isPkg && configFileOption === undefined) {
// Show help if running as standalone app and mandatory options (e.g. config file) are not specified
program.help({ error: true });
}
// Is there a log level file specified on the command line?
if (options.logLevel && options.logLevel.length > 0) {
config.logLevel = options.logLevel;
}
// Set up logger with timestamps and colors, and optional logging to disk file
const logTransports = [];
logTransports.push(
new winston.transports.Console({
name: 'console',
level: config.get('logLevel'),
format: winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.timestamp(),
winston.format.colorize(),
winston.format.simple(),
winston.format.printf((info) => `${info.timestamp} ${info.level}: ${info.message}`)
),
})
);
const execPath = isPkg ? upath.dirname(process.execPath) : __dirname;
if (config.get('fileLogging')) {
logTransports.push(
new winston.transports.DailyRotateFile({
// dirname: path.join(__dirname, config.get('logDirectory')),
dirname: upath.join(execPath, 'log'),
filename: 'butler-cw.%DATE%.log',
level: config.get('logLevel'),
datePattern: 'YYYY-MM-DD',
maxFiles: '30d',
})
);
}
const logger = winston.createLogger({
transports: logTransports,
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf((info) => `${info.timestamp} ${info.level}: ${info.message}`)
),
});
// Function to get current logging level
const getLoggingLevel = () => logTransports.find((transport) => transport.name === 'console').level;
// Are we running as standalone app or not?
logger.verbose(`Running as standalone app: ${isPkg}`);
module.exports = {
config,
logger,
getLoggingLevel,
appVersion,
};