Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"build/globals.js",
"build/deno.js"
],
"limit": "881.42 kB",
"limit": "881.97 kB",
"brotli": false,
"gzip": false
},
Expand Down Expand Up @@ -66,7 +66,7 @@
"README.md",
"LICENSE"
],
"limit": "944.17 kB",
"limit": "944.71 kB",
"brotli": false,
"gzip": false
}
Expand Down
12 changes: 11 additions & 1 deletion build/cli.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -339,18 +339,28 @@ 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+$/, "");
return mainPath === modulePath;
}
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 [
Expand Down
19 changes: 18 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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+$/, '')
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down