Skip to content
Merged
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
17 changes: 15 additions & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env node

import { Command } from 'commander';
import { realpathSync } from 'node:fs';
import { chmod, mkdir, readFile, rename, writeFile } from 'node:fs/promises';
import { homedir } from 'node:os';
import { dirname, join } from 'node:path';
import { pathToFileURL } from 'node:url';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { GraphQLClient } from 'graphql-request';
import {
type ViewerAssertionSubjectType,
Expand Down Expand Up @@ -1199,7 +1200,19 @@ export function createProgram(): Command {

const currentEntryPoint = process.argv[1];

if (currentEntryPoint && import.meta.url === pathToFileURL(currentEntryPoint).href) {
function isCliEntrypoint(entryPoint: string | undefined): boolean {
if (!entryPoint) {
return false;
}

try {
return realpathSync(entryPoint) === realpathSync(fileURLToPath(import.meta.url));
} catch {
return import.meta.url === pathToFileURL(entryPoint).href;
}
}
Comment on lines +1203 to +1213
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The fallback logic in the catch block uses pathToFileURL(entryPoint), which can throw a TypeError if entryPoint is a relative path (Node.js requires an absolute path for pathToFileURL). While realpathSync usually resolves relative paths to absolute ones, if it fails for other reasons (e.g., permission issues on a parent directory), the fallback will crash the process with an unhandled exception. Consider wrapping the fallback in a nested try-catch or ensuring the path is absolute before conversion.

Suggested change
function isCliEntrypoint(entryPoint: string | undefined): boolean {
if (!entryPoint) {
return false;
}
try {
return realpathSync(entryPoint) === realpathSync(fileURLToPath(import.meta.url));
} catch {
return import.meta.url === pathToFileURL(entryPoint).href;
}
}
function isCliEntrypoint(entryPoint: string | undefined): boolean {
if (!entryPoint) {
return false;
}
try {
return realpathSync(entryPoint) === realpathSync(fileURLToPath(import.meta.url));
} catch {
try {
return import.meta.url === pathToFileURL(entryPoint).href;
} catch {
return false;
}
}
}


if (isCliEntrypoint(currentEntryPoint)) {
createProgram().parse(process.argv);
}

Expand Down
Loading