1
1
import { mkdirSync , type Stats , statSync } from "node:fs" ;
2
2
import { resolve } from "node:path" ;
3
+ import type { ParseArgsConfig } from "node:util" ;
3
4
import { parseArgs } from "node:util" ;
4
5
5
6
import type { WranglerTarget } from "./utils/run-wrangler.js" ;
@@ -25,32 +26,34 @@ export type Arguments = (
25
26
}
26
27
) & { outputDir ?: string } ;
27
28
29
+ // Config for parsing CLI arguments
30
+ const config = {
31
+ allowPositionals : true ,
32
+ strict : false ,
33
+ options : {
34
+ skipBuild : { type : "boolean" , short : "s" , default : false } ,
35
+ output : { type : "string" , short : "o" } ,
36
+ noMinify : { type : "boolean" , default : false } ,
37
+ skipWranglerConfigCheck : { type : "boolean" , default : false } ,
38
+ cacheChunkSize : { type : "string" } ,
39
+ } ,
40
+ } as const satisfies ParseArgsConfig ;
41
+
28
42
export function getArgs ( ) : Arguments {
29
- const { positionals, values } = parseArgs ( {
30
- options : {
31
- skipBuild : { type : "boolean" , short : "s" , default : false } ,
32
- output : { type : "string" , short : "o" } ,
33
- noMinify : { type : "boolean" , default : false } ,
34
- skipWranglerConfigCheck : { type : "boolean" , default : false } ,
35
- cacheChunkSize : { type : "string" } ,
36
- } ,
37
- allowPositionals : true ,
38
- } ) ;
43
+ const { positionals, values } = parseArgs ( config ) ;
39
44
40
- const outputDir = values . output ? resolve ( values . output ) : undefined ;
45
+ const outputDir = typeof values . output === "string" ? resolve ( values . output ) : undefined ;
41
46
if ( outputDir ) assertDirArg ( outputDir , "output" , true ) ;
42
47
43
- const passthroughArgs = getPassthroughArgs ( ) ;
44
-
45
48
switch ( positionals [ 0 ] ) {
46
49
case "build" :
47
50
return {
48
51
command : "build" ,
49
52
outputDir,
50
53
skipNextBuild :
51
- values . skipBuild || [ "1" , "true" , "yes" ] . includes ( String ( process . env . SKIP_NEXT_APP_BUILD ) ) ,
54
+ ! ! values . skipBuild || [ "1" , "true" , "yes" ] . includes ( String ( process . env . SKIP_NEXT_APP_BUILD ) ) ,
52
55
skipWranglerConfigCheck :
53
- values . skipWranglerConfigCheck ||
56
+ ! ! values . skipWranglerConfigCheck ||
54
57
[ "1" , "true" , "yes" ] . includes ( String ( process . env . SKIP_WRANGLER_CONFIG_CHECK ) ) ,
55
58
minify : ! values . noMinify ,
56
59
} ;
@@ -60,7 +63,7 @@ export function getArgs(): Arguments {
60
63
return {
61
64
command : positionals [ 0 ] ,
62
65
outputDir,
63
- passthroughArgs,
66
+ passthroughArgs : getPassthroughArgs ( process . argv , config ) ,
64
67
...( values . cacheChunkSize && { cacheChunkSize : Number ( values . cacheChunkSize ) } ) ,
65
68
} ;
66
69
case "populateCache" :
@@ -71,7 +74,7 @@ export function getArgs(): Arguments {
71
74
command : "populateCache" ,
72
75
outputDir,
73
76
target : positionals [ 1 ] ,
74
- environment : getWranglerEnvironmentFlag ( passthroughArgs ) ,
77
+ environment : getWranglerEnvironmentFlag ( process . argv ) ,
75
78
...( values . cacheChunkSize && { cacheChunkSize : Number ( values . cacheChunkSize ) } ) ,
76
79
} ;
77
80
default :
@@ -81,9 +84,29 @@ export function getArgs(): Arguments {
81
84
}
82
85
}
83
86
84
- function getPassthroughArgs ( ) {
85
- const passthroughPos = process . argv . indexOf ( "--" ) ;
86
- return passthroughPos === - 1 ? [ ] : process . argv . slice ( passthroughPos + 1 ) ;
87
+ export function getPassthroughArgs < T extends ParseArgsConfig > ( args : string [ ] , { options = { } } : T ) {
88
+ const passthroughArgs : string [ ] = [ ] ;
89
+
90
+ for ( let i = 0 ; i < args . length ; i ++ ) {
91
+ if ( args [ i ] === "--" ) {
92
+ passthroughArgs . push ( ...args . slice ( i + 1 ) ) ;
93
+ return passthroughArgs ;
94
+ }
95
+
96
+ // look for `--arg(=value)`, `-arg(=value)`
97
+ const [ , name ] = / ^ - - ? ( \w [ \w - ] * ) ( = .+ ) ? $ / . exec ( args [ i ] ! ) ?? [ ] ;
98
+ if ( name && ! ( name in options ) ) {
99
+ passthroughArgs . push ( args [ i ] ! ) ;
100
+
101
+ // Array args can have multiple values
102
+ // ref https://github.com/yargs/yargs-parser/blob/main/README.md#greedy-arrays
103
+ while ( i < args . length - 1 && ! args [ i + 1 ] ?. startsWith ( "-" ) ) {
104
+ passthroughArgs . push ( args [ ++ i ] ! ) ;
105
+ }
106
+ }
107
+ }
108
+
109
+ return passthroughArgs ;
87
110
}
88
111
89
112
function assertDirArg ( path : string , argName ?: string , make ?: boolean ) {
0 commit comments