All files cli.ts

81.08% Statements 30/37
55.55% Branches 5/9
100% Functions 4/4
81.08% Lines 30/37

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    1x 1x 1x 1x 1x 1x   1x   6x 6x 6x 6x 6x     6x   1x 6x 6x 6x 6x   80x 80x 80x 80x 80x 80x 17x 17x 80x   5x 5x               1x  
#!/usr/bin/env node
 
import { realpathSync } from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import { loadProjectDotenvIntoProcessEnv } from './cli/helpers.ts'
import { ensureError } from './cli/types.ts'
 
const currentFile = realpathSync(fileURLToPath(import.meta.url))
 
function resolveInvokedPath(invoked?: string): string | null {
  if (invoked == null) return null
  try {
    return realpathSync(invoked)
  } catch {
    return path.resolve(invoked)
  }
}
 
export function shouldRunCli(invoked?: string): boolean {
  const resolved = resolveInvokedPath(invoked)
  if (resolved == null) return false
  return resolved === currentFile
}
 
export async function main(args = process.argv.slice(2)): Promise<void> {
  loadProjectDotenvIntoProcessEnv()
  const { createCli } = await import('./cli/commands/index.ts')
  const cli = createCli()
  const exitCode = await cli.run(args)
  if (exitCode !== 0) {
    process.exitCode = exitCode
  }
}
 
export async function runCliWhenExecuted(): Promise<void> {
  if (!shouldRunCli(process.argv[1])) return
 
  await main().catch((error) => {
    console.error(ensureError(error).message)
    process.exitCode = 1
  })
}
 
await runCliWhenExecuted()