-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmessage-handler.ts
More file actions
49 lines (43 loc) · 1.08 KB
/
message-handler.ts
File metadata and controls
49 lines (43 loc) · 1.08 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
import fs from 'fs';
import CLIError from './cli-error';
/**
* Message handler
*/
class Messages {
private messages: object;
messageFilePath: any;
constructor() {
this.messages = {};
}
init(context) {
if (!context.messageFilePath) {
return;
}
try {
this.messages = JSON.parse(fs.readFileSync(context.messageFilePath, 'utf-8'));
} catch (error) {
// create empty messages object if message file is not exist
if (error.code === 'ENOENT') {
this.messages = {};
} else {
throw new CLIError(`Error: ${error.message}`);
}
}
}
parse(messageKey: string, ...substitutions: Array<any>): string {
const msg = this.messages[messageKey];
if (!msg) {
return messageKey;
}
if (substitutions.length > 0) {
const callSite = msg.split('%s');
callSite.push('');
return String.raw({ raw: callSite } as TemplateStringsArray, ...substitutions);
}
return msg;
}
isEmptyMessages(): boolean {
return Object.keys(this.messages).length === 0;
}
}
export default new Messages();