From 5106424a320aa21c73ac2a2cf840dc697b7e0b01 Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Mon, 27 Mar 2023 20:21:41 +0200 Subject: [PATCH] Allow import of http jrpc definitions inside local ones --- package.json | 2 +- src/process.ts | 24 +++++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 0ca46f9..fc0dde0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/jrpcgen", - "version": "1.2.6", + "version": "1.2.11", "main": "lib/index.js", "license": "MIT", "packageManager": "yarn@3.1.1", diff --git a/src/process.ts b/src/process.ts index 7d22a5b..21b6a9e 100644 --- a/src/process.ts +++ b/src/process.ts @@ -18,7 +18,7 @@ import { ZIGTarget } from "./targets/zig"; import { DartTarget } from "./targets/dart"; import { URL } from "url"; -class CatchedError extends Error { } +class CatchedError extends Error {} const log = dbg("app"); @@ -46,15 +46,19 @@ function indexToLineAndCol(src: string, index: number) { return { line, col }; } -function resolve(base: string, ...parts: string[]) { - if (base.startsWith("http://") || base.startsWith("https://")) { +function resolve(base: string, sub?: string) { + if (sub && (sub.startsWith("http://") || sub.startsWith("https://"))) { + let u = new URL(sub); + return u.href; + } else if (base.startsWith("http://") || base.startsWith("https://")) { let u = new URL(base); - for (const part of parts) { - u = new URL(part, u); + if (sub) { + u = new URL(sub, u); } return u.href; } else { - return Path.resolve(base, ...parts); + if (!sub) return Path.resolve(base); + else return Path.resolve(Path.dirname(base), sub + ".jrpc"); } } @@ -151,13 +155,7 @@ async function processFile( let resolved: Parsed = []; for (const statement of parsed) { if (statement.type == "import") { - let res: string; - if (file.startsWith("http://") || file.startsWith("https://")) { - res = resolve(file, statement.path + ".jrpc"); - } else { - const base = Path.dirname(file); - res = resolve(base, statement.path + ".jrpc"); - } + let res = resolve(file, statement.path); resolved.push(...((await processFile(ctx, res)) || [])); } else { resolved.push(statement);