This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ProcessUtils.ts
174 lines (149 loc) · 5.14 KB
/
ProcessUtils.ts
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
174
// Copyright (c) 2020 Sendanor. All rights reserved.
import FS from 'fs';
import PATH from 'path';
import { trim } from "./functions/trim";
import { LogService } from './LogService';
import { LogLevel } from "./types/LogLevel";
import { indexOf } from "./functions/indexOf";
import { trimStart } from "./functions/trimStart";
const LOG = LogService.createLogger('ProcessUtils');
export class ProcessUtils {
public static setLogLevel (level: LogLevel) {
LOG.setLogLevel(level);
}
public static getArguments () : string[] {
return process.argv.slice(2);
}
public static parseEnvFileLine (obj : Record<string, string>, input : string) : Record<string, string> {
if ( !input || !trimStart(input) ) return obj;
let key : string;
const equalIndex : number = indexOf(input, '=');
if (equalIndex < 0) {
key = trim(input);
if (key.length) {
return {
...obj,
[key]: ''
};
} else {
return obj;
}
} else {
key = input.substring(0, equalIndex);
key = trim(key);
}
if (equalIndex === input.length - 1) {
return {
...obj,
[input]: ''
};
}
let block : string;
if (input[equalIndex + 1] === '"') {
const equalIndexEnd : number = indexOf(input, '"', equalIndex + 2);
if (equalIndexEnd >= 0) {
block = input.substring(equalIndex + 2, equalIndexEnd);
return this.parseEnvFileLine(
{
...obj,
[key]: block
},
input.substring(equalIndexEnd+1)
);
} else {
throw new TypeError('ProcessUtils.parseEnvFileLine: No ending for double quote detected');
}
}
const lineEnd : number = indexOf(input, '\n', equalIndex);
if (lineEnd < 0) {
block = input.substring(equalIndex + 1).trim();
block = block.replace(/\\n/g, '\n');
return {
...obj,
[key]: block
};
} else {
block = input.substring(equalIndex + 1, lineEnd).trim();
block = block.replace(/\\n/g, '\n');
return this.parseEnvFileLine(
{
...obj,
[key]: block
},
input.substring(lineEnd+1)
);
}
}
public static parseEnvString (input : string) {
return ProcessUtils.parseEnvFileLine({}, input);
}
public static parseEnvFile (file: string) : Record<string, string> {
const input : string = FS.readFileSync(file, {encoding:"utf-8"});
return this.parseEnvString(input);
}
public static initEnvFromRecord (params: Record<string, string>) : void {
process.env = {
...params,
...process.env
};
}
public static initEnvFromFile (file: string) : void {
const params = ProcessUtils.parseEnvFile(file);
this.initEnvFromRecord(params);
}
public static initEnvFromDefaultFiles () : void {
const file = PATH.join(process.cwd(), '.env');
if (FS.existsSync(file)) {
ProcessUtils.initEnvFromFile(file);
}
}
/**
*
* @param callback
* @param errorHandler
*/
public static setupDestroyHandler (
callback : () => void,
errorHandler : (err : any) => void
) : void {
let destroyed = false;
const closeProcessInternal = () => {
try {
if (destroyed) return;
destroyed = true;
callback();
} catch (err) {
errorHandler(err);
}
};
const closeProcess = (reason: string) => {
return (err ?: any) => {
ProcessUtils._printErrors(reason, err);
closeProcessInternal();
};
};
process.on('exit', closeProcess('exit'));
process.on('SIGTERM', closeProcess('SIGTERM'));
process.on('SIGINT', closeProcess('SIGINT'));
process.on('SIGUSR1', closeProcess('SIGUSR1'));
process.on('SIGUSR2', closeProcess('SIGUSR2'));
process.on('uncaughtException', closeProcess('uncaughtException'));
}
private static _printErrors (reason: string, err ?: any) : void {
try {
if (reason === "exit") {
if (err) {
LOG.debug(`DEBUG: Closing process because "${reason}" event: `, err);
} else {
LOG.debug(`DEBUG: Closing process because "${reason}" event`);
}
} else if (err) {
LOG.error(`ERROR: Closing process because "${reason}" event: `, err);
} else {
LOG.info(`INFO: Closing process because "${reason}" event`);
}
} catch (err2) {
console.error('Error while printing errors: ', err2);
}
}
}