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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93import * as path from "node:path";
import * as ts from "typescript";
export function formatForLog(data: unknown) {
return JSON.stringify(data, null, 2).split("\n").join("\n ");
}
export function emojiLog(_emoji: string, content: string, level: "log" | "warn" | "error" = "log") {
console[level]("ยป " + content);
}
export function isSourceFile(filePath: string): boolean {
// Declaration files are not source files
if (filePath.endsWith(".d.ts") || filePath.endsWith(".d.mts") || filePath.endsWith(".d.cts")) {
return false;
}
// TypeScript source files
return (
filePath.endsWith(".ts") || filePath.endsWith(".mts") || filePath.endsWith(".cts") || filePath.endsWith(".tsx")
);
}
export function removeExtension(filePath: string): string {
return filePath.split(".").slice(0, -1).join(".") || filePath;
}
export function readTsconfig(tsconfigPath: string) {
// Read and parse tsconfig.json
const configPath = path.resolve(tsconfigPath);
const configDir = path.dirname(configPath);
const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
if (configFile.error) {
console.error(
"Error reading tsconfig.json:",
ts.formatDiagnostic(configFile.error, {
getCurrentDirectory: () => configDir,
getCanonicalFileName: (fileName) => fileName,
getNewLine: () => ts.sys.newLine,
})
);
process.exit(1);
}
// Parse the config with explicit base path
const parsedConfig = ts.parseJsonConfigFileContent(
configFile.config,
{
...ts.sys,
// Override getCurrentDirectory to use the tsconfig directory
getCurrentDirectory: () => configDir,
},
configDir
);
if (parsedConfig.errors.length > 0) {
emojiLog("โ", "Error parsing tsconfig.json:", "error");
for (const error of parsedConfig.errors) {
console.error(
ts.formatDiagnostic(error, {
getCurrentDirectory: () => configDir,
getCanonicalFileName: (fileName) => fileName,
getNewLine: () => ts.sys.newLine,
})
);
}
process.exit(1);
}
if (!parsedConfig.options) {
emojiLog("โ", "Error reading tsconfig.json#/compilerOptions", "error");
process.exit(1);
}
return parsedConfig.options!;
}
export const jsExtensions: Set<string> = new Set([".js", ".mjs", ".cjs", ".ts", ".mts", ".cts", ".tsx"]);
export function isAssetFile(filePath: string): boolean {
const ext = path.extname(filePath).toLowerCase();
if (ext === "") return false;
return !jsExtensions.has(ext);
}
export const toPosix = (p: string): string => p.replaceAll(path.sep, path.posix.sep);
export const relativePosix = (from: string, to: string): string => {
const relativePath = path.relative(from, to);
return toPosix(relativePath);
};