-
Notifications
You must be signed in to change notification settings - Fork 22
/
tsc.ts
31 lines (29 loc) · 831 Bytes
/
tsc.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
import {
readAll,
readAllSync,
} from "https://deno.land/std@0.175.0/streams/read_all.ts";
import { StringReader } from "https://deno.land/std@0.175.0/io/string_reader.ts";
export function replaceTsFileExtensionInImportStatement(
code: string,
extension: string,
): string {
if (extension === ".ts") return code;
return code.replaceAll(
/(^\s*(?:import|export|}\s*from)\b.+?)\.ts("|')/gm,
`$1${extension}$2`,
);
}
export async function replaceTsFileExtensionInImportStatementFromReader(
reader: Deno.Reader | Deno.ReaderSync,
extension: string,
): Promise<Deno.Reader> {
const data = "readSync" in reader
? readAllSync(reader)
: await readAll(reader);
return new StringReader(
replaceTsFileExtensionInImportStatement(
new TextDecoder().decode(data),
extension,
),
);
}