@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/README.md +33 -0
- package/dist/{build-DGJAXiy_.cjs → build-C-UDqpkV.cjs} +15 -19
- package/dist/{build-CE77jmn4.mjs → build-DCAjSjA0.mjs} +16 -20
- package/dist/build.cjs +2 -4
- package/dist/build.mjs +2 -4
- package/dist/{cli-CTfdNl5c.mjs → cli-DPcRbYh5.mjs} +43 -3
- package/dist/{cli-aXPCpkGS.cjs → cli-NvCqdZtD.cjs} +43 -3
- package/dist/cli.cjs +4 -6
- package/dist/cli.mjs +4 -6
- package/dist/config-D8AyiwBU.cjs +23 -0
- package/dist/config-DV1Lwdkx.mjs +17 -0
- package/dist/config.cjs +1 -1
- package/dist/config.mjs +1 -1
- package/dist/index.cjs +4 -6
- package/dist/index.mjs +4 -6
- package/dist/{openapi-C84tyelp.cjs → openapi-BX7ba0w0.cjs} +1 -1
- package/dist/{openapi-UIMy3Lzi.mjs → openapi-BxI6zE0N.mjs} +1 -1
- package/dist/openapi.cjs +2 -2
- package/dist/openapi.mjs +2 -2
- package/package.json +3 -4
- package/src/build.ts +10 -46
- package/src/cli.ts +2 -1
- package/src/config.ts +5 -27
- package/src/types.ts +0 -1
- package/dist/config-D-b45V57.cjs +0 -41
- package/dist/config-Nd751N3o.mjs +0 -35
- package/dist/helpers/pathResolver.cjs +0 -6
- package/dist/helpers/pathResolver.mjs +0 -4
- package/dist/pathResolver-B6y4yoWk.cjs +0 -47
- package/dist/pathResolver-DaMnbf26.mjs +0 -29
- package/dist/tsconfig-83amrDAp.cjs +0 -94
- package/dist/tsconfig-BtO228Cz.mjs +0 -70
- package/dist/tsconfig.cjs +0 -6
- package/dist/tsconfig.mjs +0 -3
- package/src/helpers/pathResolver.ts +0 -46
- package/src/tsconfig.ts +0 -132
package/src/build.ts
CHANGED
|
@@ -2,8 +2,6 @@ import { mkdir, writeFile } from 'node:fs/promises';
|
|
|
2
2
|
import { dirname, join, relative } from 'path';
|
|
3
3
|
import { loadConfig } from './config.js';
|
|
4
4
|
import { loadEndpoints } from './loadEndpoints.js';
|
|
5
|
-
import { loadTsConfig, getOutputPath } from './tsconfig.js';
|
|
6
|
-
import { resolveModuleSpecifier } from './helpers/pathResolver.js';
|
|
7
5
|
import type {
|
|
8
6
|
BuildOptions,
|
|
9
7
|
Provider,
|
|
@@ -19,14 +17,6 @@ export async function buildCommand(options: BuildOptions): Promise<void> {
|
|
|
19
17
|
logger.log(`Loading routes from: ${config.routes}`);
|
|
20
18
|
logger.log(`Using envParser: ${config.envParser}`);
|
|
21
19
|
|
|
22
|
-
// Load tsconfig if available
|
|
23
|
-
const tsConfig = config.tsconfig
|
|
24
|
-
? loadTsConfig(config.tsconfig)
|
|
25
|
-
: loadTsConfig();
|
|
26
|
-
if (tsConfig) {
|
|
27
|
-
logger.log(`Using TypeScript config from: ${tsConfig.configPath}`);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
20
|
// Parse envParser configuration
|
|
31
21
|
const [envParserPath, envParserName] = config.envParser.split('#');
|
|
32
22
|
const envParserImportPattern = !envParserName
|
|
@@ -53,7 +43,7 @@ export async function buildCommand(options: BuildOptions): Promise<void> {
|
|
|
53
43
|
|
|
54
44
|
const allEndpoints = loadedEndpoints.map(({ name, endpoint, file }) => {
|
|
55
45
|
const routeInfo: RouteInfo = {
|
|
56
|
-
path: endpoint.
|
|
46
|
+
path: endpoint._path,
|
|
57
47
|
method: endpoint.method,
|
|
58
48
|
handler: '', // Will be filled in later
|
|
59
49
|
};
|
|
@@ -90,7 +80,6 @@ export async function buildCommand(options: BuildOptions): Promise<void> {
|
|
|
90
80
|
envParserImportPattern,
|
|
91
81
|
loggerPath,
|
|
92
82
|
loggerImportPattern,
|
|
93
|
-
tsConfig,
|
|
94
83
|
);
|
|
95
84
|
|
|
96
85
|
routes.push({
|
|
@@ -111,7 +100,6 @@ export async function buildCommand(options: BuildOptions): Promise<void> {
|
|
|
111
100
|
routeInfo,
|
|
112
101
|
envParserPath,
|
|
113
102
|
envParserImportPattern,
|
|
114
|
-
tsConfig,
|
|
115
103
|
);
|
|
116
104
|
|
|
117
105
|
routes.push({
|
|
@@ -152,7 +140,6 @@ async function generateServerFile(
|
|
|
152
140
|
envParserImportPattern: string,
|
|
153
141
|
loggerPath: string,
|
|
154
142
|
loggerImportPattern: string,
|
|
155
|
-
tsConfig: ReturnType<typeof loadTsConfig>,
|
|
156
143
|
): Promise<string> {
|
|
157
144
|
const serverFileName = 'app.ts';
|
|
158
145
|
const serverPath = join(outputDir, serverFileName);
|
|
@@ -161,13 +148,8 @@ async function generateServerFile(
|
|
|
161
148
|
const importsByFile = new Map<string, string[]>();
|
|
162
149
|
|
|
163
150
|
for (const { file, exportName } of endpoints) {
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
const importPath = resolveModuleSpecifier(
|
|
167
|
-
serverPath,
|
|
168
|
-
absoluteFilePath,
|
|
169
|
-
tsConfig,
|
|
170
|
-
);
|
|
151
|
+
const relativePath = relative(dirname(serverPath), file);
|
|
152
|
+
const importPath = relativePath.replace(/\.ts$/, '.js');
|
|
171
153
|
|
|
172
154
|
if (!importsByFile.has(importPath)) {
|
|
173
155
|
importsByFile.set(importPath, []);
|
|
@@ -175,16 +157,8 @@ async function generateServerFile(
|
|
|
175
157
|
importsByFile.get(importPath)!.push(exportName);
|
|
176
158
|
}
|
|
177
159
|
|
|
178
|
-
const relativeEnvParserPath =
|
|
179
|
-
|
|
180
|
-
envParserPath,
|
|
181
|
-
tsConfig,
|
|
182
|
-
);
|
|
183
|
-
const relativeLoggerPath = resolveModuleSpecifier(
|
|
184
|
-
serverPath,
|
|
185
|
-
loggerPath,
|
|
186
|
-
tsConfig,
|
|
187
|
-
);
|
|
160
|
+
const relativeEnvParserPath = relative(dirname(serverPath), envParserPath);
|
|
161
|
+
const relativeLoggerPath = relative(dirname(serverPath), loggerPath);
|
|
188
162
|
|
|
189
163
|
// Generate import statements
|
|
190
164
|
const imports = Array.from(importsByFile.entries())
|
|
@@ -198,7 +172,7 @@ async function generateServerFile(
|
|
|
198
172
|
|
|
199
173
|
const content = `import { HonoEndpoint } from '@geekmidas/api/hono';
|
|
200
174
|
import { Endpoint } from '@geekmidas/api/server';
|
|
201
|
-
import {
|
|
175
|
+
import { ServiceDiscovery } from '@geekmidas/api/services';
|
|
202
176
|
import { Hono } from 'hono';
|
|
203
177
|
import ${envParserImportPattern} from '${relativeEnvParserPath}';
|
|
204
178
|
import ${loggerImportPattern} from '${relativeLoggerPath}';
|
|
@@ -211,7 +185,7 @@ export function createApp(app?: Hono): Hono {
|
|
|
211
185
|
${allExportNames.join(',\n ')}
|
|
212
186
|
];
|
|
213
187
|
|
|
214
|
-
const serviceDiscovery =
|
|
188
|
+
const serviceDiscovery = ServiceDiscovery.getInstance(
|
|
215
189
|
logger,
|
|
216
190
|
envParser
|
|
217
191
|
);
|
|
@@ -237,24 +211,14 @@ async function generateHandlerFile(
|
|
|
237
211
|
_routeInfo: RouteInfo,
|
|
238
212
|
envParserPath: string,
|
|
239
213
|
envParserImportPattern: string,
|
|
240
|
-
tsConfig: ReturnType<typeof loadTsConfig>,
|
|
241
214
|
): Promise<string> {
|
|
242
215
|
const handlerFileName = `${exportName}.ts`;
|
|
243
216
|
const handlerPath = join(outputDir, handlerFileName);
|
|
244
217
|
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
const importPath = resolveModuleSpecifier(
|
|
248
|
-
handlerPath,
|
|
249
|
-
absoluteSourcePath,
|
|
250
|
-
tsConfig,
|
|
251
|
-
);
|
|
218
|
+
const relativePath = relative(dirname(handlerPath), sourceFile);
|
|
219
|
+
const importPath = relativePath.replace(/\.ts$/, '.js');
|
|
252
220
|
|
|
253
|
-
const relativeEnvParserPath =
|
|
254
|
-
handlerPath,
|
|
255
|
-
envParserPath,
|
|
256
|
-
tsConfig,
|
|
257
|
-
);
|
|
221
|
+
const relativeEnvParserPath = relative(dirname(handlerPath), envParserPath);
|
|
258
222
|
|
|
259
223
|
let content: string;
|
|
260
224
|
|
package/src/cli.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import { Command } from 'commander';
|
|
4
|
+
import pkg from '../package.json' assert { type: 'json' };
|
|
4
5
|
import { buildCommand } from './build.js';
|
|
5
6
|
import { openapiCommand } from './openapi.js';
|
|
6
7
|
import type { Provider } from './types.js';
|
|
@@ -10,7 +11,7 @@ const program = new Command();
|
|
|
10
11
|
program
|
|
11
12
|
.name('gkm')
|
|
12
13
|
.description('GeekMidas backend framework CLI')
|
|
13
|
-
.version(
|
|
14
|
+
.version(pkg.version)
|
|
14
15
|
.option('--cwd <path>', 'Change working directory');
|
|
15
16
|
|
|
16
17
|
program
|
package/src/config.ts
CHANGED
|
@@ -1,44 +1,22 @@
|
|
|
1
1
|
import { existsSync } from 'fs';
|
|
2
|
-
import { join
|
|
2
|
+
import { join } from 'path';
|
|
3
3
|
import type { GkmConfig } from './types.js';
|
|
4
4
|
|
|
5
5
|
export async function loadConfig(): Promise<GkmConfig> {
|
|
6
|
-
const
|
|
6
|
+
const configPath = join(process.cwd(), 'gkm.config.json');
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
const configExtensions = ['.ts', '.js', '.json'];
|
|
10
|
-
let configPath: string | null = null;
|
|
11
|
-
|
|
12
|
-
for (const ext of configExtensions) {
|
|
13
|
-
const path = join(cwd, `gkm.config${ext}`);
|
|
14
|
-
if (existsSync(path)) {
|
|
15
|
-
configPath = path;
|
|
16
|
-
break;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (!configPath) {
|
|
8
|
+
if (!existsSync(configPath)) {
|
|
21
9
|
throw new Error(
|
|
22
|
-
'gkm.config.
|
|
10
|
+
'gkm.config.json not found. Please create a configuration file.',
|
|
23
11
|
);
|
|
24
12
|
}
|
|
25
13
|
|
|
26
14
|
try {
|
|
27
|
-
const ext = extname(configPath);
|
|
28
|
-
|
|
29
|
-
// For TypeScript files, ensure tsx can handle them
|
|
30
|
-
if (ext === '.ts') {
|
|
31
|
-
// tsx handles TypeScript files automatically
|
|
32
|
-
const config = await import(configPath);
|
|
33
|
-
return config.default || config;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// For JS and JSON files
|
|
37
15
|
const config = await import(configPath);
|
|
38
16
|
return config.default || config;
|
|
39
17
|
} catch (error) {
|
|
40
18
|
throw new Error(
|
|
41
|
-
`Failed to load
|
|
19
|
+
`Failed to load gkm.config.ts: ${(error as Error).message}`,
|
|
42
20
|
);
|
|
43
21
|
}
|
|
44
22
|
}
|
package/src/types.ts
CHANGED
package/dist/config-D-b45V57.cjs
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
-
const path = require_chunk.__toESM(require("path"));
|
|
3
|
-
const fs = require_chunk.__toESM(require("fs"));
|
|
4
|
-
|
|
5
|
-
//#region src/config.ts
|
|
6
|
-
async function loadConfig() {
|
|
7
|
-
const cwd = process.cwd();
|
|
8
|
-
const configExtensions = [
|
|
9
|
-
".ts",
|
|
10
|
-
".js",
|
|
11
|
-
".json"
|
|
12
|
-
];
|
|
13
|
-
let configPath = null;
|
|
14
|
-
for (const ext of configExtensions) {
|
|
15
|
-
const path$1 = (0, path.join)(cwd, `gkm.config${ext}`);
|
|
16
|
-
if ((0, fs.existsSync)(path$1)) {
|
|
17
|
-
configPath = path$1;
|
|
18
|
-
break;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
if (!configPath) throw new Error("gkm.config.{ts,js,json} not found. Please create a configuration file.");
|
|
22
|
-
try {
|
|
23
|
-
const ext = (0, path.extname)(configPath);
|
|
24
|
-
if (ext === ".ts") {
|
|
25
|
-
const config$1 = await import(configPath);
|
|
26
|
-
return config$1.default || config$1;
|
|
27
|
-
}
|
|
28
|
-
const config = await import(configPath);
|
|
29
|
-
return config.default || config;
|
|
30
|
-
} catch (error) {
|
|
31
|
-
throw new Error(`Failed to load ${configPath}: ${error.message}`);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
//#endregion
|
|
36
|
-
Object.defineProperty(exports, 'loadConfig', {
|
|
37
|
-
enumerable: true,
|
|
38
|
-
get: function () {
|
|
39
|
-
return loadConfig;
|
|
40
|
-
}
|
|
41
|
-
});
|
package/dist/config-Nd751N3o.mjs
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { extname, join } from "path";
|
|
2
|
-
import { existsSync } from "fs";
|
|
3
|
-
|
|
4
|
-
//#region src/config.ts
|
|
5
|
-
async function loadConfig() {
|
|
6
|
-
const cwd = process.cwd();
|
|
7
|
-
const configExtensions = [
|
|
8
|
-
".ts",
|
|
9
|
-
".js",
|
|
10
|
-
".json"
|
|
11
|
-
];
|
|
12
|
-
let configPath = null;
|
|
13
|
-
for (const ext of configExtensions) {
|
|
14
|
-
const path = join(cwd, `gkm.config${ext}`);
|
|
15
|
-
if (existsSync(path)) {
|
|
16
|
-
configPath = path;
|
|
17
|
-
break;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
if (!configPath) throw new Error("gkm.config.{ts,js,json} not found. Please create a configuration file.");
|
|
21
|
-
try {
|
|
22
|
-
const ext = extname(configPath);
|
|
23
|
-
if (ext === ".ts") {
|
|
24
|
-
const config$1 = await import(configPath);
|
|
25
|
-
return config$1.default || config$1;
|
|
26
|
-
}
|
|
27
|
-
const config = await import(configPath);
|
|
28
|
-
return config.default || config;
|
|
29
|
-
} catch (error) {
|
|
30
|
-
throw new Error(`Failed to load ${configPath}: ${error.message}`);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
//#endregion
|
|
35
|
-
export { loadConfig };
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
require('../tsconfig-83amrDAp.cjs');
|
|
2
|
-
const require_pathResolver = require('../pathResolver-B6y4yoWk.cjs');
|
|
3
|
-
|
|
4
|
-
exports.convertToJsExtension = require_pathResolver.convertToJsExtension;
|
|
5
|
-
exports.resolveImportPath = require_pathResolver.resolveImportPath;
|
|
6
|
-
exports.resolveModuleSpecifier = require_pathResolver.resolveModuleSpecifier;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
-
const require_tsconfig = require('./tsconfig-83amrDAp.cjs');
|
|
3
|
-
const path = require_chunk.__toESM(require("path"));
|
|
4
|
-
|
|
5
|
-
//#region src/helpers/pathResolver.ts
|
|
6
|
-
/**
|
|
7
|
-
* Resolve import paths considering TypeScript path mappings
|
|
8
|
-
*/
|
|
9
|
-
function resolveImportPath(fromFile, toFile, tsConfig) {
|
|
10
|
-
const resolvedToFile = tsConfig ? require_tsconfig.resolvePathMapping(toFile, tsConfig) : toFile;
|
|
11
|
-
const relativePath = (0, path.relative)((0, path.dirname)(fromFile), resolvedToFile);
|
|
12
|
-
if (!relativePath.startsWith(".")) return `./${relativePath}`;
|
|
13
|
-
return relativePath;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Convert TypeScript file extensions to JavaScript
|
|
17
|
-
*/
|
|
18
|
-
function convertToJsExtension(filePath) {
|
|
19
|
-
return filePath.replace(/\.ts$/, ".js");
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Resolve module specifier for imports
|
|
23
|
-
*/
|
|
24
|
-
function resolveModuleSpecifier(fromFile, toFile, tsConfig) {
|
|
25
|
-
const importPath = resolveImportPath(fromFile, toFile, tsConfig);
|
|
26
|
-
return convertToJsExtension(importPath);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
//#endregion
|
|
30
|
-
Object.defineProperty(exports, 'convertToJsExtension', {
|
|
31
|
-
enumerable: true,
|
|
32
|
-
get: function () {
|
|
33
|
-
return convertToJsExtension;
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
Object.defineProperty(exports, 'resolveImportPath', {
|
|
37
|
-
enumerable: true,
|
|
38
|
-
get: function () {
|
|
39
|
-
return resolveImportPath;
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
Object.defineProperty(exports, 'resolveModuleSpecifier', {
|
|
43
|
-
enumerable: true,
|
|
44
|
-
get: function () {
|
|
45
|
-
return resolveModuleSpecifier;
|
|
46
|
-
}
|
|
47
|
-
});
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { resolvePathMapping } from "./tsconfig-BtO228Cz.mjs";
|
|
2
|
-
import { dirname, relative } from "path";
|
|
3
|
-
|
|
4
|
-
//#region src/helpers/pathResolver.ts
|
|
5
|
-
/**
|
|
6
|
-
* Resolve import paths considering TypeScript path mappings
|
|
7
|
-
*/
|
|
8
|
-
function resolveImportPath(fromFile, toFile, tsConfig) {
|
|
9
|
-
const resolvedToFile = tsConfig ? resolvePathMapping(toFile, tsConfig) : toFile;
|
|
10
|
-
const relativePath = relative(dirname(fromFile), resolvedToFile);
|
|
11
|
-
if (!relativePath.startsWith(".")) return `./${relativePath}`;
|
|
12
|
-
return relativePath;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Convert TypeScript file extensions to JavaScript
|
|
16
|
-
*/
|
|
17
|
-
function convertToJsExtension(filePath) {
|
|
18
|
-
return filePath.replace(/\.ts$/, ".js");
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Resolve module specifier for imports
|
|
22
|
-
*/
|
|
23
|
-
function resolveModuleSpecifier(fromFile, toFile, tsConfig) {
|
|
24
|
-
const importPath = resolveImportPath(fromFile, toFile, tsConfig);
|
|
25
|
-
return convertToJsExtension(importPath);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
//#endregion
|
|
29
|
-
export { convertToJsExtension, resolveImportPath, resolveModuleSpecifier };
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
-
const path = require_chunk.__toESM(require("path"));
|
|
3
|
-
const fs = require_chunk.__toESM(require("fs"));
|
|
4
|
-
const typescript = require_chunk.__toESM(require("typescript"));
|
|
5
|
-
|
|
6
|
-
//#region src/tsconfig.ts
|
|
7
|
-
/**
|
|
8
|
-
* Find and parse the nearest tsconfig.json file
|
|
9
|
-
*/
|
|
10
|
-
function loadTsConfig(searchPath = process.cwd()) {
|
|
11
|
-
const configPath = typescript.default.findConfigFile(searchPath, fs.existsSync);
|
|
12
|
-
if (!configPath) return null;
|
|
13
|
-
const configFile = typescript.default.readConfigFile(configPath, typescript.default.sys.readFile);
|
|
14
|
-
if (configFile.error) throw new Error(`Error reading tsconfig.json: ${typescript.default.flattenDiagnosticMessageText(configFile.error.messageText, "\n")}`);
|
|
15
|
-
const parsedConfig = typescript.default.parseJsonConfigFileContent(configFile.config, typescript.default.sys, (0, path.dirname)(configPath));
|
|
16
|
-
if (parsedConfig.errors.length > 0) {
|
|
17
|
-
const errors = parsedConfig.errors.map((error) => typescript.default.flattenDiagnosticMessageText(error.messageText, "\n")).join("\n");
|
|
18
|
-
throw new Error(`Error parsing tsconfig.json: ${errors}`);
|
|
19
|
-
}
|
|
20
|
-
return {
|
|
21
|
-
configPath,
|
|
22
|
-
config: parsedConfig,
|
|
23
|
-
compilerOptions: parsedConfig.options
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Resolve a module path using TypeScript's module resolution
|
|
28
|
-
*/
|
|
29
|
-
function resolveModulePath(moduleName, containingFile, tsConfig) {
|
|
30
|
-
const compilerOptions = tsConfig?.compilerOptions || {};
|
|
31
|
-
const result = typescript.default.resolveModuleName(moduleName, containingFile, compilerOptions, typescript.default.sys);
|
|
32
|
-
if (result.resolvedModule) return result.resolvedModule.resolvedFileName;
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Get the output path for a TypeScript file based on tsconfig
|
|
37
|
-
*/
|
|
38
|
-
function getOutputPath(filePath, tsConfig) {
|
|
39
|
-
if (!tsConfig) return filePath.replace(/\.ts$/, ".js");
|
|
40
|
-
const { compilerOptions, configPath } = tsConfig;
|
|
41
|
-
const configDir = (0, path.dirname)(configPath);
|
|
42
|
-
if (compilerOptions.outDir) {
|
|
43
|
-
const rootDir = compilerOptions.rootDir || configDir;
|
|
44
|
-
const relativePath = filePath.startsWith(rootDir) ? filePath.slice(rootDir.length) : filePath;
|
|
45
|
-
const outputPath = (0, path.join)(compilerOptions.outDir, relativePath);
|
|
46
|
-
return outputPath.replace(/\.ts$/, ".js");
|
|
47
|
-
}
|
|
48
|
-
return filePath.replace(/\.ts$/, ".js");
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Apply path mappings from tsconfig paths
|
|
52
|
-
*/
|
|
53
|
-
function resolvePathMapping(importPath, tsConfig) {
|
|
54
|
-
if (!tsConfig || !tsConfig.compilerOptions.paths) return importPath;
|
|
55
|
-
const { paths, baseUrl } = tsConfig.compilerOptions;
|
|
56
|
-
const configDir = (0, path.dirname)(tsConfig.configPath);
|
|
57
|
-
const resolvedBaseUrl = baseUrl ? (0, path.resolve)(configDir, baseUrl) : configDir;
|
|
58
|
-
for (const [pattern, replacements] of Object.entries(paths)) {
|
|
59
|
-
const regex = new RegExp("^" + pattern.replace("*", "(.*)") + "$");
|
|
60
|
-
const match = importPath.match(regex);
|
|
61
|
-
if (match && replacements.length > 0) {
|
|
62
|
-
const replacement = replacements[0];
|
|
63
|
-
const resolvedPath = replacement.replace("*", match[1] || "");
|
|
64
|
-
return (0, path.resolve)(resolvedBaseUrl, resolvedPath);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return importPath;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
//#endregion
|
|
71
|
-
Object.defineProperty(exports, 'getOutputPath', {
|
|
72
|
-
enumerable: true,
|
|
73
|
-
get: function () {
|
|
74
|
-
return getOutputPath;
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
Object.defineProperty(exports, 'loadTsConfig', {
|
|
78
|
-
enumerable: true,
|
|
79
|
-
get: function () {
|
|
80
|
-
return loadTsConfig;
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
Object.defineProperty(exports, 'resolveModulePath', {
|
|
84
|
-
enumerable: true,
|
|
85
|
-
get: function () {
|
|
86
|
-
return resolveModulePath;
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
Object.defineProperty(exports, 'resolvePathMapping', {
|
|
90
|
-
enumerable: true,
|
|
91
|
-
get: function () {
|
|
92
|
-
return resolvePathMapping;
|
|
93
|
-
}
|
|
94
|
-
});
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { dirname, join, resolve } from "path";
|
|
2
|
-
import { existsSync } from "fs";
|
|
3
|
-
import ts from "typescript";
|
|
4
|
-
|
|
5
|
-
//#region src/tsconfig.ts
|
|
6
|
-
/**
|
|
7
|
-
* Find and parse the nearest tsconfig.json file
|
|
8
|
-
*/
|
|
9
|
-
function loadTsConfig(searchPath = process.cwd()) {
|
|
10
|
-
const configPath = ts.findConfigFile(searchPath, existsSync);
|
|
11
|
-
if (!configPath) return null;
|
|
12
|
-
const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
13
|
-
if (configFile.error) throw new Error(`Error reading tsconfig.json: ${ts.flattenDiagnosticMessageText(configFile.error.messageText, "\n")}`);
|
|
14
|
-
const parsedConfig = ts.parseJsonConfigFileContent(configFile.config, ts.sys, dirname(configPath));
|
|
15
|
-
if (parsedConfig.errors.length > 0) {
|
|
16
|
-
const errors = parsedConfig.errors.map((error) => ts.flattenDiagnosticMessageText(error.messageText, "\n")).join("\n");
|
|
17
|
-
throw new Error(`Error parsing tsconfig.json: ${errors}`);
|
|
18
|
-
}
|
|
19
|
-
return {
|
|
20
|
-
configPath,
|
|
21
|
-
config: parsedConfig,
|
|
22
|
-
compilerOptions: parsedConfig.options
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Resolve a module path using TypeScript's module resolution
|
|
27
|
-
*/
|
|
28
|
-
function resolveModulePath(moduleName, containingFile, tsConfig) {
|
|
29
|
-
const compilerOptions = tsConfig?.compilerOptions || {};
|
|
30
|
-
const result = ts.resolveModuleName(moduleName, containingFile, compilerOptions, ts.sys);
|
|
31
|
-
if (result.resolvedModule) return result.resolvedModule.resolvedFileName;
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Get the output path for a TypeScript file based on tsconfig
|
|
36
|
-
*/
|
|
37
|
-
function getOutputPath(filePath, tsConfig) {
|
|
38
|
-
if (!tsConfig) return filePath.replace(/\.ts$/, ".js");
|
|
39
|
-
const { compilerOptions, configPath } = tsConfig;
|
|
40
|
-
const configDir = dirname(configPath);
|
|
41
|
-
if (compilerOptions.outDir) {
|
|
42
|
-
const rootDir = compilerOptions.rootDir || configDir;
|
|
43
|
-
const relativePath = filePath.startsWith(rootDir) ? filePath.slice(rootDir.length) : filePath;
|
|
44
|
-
const outputPath = join(compilerOptions.outDir, relativePath);
|
|
45
|
-
return outputPath.replace(/\.ts$/, ".js");
|
|
46
|
-
}
|
|
47
|
-
return filePath.replace(/\.ts$/, ".js");
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Apply path mappings from tsconfig paths
|
|
51
|
-
*/
|
|
52
|
-
function resolvePathMapping(importPath, tsConfig) {
|
|
53
|
-
if (!tsConfig || !tsConfig.compilerOptions.paths) return importPath;
|
|
54
|
-
const { paths, baseUrl } = tsConfig.compilerOptions;
|
|
55
|
-
const configDir = dirname(tsConfig.configPath);
|
|
56
|
-
const resolvedBaseUrl = baseUrl ? resolve(configDir, baseUrl) : configDir;
|
|
57
|
-
for (const [pattern, replacements] of Object.entries(paths)) {
|
|
58
|
-
const regex = new RegExp("^" + pattern.replace("*", "(.*)") + "$");
|
|
59
|
-
const match = importPath.match(regex);
|
|
60
|
-
if (match && replacements.length > 0) {
|
|
61
|
-
const replacement = replacements[0];
|
|
62
|
-
const resolvedPath = replacement.replace("*", match[1] || "");
|
|
63
|
-
return resolve(resolvedBaseUrl, resolvedPath);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return importPath;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
//#endregion
|
|
70
|
-
export { getOutputPath, loadTsConfig, resolveModulePath, resolvePathMapping };
|
package/dist/tsconfig.cjs
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
const require_tsconfig = require('./tsconfig-83amrDAp.cjs');
|
|
2
|
-
|
|
3
|
-
exports.getOutputPath = require_tsconfig.getOutputPath;
|
|
4
|
-
exports.loadTsConfig = require_tsconfig.loadTsConfig;
|
|
5
|
-
exports.resolveModulePath = require_tsconfig.resolveModulePath;
|
|
6
|
-
exports.resolvePathMapping = require_tsconfig.resolvePathMapping;
|
package/dist/tsconfig.mjs
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { relative, dirname } from 'path';
|
|
2
|
-
import type { TsConfigResult } from '../tsconfig.js';
|
|
3
|
-
import { resolvePathMapping } from '../tsconfig.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Resolve import paths considering TypeScript path mappings
|
|
7
|
-
*/
|
|
8
|
-
export function resolveImportPath(
|
|
9
|
-
fromFile: string,
|
|
10
|
-
toFile: string,
|
|
11
|
-
tsConfig?: TsConfigResult | null,
|
|
12
|
-
): string {
|
|
13
|
-
// Apply path mappings if tsconfig is available
|
|
14
|
-
const resolvedToFile = tsConfig
|
|
15
|
-
? resolvePathMapping(toFile, tsConfig)
|
|
16
|
-
: toFile;
|
|
17
|
-
|
|
18
|
-
// Calculate relative path
|
|
19
|
-
const relativePath = relative(dirname(fromFile), resolvedToFile);
|
|
20
|
-
|
|
21
|
-
// Ensure proper relative path format
|
|
22
|
-
if (!relativePath.startsWith('.')) {
|
|
23
|
-
return `./${relativePath}`;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return relativePath;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Convert TypeScript file extensions to JavaScript
|
|
31
|
-
*/
|
|
32
|
-
export function convertToJsExtension(filePath: string): string {
|
|
33
|
-
return filePath.replace(/\.ts$/, '.js');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Resolve module specifier for imports
|
|
38
|
-
*/
|
|
39
|
-
export function resolveModuleSpecifier(
|
|
40
|
-
fromFile: string,
|
|
41
|
-
toFile: string,
|
|
42
|
-
tsConfig?: TsConfigResult | null,
|
|
43
|
-
): string {
|
|
44
|
-
const importPath = resolveImportPath(fromFile, toFile, tsConfig);
|
|
45
|
-
return convertToJsExtension(importPath);
|
|
46
|
-
}
|