UNPKG

6.87 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 /**
104 * default
105 */
106 default:
107 return require(name);
108 }
109 }
110 catch (err) {
111 if (!path.isAbsolute(name) && name.substr(0, 2) !== "./" && name.substr(0, 3) !== "../") {
112 return require(path.resolve(process.cwd() + "/node_modules/" + name));
113 }
114 throw err;
115 }
116 };
117 /**
118 * Normalizes given path. Does "path.normalize".
119 */
120 PlatformTools.pathNormalize = function (pathStr) {
121 return path.normalize(pathStr);
122 };
123 /**
124 * Gets file extension. Does "path.extname".
125 */
126 PlatformTools.pathExtname = function (pathStr) {
127 return path.extname(pathStr);
128 };
129 /**
130 * Resolved given path. Does "path.resolve".
131 */
132 PlatformTools.pathResolve = function (pathStr) {
133 return path.resolve(pathStr);
134 };
135 /**
136 * Synchronously checks if file exist. Does "fs.existsSync".
137 */
138 PlatformTools.fileExist = function (pathStr) {
139 return fs.existsSync(pathStr);
140 };
141 PlatformTools.readFileSync = function (filename) {
142 return fs.readFileSync(filename);
143 };
144 PlatformTools.appendFileSync = function (filename, data) {
145 fs.appendFileSync(filename, data);
146 };
147 PlatformTools.writeFile = function (path, data) {
148 return tslib_1.__awaiter(this, void 0, void 0, function () {
149 return tslib_1.__generator(this, function (_a) {
150 return [2 /*return*/, new Promise(function (ok, fail) {
151 fs.writeFile(path, data, function (err) {
152 if (err)
153 fail(err);
154 ok();
155 });
156 })];
157 });
158 });
159 };
160 /**
161 * Gets environment variable.
162 */
163 PlatformTools.getEnvVariable = function (name) {
164 return process.env[name];
165 };
166 /**
167 * Highlights sql string to be print in the console.
168 */
169 PlatformTools.highlightSql = function (sql) {
170 var theme = {
171 "keyword": chalk.blueBright,
172 "literal": chalk.blueBright,
173 "string": chalk.white,
174 "type": chalk.magentaBright,
175 "built_in": chalk.magentaBright,
176 "comment": chalk.gray,
177 };
178 return cli_highlight_1.highlight(sql, { theme: theme, language: "sql" });
179 };
180 /**
181 * Highlights json string to be print in the console.
182 */
183 PlatformTools.highlightJson = function (json) {
184 return cli_highlight_1.highlight(json, { language: "json" });
185 };
186 /**
187 * Logging functions needed by AdvancedConsoleLogger
188 */
189 PlatformTools.logInfo = function (prefix, info) {
190 console.log(chalk.gray.underline(prefix), info);
191 };
192 PlatformTools.logError = function (prefix, error) {
193 console.log(chalk.underline.red(prefix), error);
194 };
195 PlatformTools.logWarn = function (prefix, warning) {
196 console.log(chalk.underline.yellow(prefix), warning);
197 };
198 PlatformTools.log = function (message) {
199 console.log(chalk.underline(message));
200 };
201 PlatformTools.warn = function (message) {
202 return chalk.yellow(message);
203 };
204 /**
205 * Type of the currently running platform.
206 */
207 PlatformTools.type = "node";
208 return PlatformTools;
209}());
210exports.PlatformTools = PlatformTools;
211
212//# sourceMappingURL=PlatformTools.js.map