forked from fbacker/docker-opensubtitles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
113 lines (103 loc) · 2.69 KB
/
index.js
File metadata and controls
113 lines (103 loc) · 2.69 KB
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
import config from 'config';
import winston from 'winston';
import fs from 'fs';
import startup from './src/index.js';
global.config = null;
global.logger = null;
global.userToken = null;
global.languages = [];
global.openSubtitles = null;
/**
* Configure Logger for output
*/
const createLogger = () => new Promise((resolve) => {
const { combine, timestamp, printf } = winston.format;
const myFormat = printf((info) => {
const meta = info.meta ? `\n${JSON.stringify(info.meta, null, 4)}` : '';
return `${info.timestamp} [${info.label}] ${info.level}: ${info.message} ${meta}`;
});
const logfile = new Date()
.toISOString()
.split('T')
.map((value) => value.split('.')[0])
.join('_');
const logger = winston.createLogger({
level: config.logger.level,
format: combine(timestamp(), myFormat),
transports: [
new winston.transports.File({
filename: `logs/${logfile}_error.log`,
level: 'error',
tailable: true,
}),
new winston.transports.Console({
level: 'debug',
colorize: true,
prettyPrint: true,
}),
],
});
global.logger = logger;
logger.log({
level: 'info',
label: 'Startup',
message: 'Starting Open Subtitle Autogetter',
});
logger.log({
level: 'info',
label: 'Startup',
message: '---------------------------------',
});
resolve();
});
/**
* Read extra config to extend
*/
const readConfig = () => new Promise((resolve) => {
const { logger } = global;
let path = config.base.extend;
if (process.env.NODE_ENV !== 'production') {
path = './test-config/local.json';
}
fs.readFile(path, 'utf8', (err, content) => {
let c = { ...config };
if (err) {
logger.info('No extra config file to merge');
} else if (!err) {
// no extra settings
const e = JSON.parse(content);
c = config.util.extendDeep({}, config, e);
}
const oun = process.env.USERNAME && process.env.USERNAME !== '' ? process.env.USERNAME : null;
const oup = process.env.USERNAME && process.env.PASSWORD !== '' ? process.env.PASSWORD : null;
if (oun) c.opensubtitles.username = oun;
if (oup) c.opensubtitles.password = oup;
logger.log({
level: 'info',
label: 'Startup',
message: 'Loaded Settings',
meta: c,
});
global.config = c;
resolve();
});
});
createLogger()
.then(readConfig)
.then(() => {
startup();
})
.catch((err) => {
const { logger } = global;
if (logger) {
logger.log({
level: 'error',
label: 'Startup',
message: 'Failed to start',
meta: err,
});
} else {
console.error('Failed to start', err);
}
process.exit(1);
});