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

typeorm / typeorm / 13573761448

27 Feb 2025 06:53PM UTC coverage: 72.376% (+0.007%) from 72.369%
13573761448

push

github

web-flow
refactor: use ansis instead of chalk (#11263)

8657 of 12650 branches covered (68.43%)

Branch coverage included in aggregate %.

35 of 52 new or added lines in 11 files covered. (67.31%)

4 existing lines in 3 files now uncovered.

17895 of 24036 relevant lines covered (74.45%)

144036.91 hits per line

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

62.34
/src/platform/PlatformTools.ts
1
import ansi from "ansis"
279✔
2
import dotenv from "dotenv"
279✔
3
import fs from "fs"
279✔
4
import path from "path"
279✔
5
import { highlight } from "sql-highlight"
279✔
6

7
export { EventEmitter } from "events"
279✔
8
export { ReadStream } from "fs"
279✔
9
export { Readable, Writable } from "stream"
279✔
10

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

20
    /**
21
     * Gets global variable where global stuff can be stored.
22
     */
23
    static getGlobalVariable(): any {
24
        return global
295,922✔
25
    }
26

27
    /**
28
     * Loads ("require"-s) given file or package.
29
     * This operation only supports on node platform
30
     */
31
    static load(name: string): any {
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

36
        try {
19,072✔
37
            // switch case to explicit require statements for webpack compatibility.
38
            switch (name) {
19,072!
39
                /**
40
                 * spanner
41
                 */
42
                case "spanner":
43
                    return require("@google-cloud/spanner")
×
44

45
                /**
46
                 * mongodb
47
                 */
48
                case "mongodb":
49
                    return require("mongodb")
392✔
50

51
                /**
52
                 * hana
53
                 */
54
                case "@sap/hana-client":
55
                    return require("@sap/hana-client")
×
56

57
                case "@sap/hana-client/extension/Stream":
58
                    return require("@sap/hana-client/extension/Stream")
×
59

60
                case "hdb-pool":
61
                    return require("hdb-pool")
×
62

63
                /**
64
                 * mysql
65
                 */
66
                case "mysql":
67
                    return require("mysql")
3,460✔
68

69
                case "mysql2":
70
                    return require("mysql2")
×
71

72
                /**
73
                 * oracle
74
                 */
75
                case "oracledb":
76
                    return require("oracledb")
874✔
77

78
                /**
79
                 * postgres
80
                 */
81
                case "pg":
82
                    return require("pg")
3,267✔
83

84
                case "pg-native":
85
                    return require("pg-native")
3,267✔
86

87
                case "pg-query-stream":
88
                    return require("pg-query-stream")
12✔
89

90
                case "typeorm-aurora-data-api-driver":
91
                    return require("typeorm-aurora-data-api-driver")
×
92

93
                /**
94
                 * redis
95
                 */
96
                case "redis":
97
                    return require("redis")
×
98

99
                case "ioredis":
100
                    return require("ioredis")
×
101

102
                /**
103
                 * better-sqlite3
104
                 */
105
                case "better-sqlite3":
106
                    return require("better-sqlite3")
1,356✔
107

108
                /**
109
                 * sqlite
110
                 */
111
                case "sqlite3":
112
                    return require("sqlite3")
960✔
113

114
                /**
115
                 * sql.js
116
                 */
117
                case "sql.js":
118
                    return require("sql.js")
1,245✔
119

120
                /**
121
                 * sqlserver
122
                 */
123
                case "mssql":
124
                    return require("mssql")
4,239✔
125

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

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

145
    /**
146
     * Normalizes given path. Does "path.normalize" and replaces backslashes with forward slashes on Windows.
147
     */
148
    static pathNormalize(pathStr: string): string {
149
        let normalizedPath = path.normalize(pathStr)
15,843✔
150
        if (process.platform === "win32")
15,843!
151
            normalizedPath = normalizedPath.replace(/\\/g, "/")
×
152
        return normalizedPath
15,843✔
153
    }
154

155
    /**
156
     * Gets file extension. Does "path.extname".
157
     */
158
    static pathExtname(pathStr: string): string {
159
        return path.extname(pathStr)
61,058✔
160
    }
161

162
    /**
163
     * Resolved given path. Does "path.resolve".
164
     */
165
    static pathResolve(pathStr: string): string {
166
        return path.resolve(pathStr)
30,620✔
167
    }
168

169
    /**
170
     * Synchronously checks if file exist. Does "fs.existsSync".
171
     */
172
    static fileExist(pathStr: string): boolean {
173
        return fs.existsSync(pathStr)
2,271✔
174
    }
175

176
    static readFileSync(filename: string): Buffer {
177
        return fs.readFileSync(filename)
6✔
178
    }
179

180
    static appendFileSync(filename: string, data: any): void {
181
        fs.appendFileSync(filename, data)
1,376✔
182
    }
183

184
    static async writeFile(path: string, data: any): Promise<void> {
185
        return fs.promises.writeFile(path, data)
42✔
186
    }
187

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

197
    /**
198
     * Gets environment variable.
199
     */
200
    static getEnvVariable(name: string): any {
201
        return process.env[name]
3,188✔
202
    }
203

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

223
    /**
224
     * Logging functions needed by AdvancedConsoleLogger
225
     */
226
    static logInfo(prefix: string, info: any) {
227
        console.log(ansi.gray.underline(prefix), info)
1,686✔
228
    }
229

230
    static logError(prefix: string, error: any) {
NEW
231
        console.log(ansi.underline.red(prefix), error)
×
232
    }
233

234
    static logWarn(prefix: string, warning: any) {
NEW
235
        console.log(ansi.underline.yellow(prefix), warning)
×
236
    }
237

238
    static log(message: string) {
239
        console.log(ansi.underline(message))
37✔
240
    }
241

242
    static info(info: any) {
NEW
243
        return ansi.gray(info)
×
244
    }
245

246
    static error(error: any) {
NEW
247
        return ansi.red(error)
×
248
    }
249

250
    static warn(message: string) {
NEW
251
        return ansi.yellow(message)
×
252
    }
253

254
    static logCmdErr(prefix: string, err?: any) {
NEW
255
        console.log(ansi.black.bgRed(prefix))
×
256
        if (err) console.error(err)
×
257
    }
258
}
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

© 2026 Coveralls, Inc