1- #! /usr/bin/env -S deno run --allow-run --allow-net --allow-read --allow-write=/tmp --allow-env
1+ #! /usr/bin/env node
22
33/*
44 * This Source Code Form is subject to the terms of the Mozilla Public
77 *
88 * Copyright Oxide Computer Company
99 */
10- import { Command , ValidationError } from 'jsr:@cliffy/[email protected] ' 11- import { $ } from 'jsr:@david/[email protected] ' 12- import { exists } from 'jsr:@std/[email protected] ' 10+ import { existsSync } from 'node:fs'
11+ import { mkdir , writeFile } from 'node:fs/promises'
12+
13+ import { Command , Option } from '@commander-js/extra-typings'
14+ import $ from 'dax'
1315
1416import { isJjRepository , pickPr , resolveLocalCommit } from './common.ts'
1517
@@ -122,7 +124,7 @@ const remoteSource: Source = {
122124/** Read schemas from the git repo in the current directory (run from an omicron checkout) */
123125async function createLocalSource ( ) : Promise < Source > {
124126 if ( ! $ . commandExistsSync ( 'git' ) ) throw new Error ( '--local requires git' )
125- const repoRoot = Deno . cwd ( )
127+ const repoRoot = process . cwd ( )
126128 // jj's working copy is always a commit, so in a jj repo @ is the natural
127129 // default and reflects in-progress (even uncommitted) work. Plain git uses HEAD.
128130 const isJj = await isJjRepository ( repoRoot )
@@ -243,8 +245,6 @@ async function resolveTarget(
243245
244246 // Two refs: compare latest schema on each
245247 if ( ref2 !== undefined ) {
246- if ( ref1 === undefined )
247- throw new ValidationError ( 'Provide a base ref when passing two refs' )
248248 const [ baseRef , headRef ] = await Promise . all ( [
249249 source . resolveCommit ( norm ( ref1 ) ) ,
250250 source . resolveCommit ( norm ( ref2 ) ) ,
@@ -297,11 +297,11 @@ async function ensureSchema(
297297) {
298298 const dir = `/tmp/api-diff/${ commit } /${ specFilename } `
299299 const schemaPath = `${ dir } /spec.json`
300- if ( force || ! ( await exists ( schemaPath ) ) ) {
301- await $ ` mkdir -p ${ dir } `
300+ if ( force || ! existsSync ( schemaPath ) ) {
301+ await mkdir ( dir , { recursive : true } )
302302 console . error ( `Loading ${ specFilename } ...` )
303303 const content = await source . readSpec ( commit , specFilename )
304- await Deno . writeTextFile ( schemaPath , content )
304+ await writeFile ( schemaPath , content )
305305 }
306306 return schemaPath
307307}
@@ -315,15 +315,15 @@ const OXFMT_CONFIG = '/tmp/api-diff/oxfmt.json'
315315 */
316316async function ensureClientTools ( ) {
317317 await $ `npx --yes --package @oxide/openapi-gen-ts@latest --package oxfmt true`
318- await $ ` mkdir -p /tmp/api-diff`
318+ await mkdir ( ' /tmp/api-diff' , { recursive : true } )
319319 // an explicit (empty) config silences oxfmt's "No config found" warning
320- await Deno . writeTextFile ( OXFMT_CONFIG , '{}\n' )
320+ await writeFile ( OXFMT_CONFIG , '{}\n' )
321321}
322322
323323async function ensureClient ( schemaPath : string , force : boolean ) {
324324 const dir = schemaPath . replace ( / \/ s p e c \. j s o n $ / , '' )
325325 const clientPath = `${ dir } /Api.ts`
326- if ( force || ! ( await exists ( clientPath ) ) ) {
326+ if ( force || ! existsSync ( clientPath ) ) {
327327 console . error ( `Generating client...` )
328328 await $ `npx --yes @oxide/openapi-gen-ts@latest ${ schemaPath } ${ dir } `
329329 await $ `npx --yes oxfmt -c ${ OXFMT_CONFIG } ${ dir } `
@@ -345,7 +345,7 @@ async function runDiff(
345345 const filename = base . endsWith ( 'spec.json' ) ? 'spec.json' : 'Api.ts'
346346 // prefer delta if it exists and output is a terminal, otherwise use diff
347347 // https://dandavison.github.io/delta/
348- const useDelta = $ . commandExistsSync ( 'delta' ) && Deno . stdout . isTerminal ( )
348+ const useDelta = $ . commandExistsSync ( 'delta' ) && process . stdout . isTTY
349349
350350 // use -L to set labels, extracting version from spec filename (e.g., nexus-2026010300.0.0-7599dd.json)
351351 const getVersion = ( spec : string ) =>
@@ -359,9 +359,10 @@ async function runDiff(
359359
360360await new Command ( )
361361 . name ( 'api-diff' )
362- . description (
363- `Display changes to API client or schema caused by a given Omicron PR.
364-
362+ . description ( 'Display changes to API client or schema caused by a given Omicron PR.' )
363+ . addHelpText (
364+ 'after' ,
365+ `
365366Arguments:
366367 No args Interactive PR picker (or, with --local, the default ref)
367368 <ref> PR number, commit SHA, branch, or tag
@@ -376,32 +377,29 @@ the current schema (nexus-latest) at each commit:
376377Refs are git revisions (jj revsets in a jj repo). PR numbers are not available.
377378
378379Dependencies:
379- - Deno
380+ - Node.js
380381 - GitHub CLI (gh), or git in the current directory with --local
381382 - Optional: delta diff pager https://dandavison.github.io/delta/
382383 - Optional: fzf for PR picker https://github.com/junegunn/fzf`
383384 )
384- . helpOption ( '-h, --help' , 'Show help' )
385385 . option ( '--local' , 'Read schemas from the repo in the current directory' )
386386 . option ( '--force' , 'Redo everything even if cached' )
387- . type ( 'format' , ( { value } ) => {
388- if ( value !== 'ts' && value !== 'schema' ) {
389- throw new ValidationError ( `Invalid format: '${ value } '. Must be 'ts' or 'schema'` )
390- }
391- return value
392- } )
393- . option ( '-f, --format <format:format>' , "Output format: 'ts' or 'schema'" , {
394- default : 'ts' as const ,
395- } )
396- . arguments ( '[ref1:string] [ref2:string]' )
397- . action ( async ( options , ref ?: string , ref2 ?: string ) => {
387+ . addOption (
388+ new Option ( '-f, --format <format>' , 'Output format' )
389+ . choices ( [ 'ts' , 'schema' ] as const )
390+ . default ( 'ts' as const )
391+ )
392+ . argument ( '[ref1]' )
393+ . argument ( '[ref2]' )
394+ . showHelpAfterError ( )
395+ . action ( async ( ref , ref2 , options ) => {
398396 try {
399397 const local = options . local ?? false
398+ const force = options . force ?? false
400399 if ( ! local && ! $ . commandExistsSync ( 'gh' ) ) throw new Error ( 'Need gh (GitHub CLI)' )
401400 const source = local ? await createLocalSource ( ) : remoteSource
402401
403402 const target = await resolveTarget ( source , local , ref , ref2 )
404- const force = options . force ?? false
405403
406404 const [ baseSchema , headSchema ] = await Promise . all ( [
407405 ensureSchema ( source , target . baseCommit , target . baseSchema , force ) ,
@@ -420,7 +418,7 @@ Dependencies:
420418 }
421419 } catch ( e ) {
422420 console . error ( `error: ${ e instanceof Error ? e . message : String ( e ) } ` )
423- Deno . exit ( 1 )
421+ process . exit ( 1 )
424422 }
425423 } )
426- . parse ( Deno . args )
424+ . parseAsync ( )
0 commit comments