1- const assert = require ( 'assert' )
2- const { resolve } = require ( 'path' )
3- const minimist = require ( 'minimist' )
1+ import assert from 'node:assert'
2+ import { resolve } from 'node:path'
3+ import minimist , { ParsedArgs } from 'minimist'
4+ import { getConfig } from './cfg.cjs'
45
5- const { getConfig } = require ( './cfg' )
6+ const arrayify = < T > ( v : T | T [ ] ) => ( Array . isArray ( v ) ? [ ...v ] : [ v ] )
7+ const argify = ( key : string ) => ( { arg : `--${ key } ` , key } )
68
7- const arrayify = v => ( Array . isArray ( v ) ? [ ...v ] : [ v ] )
8- const argify = key => ( { arg : `--${ key } ` , key } )
9-
10- const resolvePath = p => resolve ( process . cwd ( ) , p )
9+ const resolvePath = ( p : string ) => resolve ( process . cwd ( ) , p )
1110
1211const nodeAlias = { require : 'r' }
1312const nodeBoolean = [ 'expose_gc' , 'preserve-symlinks' ]
@@ -23,8 +22,8 @@ const boolean = [...nodeBoolean, ...nodeDevBoolean]
2322const string = [ ...nodeString , ...nodeDevString ]
2423
2524const nodeArgsReducer =
26- opts =>
27- ( out , { arg, key } ) => {
25+ ( opts : ParsedArgs ) =>
26+ ( out : string [ ] , { arg, key } : { arg : string , key : string } ) => {
2827 const value = opts [ key ]
2928
3029 if ( typeof value === 'boolean' ) {
@@ -44,22 +43,40 @@ const nodeArgsReducer =
4443 return out
4544 }
4645
47- const nodeCustomFactory = args => arg => {
46+ const nodeCustomFactory = ( args : any [ ] ) => ( arg : string ) => {
4847 const isNodeCustom = nodeCustom . includes ( arg . substring ( 2 ) )
4948 if ( isNodeCustom ) args . push ( arg )
5049 return ! isNodeCustom
5150}
5251
53- const unknownFactory = args => arg => {
52+ const unknownFactory = ( args : Array < { arg : string , key : string } > ) => ( arg : string ) => {
5453 const [ , key ] = Object . keys ( minimist ( [ arg ] ) )
5554 key && ! nodeDevNumber . includes ( key ) && args . push ( { arg, key } )
55+ return true
56+ }
57+
58+ export interface Options {
59+ clear : boolean
60+ dedupe : boolean
61+ fork : boolean
62+ notify : boolean
63+ poll : boolean
64+ respawn : boolean
65+ vm : boolean
66+ debounce : number
67+ deps : number
68+ extensions : Record < string , string >
69+ graceful_ipc : string
70+ ignore : string [ ]
71+ interval : number
72+ timestamp : string
5673}
5774
58- module . exports = argv => {
59- const nodeCustomArgs = [ ]
75+ export const cli = ( argv : string [ ] ) => {
76+ const nodeCustomArgs = new Array < string > ( )
6077 const args = argv . slice ( 2 ) . filter ( nodeCustomFactory ( nodeCustomArgs ) )
6178
62- const unknownArgs = [ ]
79+ const unknownArgs = new Array < { arg : string , key : string } > ( )
6380 const unknown = unknownFactory ( unknownArgs )
6481
6582 const {
@@ -70,10 +87,10 @@ module.exports = argv => {
7087
7188 const opts = minimist ( args , { alias, boolean, default : getConfig ( script ) } )
7289 const nodeArgs = [ ...nodeBoolean . map ( argify ) , ...nodeString . map ( argify ) , ...unknownArgs ]
73- . sort ( ( a , b ) => a . key - b . key )
90+ . sort ( ( a , b ) => parseFloat ( a . key ) - parseFloat ( b . key ) )
7491 . reduce ( nodeArgsReducer ( opts ) , [ ...nodeCustomArgs ] )
7592
7693 opts . ignore = arrayify ( opts . ignore ) . map ( resolvePath )
7794
78- return { nodeArgs, opts, script, scriptArgs }
95+ return { nodeArgs, opts : opts as any as Options , script, scriptArgs }
7996}
0 commit comments