// you may not need all of these but they come up a lot
import fs from "fs" ;
import assert from "assert" ;
import logging from '../lib/logging.js' ;
import glob from "fast-glob" ;
import path from "path" ;
import template from "lodash/template.js" ;
const log = logging . create ( import . meta . url ) ;
export const description = "Describe your command here."
// your command uses the npm package commander's options format
export const options = [
[ "--input <string>" , "A string option with a default." , "default.json" ] ,
[ "--min <number>" , "A number option, no default." ] ,
]
// example of a positional argument, it's the 1st argument to main
export const argument = [ "source" , "source directory" ] ;
// put required options in the required variable
export const required = [
[ "--output <string>" , "Save to file rather than stdout." ] ,
]
// handy function for checking things are good and aborting
const check = ( test , fail _message ) => {
if ( ! test ) {
log . error ( fail _message ) ;
process . exit ( 1 ) ;
}
}
/ *
+ FOOTGUN : commander tries to be "fancy" and if you don ' t have
an ` arguments ` setting for positional arguments then you
have to change this to ` (opts) ` instead of ` (arg, opts) ` .
The reason is if your command doesn ' t have an argument , then
commander will call your main with only opts , and if it does
then it calls your main with arg , and opts . If you get weird
errors and ` opts ` looks like a ` Command ` object then this is
what happened .
* /
export const main = async ( arg , opts ) => {
// if they give an numeric option this is how you can convert it
// yes, this is annoying and commander should handle it but oh well
if ( opts . min !== undefined ) {
// commander doesn't do integer conversions sadly
opts . min = parseInt ( opts . min , 10 ) ;
check ( ! isNaN ( opts . min ) , "--min must be a number" ) ;
}
// it's easier to debug options with console
console . log ( "OPTIONS" , opts , "ARG" , arg ) ;
// due to how async/await works it's just easier to manually exit with exit codes
process . exit ( 0 ) ;
}