/
PlatformTools.ts
234 lines (192 loc) · 6.24 KB
/
PlatformTools.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import * as path from "path";
import * as fs from "fs";
import chalk from "chalk";
import {highlight, Theme} from "cli-highlight";
export {ReadStream} from "fs";
export {EventEmitter} from "events";
export {Readable, Writable} from "stream";
/**
* Platform-specific tools.
*/
export class PlatformTools {
/**
* Type of the currently running platform.
*/
static type: "browser"|"node" = "node";
/**
* Gets global variable where global stuff can be stored.
*/
static getGlobalVariable(): any {
return global;
}
/**
* Loads ("require"-s) given file or package.
* This operation only supports on node platform
*/
static load(name: string): any {
// if name is not absolute or relative, then try to load package from the node_modules of the directory we are currently in
// this is useful when we are using typeorm package globally installed and it accesses drivers
// that are not installed globally
try {
// switch case to explicit require statements for webpack compatibility.
switch (name) {
/**
* mongodb
*/
case "mongodb":
return require("mongodb");
/**
* hana
*/
case "@sap/hana-client":
return require("@sap/hana-client");
case "hdb-pool":
return require("hdb-pool");
/**
* mysql
*/
case "mysql":
return require("mysql");
case "mysql2":
return require("mysql2");
/**
* oracle
*/
case "oracledb":
return require("oracledb");
/**
* postgres
*/
case "pg":
return require("pg");
case "pg-native":
return require("pg-native");
case "pg-query-stream":
return require("pg-query-stream");
case "typeorm-aurora-data-api-driver":
return require("typeorm-aurora-data-api-driver");
/**
* redis
*/
case "redis":
return require("redis");
case "ioredis":
return require("ioredis");
/**
* better-sqlite3
*/
case "better-sqlite3":
return require("better-sqlite3");
/**
* sqlite
*/
case "sqlite3":
return require("sqlite3");
/**
* sql.js
*/
case "sql.js":
return require("sql.js");
/**
* sqlserver
*/
case "mssql":
return require("mssql");
/**
* react-native-sqlite
*/
case "react-native-sqlite-storage":
return require("react-native-sqlite-storage");
}
} catch (err) {
return require(path.resolve(process.cwd() + "/node_modules/" + name));
}
// If nothing above matched and we get here, the package was not listed within PlatformTools
// and is an Invalid Package. To make it explicit that this is NOT the intended use case for
// PlatformTools.load - it's not just a way to replace `require` all willy-nilly - let's throw
// an error.
throw new TypeError(`Invalid Package for PlatformTools.load: ${name}`);
}
/**
* Normalizes given path. Does "path.normalize".
*/
static pathNormalize(pathStr: string): string {
return path.normalize(pathStr);
}
/**
* Gets file extension. Does "path.extname".
*/
static pathExtname(pathStr: string): string {
return path.extname(pathStr);
}
/**
* Resolved given path. Does "path.resolve".
*/
static pathResolve(pathStr: string): string {
return path.resolve(pathStr);
}
/**
* Synchronously checks if file exist. Does "fs.existsSync".
*/
static fileExist(pathStr: string): boolean {
return fs.existsSync(pathStr);
}
static readFileSync(filename: string): Buffer {
return fs.readFileSync(filename);
}
static appendFileSync(filename: string, data: any): void {
fs.appendFileSync(filename, data);
}
static async writeFile(path: string, data: any): Promise<void> {
return new Promise<void>((ok, fail) => {
fs.writeFile(path, data, (err) => {
if (err) fail(err);
ok();
});
});
}
/**
* Gets environment variable.
*/
static getEnvVariable(name: string): any {
return process.env[name];
}
/**
* Highlights sql string to be print in the console.
*/
static highlightSql(sql: string) {
const theme: Theme = {
"keyword": chalk.blueBright,
"literal": chalk.blueBright,
"string": chalk.white,
"type": chalk.magentaBright,
"built_in": chalk.magentaBright,
"comment": chalk.gray,
};
return highlight(sql, { theme: theme, language: "sql" });
}
/**
* Highlights json string to be print in the console.
*/
static highlightJson(json: string) {
return highlight(json, { language: "json" });
}
/**
* Logging functions needed by AdvancedConsoleLogger
*/
static logInfo(prefix: string, info: any) {
console.log(chalk.gray.underline(prefix), info);
}
static logError(prefix: string, error: any) {
console.log(chalk.underline.red(prefix), error);
}
static logWarn(prefix: string, warning: any) {
console.log(chalk.underline.yellow(prefix), warning);
}
static log(message: string) {
console.log(chalk.underline(message));
}
static warn(message: string) {
return chalk.yellow(message);
}
}