From b1f2395e50f0341f90a0c4234e0774f7e1138bd5 Mon Sep 17 00:00:00 2001 From: Rohan Patnaik Date: Fri, 22 May 2026 11:58:32 +0530 Subject: [PATCH] fix(cli): detect deno jsr cli entrypoint --- .size-limit.json | 4 ++-- build/cli.cjs | 12 +++++++++++- src/cli.ts | 19 ++++++++++++++++++- test/cli.test.js | 17 +++++++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/.size-limit.json b/.size-limit.json index 258fd6c908..05e65ad69e 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -33,7 +33,7 @@ "build/globals.js", "build/deno.js" ], - "limit": "881.42 kB", + "limit": "881.97 kB", "brotli": false, "gzip": false }, @@ -66,7 +66,7 @@ "README.md", "LICENSE" ], - "limit": "944.17 kB", + "limit": "944.71 kB", "brotli": false, "gzip": false } diff --git a/build/cli.cjs b/build/cli.cjs index e494590738..491ff1c9f1 100755 --- a/build/cli.cjs +++ b/build/cli.cjs @@ -339,6 +339,7 @@ function injectGlobalRequire(origin) { } function isMain(meta = import_meta_url, scriptpath = import_node_process2.default.argv[1]) { if (typeof meta === "string") { + if (isDenoJsrMain(meta)) return true; if (meta.startsWith("file:")) { const modulePath = import_node_url.default.fileURLToPath(meta).replace(/\.\w+$/, ""); const mainPath = import_index.fs.realpathSync(scriptpath).replace(/\.\w+$/, ""); @@ -346,11 +347,20 @@ function isMain(meta = import_meta_url, scriptpath = import_node_process2.defaul } return false; } - return !!meta.main; + return !!meta.main || isDenoJsrMain(meta.url); } function normalizeExt(ext) { return ext ? import_index.path.parse(`foo.${ext}`).ext : ext; } +function isDenoJsrMain(metaUrl) { + var _a2; + const mainModule = (_a2 = globalThis.Deno) == null ? void 0 : _a2.mainModule; + if (!(mainModule == null ? void 0 : mainModule.startsWith("jsr:@webpod/zx"))) return false; + const normalize = (s) => s.replace("/./", "/").replace(/\/\.?$/, ""); + const main2 = normalize(mainModule); + const current = normalize(metaUrl); + return current === "jsr:@webpod/zx/cli" && (main2 === "jsr:@webpod/zx" || main2 === "jsr:@webpod/zx/cli"); +} function getFilepath(cwd = ".", name = "zx", _ext) { const ext = _ext || argv.ext || EXT; return [ diff --git a/src/cli.ts b/src/cli.ts index df1ea719f0..98de7041eb 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -270,6 +270,8 @@ export function isMain( scriptpath: string = process.argv[1] ): boolean { if (typeof meta === 'string') { + if (isDenoJsrMain(meta)) return true + if (meta.startsWith('file:')) { const modulePath = url.fileURLToPath(meta).replace(/\.\w+$/, '') const mainPath = fs.realpathSync(scriptpath).replace(/\.\w+$/, '') @@ -279,13 +281,28 @@ export function isMain( return false } - return !!meta.main + return !!meta.main || isDenoJsrMain(meta.url) } export function normalizeExt(ext?: string): string | undefined { return ext ? path.parse(`foo.${ext}`).ext : ext } +function isDenoJsrMain(metaUrl: string): boolean { + const mainModule = (globalThis as { Deno?: { mainModule?: string } }).Deno + ?.mainModule + if (!mainModule?.startsWith('jsr:@webpod/zx')) return false + + const normalize = (s: string) => s.replace('/./', '/').replace(/\/\.?$/, '') + const main = normalize(mainModule) + const current = normalize(metaUrl) + + return ( + current === 'jsr:@webpod/zx/cli' && + (main === 'jsr:@webpod/zx' || main === 'jsr:@webpod/zx/cli') + ) +} + // prettier-ignore function getFilepath(cwd = '.', name = 'zx', _ext?: string): string { const ext = _ext || argv.ext || EXT diff --git a/test/cli.test.js b/test/cli.test.js index 848cade891..f985c4227e 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -445,6 +445,23 @@ console.log(a); assert.equal(isMain('///root/zx/test/cli.test.js'), false) }) + test('isMain() accepts Deno JSR cli entrypoints', () => { + const deno = globalThis.Deno + globalThis.Deno = { mainModule: 'jsr:@webpod/zx' } + try { + assert.equal(isMain('jsr:@webpod/zx/cli'), true) + assert.equal(isMain('jsr:@webpod/zx/./cli'), true) + + globalThis.Deno.mainModule = 'jsr:@webpod/zx/.' + assert.equal(isMain('jsr:@webpod/zx/cli'), true) + + globalThis.Deno.mainModule = 'jsr:@webpod/zx/core' + assert.equal(isMain('jsr:@webpod/zx/cli'), false) + } finally { + globalThis.Deno = deno + } + }) + test('normalizeExt()', () => { assert.equal(normalizeExt('.ts'), '.ts') assert.equal(normalizeExt('ts'), '.ts')