• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

typeorm / typeorm / 14796576772

02 May 2025 01:52PM UTC coverage: 45.367% (-30.9%) from 76.309%
14796576772

Pull #11434

github

web-flow
Merge ec4ce2d00 into fadad1a74
Pull Request #11434: feat: release PR releases using pkg.pr.new

5216 of 12761 branches covered (40.87%)

Branch coverage included in aggregate %.

11439 of 23951 relevant lines covered (47.76%)

15712.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

51.76
/src/platform/PlatformTools.ts
1
import ansi from "ansis"
36✔
2
import dotenv from "dotenv"
36✔
3
import fs from "fs"
36✔
4
import path from "path"
36✔
5
import { highlight } from "sql-highlight"
36✔
6
import { format as sqlFormat } from "@sqltools/formatter"
36✔
7
import { type Config as SqlFormatterConfig } from "@sqltools/formatter/lib/core/types"
8
import { type DatabaseType } from "../driver/types/DatabaseType"
9

10
export { EventEmitter } from "events"
36✔
11
export { ReadStream } from "fs"
36✔
12
export { Readable, Writable } from "stream"
36✔
13

14
/**
15
 * Platform-specific tools.
16
 */
17
export class PlatformTools {
36✔
18
    /**
19
     * Type of the currently running platform.
20
     */
21
    static type: "browser" | "node" = "node"
36✔
22

23
    /**
24
     * Gets global variable where global stuff can be stored.
25
     */
26
    static getGlobalVariable(): any {
27
        return global
38,144✔
28
    }
29

30
    /**
31
     * Loads ("require"-s) given file or package.
32
     * This operation only supports on node platform
33
     */
34
    static load(name: string): any {
35
        // if name is not absolute or relative, then try to load package from the node_modules of the directory we are currently in
36
        // this is useful when we are using typeorm package globally installed and it accesses drivers
37
        // that are not installed globally
38

39
        try {
1,860✔
40
            // switch case to explicit require statements for webpack compatibility.
41
            switch (name) {
1,860!
42
                /**
43
                 * spanner
44
                 */
45
                case "spanner":
46
                    return require("@google-cloud/spanner")
×
47

48
                /**
49
                 * mongodb
50
                 */
51
                case "mongodb":
52
                    return require("mongodb")
8✔
53

54
                /**
55
                 * hana
56
                 */
57
                case "@sap/hana-client":
58
                    return require("@sap/hana-client")
×
59

60
                case "@sap/hana-client/extension/Stream":
61
                    return require("@sap/hana-client/extension/Stream")
×
62

63
                case "hdb-pool":
64
                    return require("hdb-pool")
×
65

66
                /**
67
                 * mysql
68
                 */
69
                case "mysql":
70
                    return require("mysql")
52✔
71

72
                case "mysql2":
73
                    return require("mysql2")
×
74

75
                /**
76
                 * oracle
77
                 */
78
                case "oracledb":
79
                    return require("oracledb")
440✔
80

81
                /**
82
                 * postgres
83
                 */
84
                case "pg":
85
                    return require("pg")
×
86

87
                case "pg-native":
88
                    return require("pg-native")
×
89

90
                case "pg-query-stream":
91
                    return require("pg-query-stream")
×
92

93
                case "typeorm-aurora-data-api-driver":
94
                    return require("typeorm-aurora-data-api-driver")
×
95

96
                /**
97
                 * redis
98
                 */
99
                case "redis":
100
                    return require("redis")
×
101

102
                case "ioredis":
103
                    return require("ioredis")
×
104

105
                /**
106
                 * better-sqlite3
107
                 */
108
                case "better-sqlite3":
109
                    return require("better-sqlite3")
457✔
110

111
                /**
112
                 * sqlite
113
                 */
114
                case "sqlite3":
115
                    return require("sqlite3")
485✔
116

117
                /**
118
                 * sql.js
119
                 */
120
                case "sql.js":
121
                    return require("sql.js")
418✔
122

123
                /**
124
                 * sqlserver
125
                 */
126
                case "mssql":
127
                    return require("mssql")
×
128

129
                /**
130
                 * react-native-sqlite
131
                 */
132
                case "react-native-sqlite-storage":
133
                    return require("react-native-sqlite-storage")
×
134
            }
135
        } catch (err) {
136
            return require(path.resolve(
×
137
                process.cwd() + "/node_modules/" + name,
138
            ))
139
        }
140

141
        // If nothing above matched and we get here, the package was not listed within PlatformTools
142
        // and is an Invalid Package.  To make it explicit that this is NOT the intended use case for
143
        // PlatformTools.load - it's not just a way to replace `require` all willy-nilly - let's throw
144
        // an error.
145
        throw new TypeError(`Invalid Package for PlatformTools.load: ${name}`)
×
146
    }
147

148
    /**
149
     * Normalizes given path. Does "path.normalize" and replaces backslashes with forward slashes on Windows.
150
     */
151
    static pathNormalize(pathStr: string): string {
152
        let normalizedPath = path.normalize(pathStr)
1,960✔
153
        if (process.platform === "win32")
1,960✔
154
            normalizedPath = normalizedPath.replace(/\\/g, "/")
1,490✔
155
        return normalizedPath
1,960✔
156
    }
157

158
    /**
159
     * Gets file extension. Does "path.extname".
160
     */
161
    static pathExtname(pathStr: string): string {
162
        return path.extname(pathStr)
7,637✔
163
    }
164

165
    /**
166
     * Resolved given path. Does "path.resolve".
167
     */
168
    static pathResolve(pathStr: string): string {
169
        return path.resolve(pathStr)
3,825✔
170
    }
171

172
    /**
173
     * Synchronously checks if file exist. Does "fs.existsSync".
174
     */
175
    static fileExist(pathStr: string): boolean {
176
        return fs.existsSync(pathStr)
263✔
177
    }
178

179
    static readFileSync(filename: string): Buffer {
180
        return fs.readFileSync(filename)
2✔
181
    }
182

183
    static appendFileSync(filename: string, data: any): void {
184
        fs.appendFileSync(filename, data)
179✔
185
    }
186

187
    static async writeFile(path: string, data: any): Promise<void> {
188
        return fs.promises.writeFile(path, data)
14✔
189
    }
190

191
    /**
192
     * Loads a dotenv file into the environment variables.
193
     *
194
     * @param path The file to load as a dotenv configuration
195
     */
196
    static dotenv(pathStr: string): void {
197
        dotenv.config({ path: pathStr })
12✔
198
    }
199

200
    /**
201
     * Gets environment variable.
202
     */
203
    static getEnvVariable(name: string): any {
204
        return process.env[name]
404✔
205
    }
206

207
    /**
208
     * Highlights sql string to be printed in the console.
209
     */
210
    static highlightSql(sql: string) {
211
        return highlight(sql, {
×
212
            colors: {
213
                keyword: ansi.blueBright.open,
214
                function: ansi.magentaBright.open,
215
                number: ansi.green.open,
216
                string: ansi.white.open,
217
                identifier: ansi.white.open,
218
                special: ansi.white.open,
219
                bracket: ansi.white.open,
220
                comment: ansi.gray.open,
221
                clear: ansi.reset.open,
222
            },
223
        })
224
    }
225

226
    /**
227
     * Pretty-print sql string to be print in the console.
228
     */
229
    static formatSql(sql: string, dataSourceType?: DatabaseType): string {
230
        const databaseLanguageMap: Record<
231
            string,
232
            SqlFormatterConfig["language"]
233
        > = {
12✔
234
            oracle: "pl/sql",
235
        }
236

237
        const databaseLanguage = dataSourceType
12!
238
            ? databaseLanguageMap[dataSourceType] || "sql"
×
239
            : "sql"
240

241
        return sqlFormat(sql, {
12✔
242
            language: databaseLanguage,
243
            indent: "    ",
244
        })
245
    }
246

247
    /**
248
     * Logging functions needed by AdvancedConsoleLogger
249
     */
250
    static logInfo(prefix: string, info: any) {
251
        console.log(ansi.gray.underline(prefix), info)
×
252
    }
253

254
    static logError(prefix: string, error: any) {
255
        console.log(ansi.underline.red(prefix), error)
×
256
    }
257

258
    static logWarn(prefix: string, warning: any) {
259
        console.log(ansi.underline.yellow(prefix), warning)
×
260
    }
261

262
    static log(message: string) {
263
        console.log(ansi.underline(message))
4✔
264
    }
265

266
    static info(info: any) {
267
        return ansi.gray(info)
×
268
    }
269

270
    static error(error: any) {
271
        return ansi.red(error)
×
272
    }
273

274
    static warn(message: string) {
275
        return ansi.yellow(message)
×
276
    }
277

278
    static logCmdErr(prefix: string, err?: any) {
279
        console.log(ansi.black.bgRed(prefix))
×
280
        if (err) console.error(err)
×
281
    }
282
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc