UNPKG

7 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var path = require("path");
5var fs = require("fs");
6var cli_highlight_1 = require("cli-highlight");
7var fs_1 = require("fs");
8exports.ReadStream = fs_1.ReadStream;
9var events_1 = require("events");
10exports.EventEmitter = events_1.EventEmitter;
11var stream_1 = require("stream");
12exports.Readable = stream_1.Readable;
13exports.Writable = stream_1.Writable;
14var chalk = require("chalk");
15/**
16 * Platform-specific tools.
17 */
18var PlatformTools = /** @class */ (function () {
19 function PlatformTools() {
20 }
21 /**
22 * Gets global variable where global stuff can be stored.
23 */
24 PlatformTools.getGlobalVariable = function () {
25 return global;
26 };
27 /**
28 * Loads ("require"-s) given file or package.
29 * This operation only supports on node platform
30 */
31 PlatformTools.load = function (name) {
32 // if name is not absolute or relative, then try to load package from the node_modules of the directory we are currently in
33 // this is useful when we are using typeorm package globally installed and it accesses drivers
34 // that are not installed globally
35 try {
36 // switch case to explicit require statements for webpack compatibility.
37 switch (name) {
38 /**
39 * mongodb
40 */
41 case "mongodb":
42 return require("mongodb");
43 /**
44 * mysql
45 */
46 case "mysql":
47 return require("mysql");
48 case "mysql2":
49 return require("mysql2");
50 /**
51 * oracle
52 */
53 case "oracledb":
54 return require("oracledb");
55 /**
56 * postgres
57 */
58 case "pg":
59 return require("pg");
60 case "pg-native":
61 return require("pg-native");
62 case "pg-query-stream":
63 return require("pg-query-stream");
64 /**
65 * redis
66 */
67 case "redis":
68 return require("redis");
69 /**
70 * ioredis
71 */
72 case "ioredis":
73 case "ioredis/cluster":
74 return require("ioredis");
75 /**
76 * sqlite
77 */
78 case "sqlite3":
79 return require("sqlite3");
80 /**
81 * sql.js
82 */
83 case "sql.js":
84 return require("sql.js");
85 /**
86 * sqlserver
87 */
88 case "mssql":
89 return require("mssql");
90 /**
91 * other modules
92 */
93 case "mkdirp":
94 return require("mkdirp");
95 case "path":
96 return require("path");
97 case "debug":
98 return require("debug");
99 case "app-root-path":
100 return require("app-root-path");
101 case "glob":
102 return require("glob");
103 case "typeorm-aurora-data-api-driver":
104 return require("typeorm-aurora-data-api-driver");
105 /**
106 * default
107 */
108 default:
109 return require(name);
110 }
111 }
112 catch (err) {
113 if (!path.isAbsolute(name) && name.substr(0, 2) !== "./" && name.substr(0, 3) !== "../") {
114 return require(path.resolve(process.cwd() + "/node_modules/" + name));
115 }
116 throw err;
117 }
118 };
119 /**
120 * Normalizes given path. Does "path.normalize".
121 */
122 PlatformTools.pathNormalize = function (pathStr) {
123 return path.normalize(pathStr);
124 };
125 /**
126 * Gets file extension. Does "path.extname".
127 */
128 PlatformTools.pathExtname = function (pathStr) {
129 return path.extname(pathStr);
130 };
131 /**
132 * Resolved given path. Does "path.resolve".
133 */
134 PlatformTools.pathResolve = function (pathStr) {
135 return path.resolve(pathStr);
136 };
137 /**
138 * Synchronously checks if file exist. Does "fs.existsSync".
139 */
140 PlatformTools.fileExist = function (pathStr) {
141 return fs.existsSync(pathStr);
142 };
143 PlatformTools.readFileSync = function (filename) {
144 return fs.readFileSync(filename);
145 };
146 PlatformTools.appendFileSync = function (filename, data) {
147 fs.appendFileSync(filename, data);
148 };
149 PlatformTools.writeFile = function (path, data) {
150 return tslib_1.__awaiter(this, void 0, void 0, function () {
151 return tslib_1.__generator(this, function (_a) {
152 return [2 /*return*/, new Promise(function (ok, fail) {
153 fs.writeFile(path, data, function (err) {
154 if (err)
155 fail(err);
156 ok();
157 });
158 })];
159 });
160 });
161 };
162 /**
163 * Gets environment variable.
164 */
165 PlatformTools.getEnvVariable = function (name) {
166 return process.env[name];
167 };
168 /**
169 * Highlights sql string to be print in the console.
170 */
171 PlatformTools.highlightSql = function (sql) {
172 var theme = {
173 "keyword": chalk.blueBright,
174 "literal": chalk.blueBright,
175 "string": chalk.white,
176 "type": chalk.magentaBright,
177 "built_in": chalk.magentaBright,
178 "comment": chalk.gray,
179 };
180 return cli_highlight_1.highlight(sql, { theme: theme, language: "sql" });
181 };
182 /**
183 * Highlights json string to be print in the console.
184 */
185 PlatformTools.highlightJson = function (json) {
186 return cli_highlight_1.highlight(json, { language: "json" });
187 };
188 /**
189 * Logging functions needed by AdvancedConsoleLogger
190 */
191 PlatformTools.logInfo = function (prefix, info) {
192 console.log(chalk.gray.underline(prefix), info);
193 };
194 PlatformTools.logError = function (prefix, error) {
195 console.log(chalk.underline.red(prefix), error);
196 };
197 PlatformTools.logWarn = function (prefix, warning) {
198 console.log(chalk.underline.yellow(prefix), warning);
199 };
200 PlatformTools.log = function (message) {
201 console.log(chalk.underline(message));
202 };
203 PlatformTools.warn = function (message) {
204 return chalk.yellow(message);
205 };
206 /**
207 * Type of the currently running platform.
208 */
209 PlatformTools.type = "node";
210 return PlatformTools;
211}());
212exports.PlatformTools = PlatformTools;
213
214//# sourceMappingURL=PlatformTools.js.map