@geekmidas/cli 0.0.9 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/tsconfig.ts DELETED
@@ -1,132 +0,0 @@
1
- import ts from 'typescript';
2
- import { existsSync } from 'fs';
3
- import { join, dirname, resolve } from 'path';
4
-
5
- export interface TsConfigResult {
6
- configPath: string;
7
- config: ts.ParsedCommandLine;
8
- compilerOptions: ts.CompilerOptions;
9
- }
10
-
11
- /**
12
- * Find and parse the nearest tsconfig.json file
13
- */
14
- export function loadTsConfig(
15
- searchPath: string = process.cwd(),
16
- ): TsConfigResult | null {
17
- const configPath = ts.findConfigFile(searchPath, existsSync);
18
-
19
- if (!configPath) {
20
- return null;
21
- }
22
-
23
- const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
24
-
25
- if (configFile.error) {
26
- throw new Error(
27
- `Error reading tsconfig.json: ${ts.flattenDiagnosticMessageText(configFile.error.messageText, '\n')}`,
28
- );
29
- }
30
-
31
- const parsedConfig = ts.parseJsonConfigFileContent(
32
- configFile.config,
33
- ts.sys,
34
- dirname(configPath),
35
- );
36
-
37
- if (parsedConfig.errors.length > 0) {
38
- const errors = parsedConfig.errors
39
- .map((error) => ts.flattenDiagnosticMessageText(error.messageText, '\n'))
40
- .join('\n');
41
- throw new Error(`Error parsing tsconfig.json: ${errors}`);
42
- }
43
-
44
- return {
45
- configPath,
46
- config: parsedConfig,
47
- compilerOptions: parsedConfig.options,
48
- };
49
- }
50
-
51
- /**
52
- * Resolve a module path using TypeScript's module resolution
53
- */
54
- export function resolveModulePath(
55
- moduleName: string,
56
- containingFile: string,
57
- tsConfig?: TsConfigResult,
58
- ): string | null {
59
- const compilerOptions = tsConfig?.compilerOptions || {};
60
-
61
- const result = ts.resolveModuleName(
62
- moduleName,
63
- containingFile,
64
- compilerOptions,
65
- ts.sys,
66
- );
67
-
68
- if (result.resolvedModule) {
69
- return result.resolvedModule.resolvedFileName;
70
- }
71
-
72
- return null;
73
- }
74
-
75
- /**
76
- * Get the output path for a TypeScript file based on tsconfig
77
- */
78
- export function getOutputPath(
79
- filePath: string,
80
- tsConfig?: TsConfigResult,
81
- ): string {
82
- if (!tsConfig) {
83
- // Default: replace .ts with .js
84
- return filePath.replace(/\.ts$/, '.js');
85
- }
86
-
87
- const { compilerOptions, configPath } = tsConfig;
88
- const configDir = dirname(configPath);
89
-
90
- // Handle outDir option
91
- if (compilerOptions.outDir) {
92
- const rootDir = compilerOptions.rootDir || configDir;
93
- const relativePath = filePath.startsWith(rootDir)
94
- ? filePath.slice(rootDir.length)
95
- : filePath;
96
-
97
- const outputPath = join(compilerOptions.outDir, relativePath);
98
- return outputPath.replace(/\.ts$/, '.js');
99
- }
100
-
101
- // Default: in-place compilation
102
- return filePath.replace(/\.ts$/, '.js');
103
- }
104
-
105
- /**
106
- * Apply path mappings from tsconfig paths
107
- */
108
- export function resolvePathMapping(
109
- importPath: string,
110
- tsConfig?: TsConfigResult,
111
- ): string {
112
- if (!tsConfig || !tsConfig.compilerOptions.paths) {
113
- return importPath;
114
- }
115
-
116
- const { paths, baseUrl } = tsConfig.compilerOptions;
117
- const configDir = dirname(tsConfig.configPath);
118
- const resolvedBaseUrl = baseUrl ? resolve(configDir, baseUrl) : configDir;
119
-
120
- for (const [pattern, replacements] of Object.entries(paths)) {
121
- const regex = new RegExp('^' + pattern.replace('*', '(.*)') + '$');
122
- const match = importPath.match(regex);
123
-
124
- if (match && replacements.length > 0) {
125
- const replacement = replacements[0];
126
- const resolvedPath = replacement.replace('*', match[1] || '');
127
- return resolve(resolvedBaseUrl, resolvedPath);
128
- }
129
- }
130
-
131
- return importPath;
132
- }