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

typeorm / typeorm / 15219332477

23 May 2025 09:13PM UTC coverage: 17.216% (-59.1%) from 76.346%
15219332477

Pull #11332

github

naorpeled
cr comments - move if block
Pull Request #11332: feat: add new undefined and null behavior flags

1603 of 12759 branches covered (12.56%)

Branch coverage included in aggregate %.

0 of 31 new or added lines in 3 files covered. (0.0%)

14132 existing lines in 166 files now uncovered.

4731 of 24033 relevant lines covered (19.69%)

60.22 hits per line

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

32.73
/src/commands/CommandUtils.ts
1
import fs from "fs/promises"
9✔
2
import path from "path"
9✔
3
import { TypeORMError } from "../error"
9✔
4
import { DataSource } from "../data-source"
5
import { InstanceChecker } from "../util/InstanceChecker"
9✔
6
import { importOrRequireFile } from "../util/ImportUtils"
9✔
7

8
/**
9
 * Command line utils functions.
10
 */
11
export class CommandUtils {
9✔
12
    static async loadDataSource(
13
        dataSourceFilePath: string,
14
    ): Promise<DataSource> {
15
        let dataSourceFileExports
16
        try {
×
17
            ;[dataSourceFileExports] = await importOrRequireFile(
×
18
                dataSourceFilePath,
19
            )
20
        } catch (err) {
21
            throw new Error(
×
22
                `Unable to open file: "${dataSourceFilePath}". ${err.message}`,
23
            )
24
        }
25

26
        if (
×
27
            !dataSourceFileExports ||
×
28
            typeof dataSourceFileExports !== "object"
29
        ) {
30
            throw new Error(
×
31
                `Given data source file must contain export of a DataSource instance`,
32
            )
33
        }
34

35
        if (InstanceChecker.isDataSource(dataSourceFileExports)) {
×
36
            return dataSourceFileExports
×
37
        }
38

39
        const dataSourceExports = []
×
40
        for (const fileExportKey in dataSourceFileExports) {
×
41
            const fileExport = dataSourceFileExports[fileExportKey]
×
42
            // It is necessary to await here in case of the exported async value (Promise<DataSource>).
43
            // e.g. the DataSource is instantiated with an async factory in the source file
44
            // It is safe to await regardless of the export being async or not due to `awaits` definition:
45
            // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#return_value
46
            const awaitedFileExport = await fileExport
×
47
            if (InstanceChecker.isDataSource(awaitedFileExport)) {
×
48
                dataSourceExports.push(awaitedFileExport)
×
49
            }
50
        }
51

52
        if (dataSourceExports.length === 0) {
×
53
            throw new Error(
×
54
                `Given data source file must contain export of a DataSource instance`,
55
            )
56
        }
57
        if (dataSourceExports.length > 1) {
×
58
            throw new Error(
×
59
                `Given data source file must contain only one export of DataSource instance`,
60
            )
61
        }
62
        return dataSourceExports[0]
×
63
    }
64

65
    /**
66
     * Creates directories recursively.
67
     */
68
    static async createDirectories(directory: string): Promise<void> {
69
        await fs.mkdir(directory, { recursive: true })
72✔
70
    }
71

72
    /**
73
     * Creates a file with the given content in the given path.
74
     */
75
    static async createFile(
76
        filePath: string,
77
        content: string,
78
        override: boolean = true,
48✔
79
    ): Promise<void> {
80
        await CommandUtils.createDirectories(path.dirname(filePath))
64✔
81
        if (override === false && (await CommandUtils.fileExists(filePath))) {
64!
82
            return
×
83
        }
84
        await fs.writeFile(filePath, content)
64✔
85
    }
86

87
    /**
88
     * Reads everything from a given file and returns its content as a string.
89
     */
90
    static async readFile(filePath: string): Promise<string> {
91
        const file = await fs.readFile(filePath)
8✔
92

93
        return file.toString()
8✔
94
    }
95

96
    static async fileExists(filePath: string) {
97
        try {
16✔
98
            await fs.access(filePath, fs.constants.F_OK)
16✔
99
            return true
×
100
        } catch {
101
            return false
16✔
102
        }
103
    }
104

105
    /**
106
     * Gets migration timestamp and validates argument (if sent)
107
     */
108
    static getTimestamp(timestampOptionArgument: any): number {
UNCOV
109
        if (
×
110
            timestampOptionArgument &&
×
111
            (isNaN(timestampOptionArgument) || timestampOptionArgument < 0)
112
        ) {
113
            throw new TypeORMError(
×
114
                `timestamp option should be a non-negative number. received: ${timestampOptionArgument}`,
115
            )
116
        }
UNCOV
117
        return timestampOptionArgument
×
118
            ? new Date(Number(timestampOptionArgument)).getTime()
119
            : Date.now()
120
    }
121
}
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