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

horiuchi / dtsgenerator / 4311460381

pending completion
4311460381

push

github

GitHub
Merge pull request #552 from horiuchi/update-feature

673 of 775 branches covered (86.84%)

20 of 20 new or added lines in 3 files covered. (100.0%)

1115 of 1225 relevant lines covered (91.02%)

1508.83 hits per line

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

92.99
/src/utils.ts
1
import * as fs from 'fs';
1✔
2
import { URL } from 'url';
1✔
3
import glob, { GlobOptions } from 'glob';
1✔
4
import proxy from 'https-proxy-agent';
1✔
5
import { ScriptTarget } from 'typescript';
1✔
6
import { CommandOptions, defaultConfigFile } from './commandOptions';
1✔
7
import { PartialConfig } from './core/config';
8

9
export function readStream(
1✔
10
    stream: NodeJS.ReadableStream,
11
    encoding: BufferEncoding = 'utf8'
1✔
12
): Promise<string> {
13
    stream.setEncoding(encoding);
1✔
14
    return new Promise((resolve, reject) => {
1✔
15
        let data = '';
1✔
16
        stream.on('data', (chunk) => (data += chunk));
1✔
17
        stream.on('end', () => resolve(data));
1✔
18
        stream.on('error', (error) => reject(error));
1✔
19
    });
20
}
21

22
export async function readUrl(url: string): Promise<string> {
1✔
23
    const init = buildProxyOptions(url);
3✔
24
    const res = await fetch(url, init);
3✔
25
    const data = await res.text();
3✔
26
    if (!res.ok) {
3!
27
        throw new Error(
×
28
            `Error on fetch from url(${url}): ${res.status}, ${data}`
29
        );
30
    }
31
    return data;
3✔
32
}
33
function noProxy(url: URL): boolean {
34
    if (process.env.NO_PROXY) {
6✔
35
        for (const domain of process.env.NO_PROXY.split(/[, ]+/)) {
3✔
36
            if (url.hostname.endsWith(domain)) {
6✔
37
                return true;
1✔
38
            }
39
        }
40
    }
41
    return false;
5✔
42
}
43
export function buildProxyOptions(url: string): RequestInit | undefined {
1✔
44
    const parsedUrl = new URL(url);
6✔
45
    let proxyUrl;
46
    if (!noProxy(parsedUrl)) {
6✔
47
        if (parsedUrl.protocol === 'http:' && process.env.HTTP_PROXY) {
5✔
48
            proxyUrl = new URL(process.env.HTTP_PROXY);
1✔
49
        } else if (parsedUrl.protocol === 'https:' && process.env.HTTPS_PROXY) {
4✔
50
            proxyUrl = new URL(process.env.HTTPS_PROXY);
1✔
51
        }
52
    }
53
    if (proxyUrl) {
6✔
54
        const agentOptions: proxy.HttpsProxyAgentOptions = {};
2✔
55
        agentOptions.protocol = proxyUrl.protocol;
2✔
56
        agentOptions.host = proxyUrl.hostname;
2✔
57
        agentOptions.port = proxyUrl.port;
2✔
58
        if (proxyUrl.username) {
2!
59
            agentOptions.auth = proxyUrl.username + ':' + proxyUrl.password;
2✔
60
        }
61
        return { agent: proxy(agentOptions) } as RequestInit;
2✔
62
    }
63
    return undefined;
4✔
64
}
65

66
export async function globFiles(
1✔
67
    pattern: string,
68
    options?: GlobOptions
69
): Promise<string[]> {
70
    const res = await glob(pattern, options ?? {});
2✔
71
    return res.map((r) => {
1✔
72
        if (typeof r === 'string') {
4!
73
            return r;
4✔
74
        } else {
75
            return r.fullpath();
×
76
        }
77
    });
78
}
79

80
export function readConfig(options: CommandOptions): PartialConfig {
1✔
81
    let pc: PartialConfig = {};
16✔
82
    const configFile = options.configFile ?? defaultConfigFile;
16✔
83
    try {
16✔
84
        pc = loadJSON(configFile);
16✔
85
        pc.configFile = configFile;
2✔
86
    } catch (err) {
87
        if (options.configFile != null) {
14!
88
            console.error(
×
89
                'Error to load config file from ' + options.configFile
90
            );
91
        }
92
    }
93

94
    if (pc.input == null) {
16✔
95
        pc.input = {
14✔
96
            files: [],
97
            urls: [],
98
            stdin: false,
99
        };
100
    }
101
    if (options.files.length > 0) {
16✔
102
        pc.input.files = options.files;
1✔
103
    } else if (pc.input.files == null) {
15!
104
        pc.input.files = [];
×
105
    }
106
    if (options.urls.length > 0) {
16✔
107
        pc.input.urls = options.urls;
1✔
108
    } else if (pc.input.urls == null) {
15!
109
        pc.input.urls = [];
×
110
    }
111
    if (options.stdin != null) {
16✔
112
        pc.input.stdin = options.stdin;
1✔
113
    } else {
114
        pc.input.stdin =
15✔
115
            pc.input.stdin ||
42✔
116
            (pc.input.files.length === 0 && pc.input.urls.length === 0);
117
    }
118

119
    if (options.out != null) {
16✔
120
        pc.outputFile = options.out;
1✔
121
    }
122
    if (options.target != null) {
16✔
123
        pc.target = convertToScriptTarget(options.target);
12✔
124
    } else if (pc.target != null) {
4✔
125
        pc.target = convertToScriptTarget(pc.target as unknown as string);
2✔
126
    }
127
    pc.outputAST = !!options.outputAST;
16✔
128
    return pc;
16✔
129
}
130
function loadJSON(file: string): PartialConfig {
131
    const content = fs.readFileSync(file, 'utf-8');
16✔
132
    return JSON.parse(content) as PartialConfig;
2✔
133
}
134
function convertToScriptTarget(target: string): ScriptTarget {
135
    switch (target.trim().toLowerCase()) {
14✔
136
        case 'es3':
137
            return ScriptTarget.ES3;
1✔
138
        case 'es5':
139
            return ScriptTarget.ES5;
1✔
140
        case 'es2015':
141
            return ScriptTarget.ES2015;
1✔
142
        case 'es2016':
143
            return ScriptTarget.ES2016;
1✔
144
        case 'es2017':
145
            return ScriptTarget.ES2017;
1✔
146
        case 'es2018':
147
            return ScriptTarget.ES2018;
1✔
148
        case 'es2019':
149
            return ScriptTarget.ES2019;
1✔
150
        case 'es2020':
151
            return ScriptTarget.ES2020;
1✔
152
        case 'es2021':
153
            return ScriptTarget.ES2021;
1✔
154
        case 'es2022':
155
            return ScriptTarget.ES2022;
1✔
156
        case 'esnext':
157
            return ScriptTarget.ESNext;
3✔
158
        default:
159
            return ScriptTarget.Latest;
1✔
160
    }
161
}
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