-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
utils.ts
63 lines (49 loc) · 1.43 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import path from 'path';
import type { ModuleFormat } from 'module';
import {
getTsconfig,
parseTsconfig,
createPathsMatcher,
createFilesMatcher,
} from 'get-tsconfig';
import { getPackageType } from './package-json.js';
const tsconfig = (
process.env.TSX_TSCONFIG_PATH
? {
path: path.resolve(process.env.TSX_TSCONFIG_PATH),
config: parseTsconfig(process.env.TSX_TSCONFIG_PATH),
}
: getTsconfig()
);
export const fileMatcher = tsconfig && createFilesMatcher(tsconfig);
export const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);
export const allowJs = tsconfig?.config.compilerOptions?.allowJs ?? false;
export const fileProtocol = 'file://';
export const tsExtensionsPattern = /\.([cm]?ts|[tj]sx)($|\?)/;
export const isJsonPattern = /\.json(?:$|\?)/;
const getFormatFromExtension = (fileUrl: string): ModuleFormat | undefined => {
const extension = path.extname(fileUrl);
if (extension === '.json') {
return 'json';
}
if (extension === '.mjs' || extension === '.mts') {
return 'module';
}
if (extension === '.cjs' || extension === '.cts') {
return 'commonjs';
}
};
export const getFormatFromFileUrl = (fileUrl: string) => {
const format = getFormatFromExtension(fileUrl);
if (format) {
return format;
}
// ts, tsx, jsx
if (tsExtensionsPattern.test(fileUrl)) {
return getPackageType(fileUrl);
}
};
export type MaybePromise<T> = T | Promise<T>;
export type NodeError = Error & {
code: string;
};