All files / src/cli/init browser.ts

84.61% Statements 11/13
100% Branches 0/0
75% Functions 3/4
84.61% Lines 11/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50                                    3x           3x 7x             3x 7x             3x 7x 7x 6x 4x   3x      
/**
 * Browser utilities for init wizard
 */
 
type OpenModule = { default: (url: string) => Promise<unknown> };
 
/**
 * Dynamic import that works in CommonJS context for ESM-only packages.
 * TypeScript transpiles dynamic import() to require() in CommonJS mode,
 * so we use eval to preserve the actual import() call at runtime.
 */
async function dynamicImport(): Promise<OpenModule> {
  // Using indirect eval to preserve dynamic import in CommonJS output
  const importFn = (0, eval)("m => import(m)") as (m: string) => Promise<OpenModule>;
  return importFn("open");
}
 
/** Import function - can be replaced in tests */
let importOpen: () => Promise<OpenModule> = dynamicImport;
 
/**
 * Set custom import function (for testing)
 * @internal
 */
export function _setImportOpen(fn: () => Promise<OpenModule>): void {
  importOpen = fn;
}
 
/**
 * Reset to default import function (for testing cleanup)
 * @internal
 */
export function _resetImportOpen(): void {
  importOpen = dynamicImport;
}
 
/**
 * Open URL in browser using the ESM-only 'open' package.
 * Returns true if successful, false otherwise.
 */
export async function openUrl(url: string): Promise<boolean> {
  try {
    const openModule = await importOpen();
    await openModule.default(url);
    return true;
  } catch {
    return false;
  }
}