Skip to content

Commit 26e9559

Browse files
authored
feat(cli): use ZX_-prefixed envars as default flags (#1137)
1 parent e1b66cb commit 26e9559

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

.size-limit.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
"name": "zx/core",
44
"path": ["build/core.cjs", "build/util.cjs", "build/vendor-core.cjs"],
5-
"limit": "77.6 kB",
5+
"limit": "77.3 kB",
66
"brotli": false,
77
"gzip": false
88
},

src/cli.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const argv: minimist.ParsedArgs = parseArgv(process.argv.slice(2), {
8989
stopEarly: true,
9090
parseBoolean: true,
9191
camelCase: true,
92-
})
92+
}, resolveDefaults({} as any, 'ZX_', process.env, new Set(['env', 'eval', 'install', 'registry'])))
9393

9494
export async function main(): Promise<void> {
9595
await import('./globals.js')

src/core.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@ const EOL = Buffer.from(_EOL)
7171
const BR_CC = '\n'.charCodeAt(0)
7272
const SIGTERM = 'SIGTERM'
7373
const ENV_PREFIX = 'ZX_'
74+
const ENV_ALLOWED: Set<string> = new Set([
75+
'cwd',
76+
'preferLocal',
77+
'detached',
78+
'verbose',
79+
'quiet',
80+
'timeout',
81+
'timeoutSignal',
82+
'killSignal',
83+
'prefix',
84+
'postfix',
85+
'shell',
86+
])
7487
const storage = new AsyncLocalStorage<Options>()
7588

7689
function getStore() {
@@ -927,22 +940,9 @@ const promisifyStream = <S extends Writable>(
927940
export function resolveDefaults(
928941
defs: Options = defaults,
929942
prefix: string = ENV_PREFIX,
930-
env = process.env
943+
env = process.env,
944+
allowed = ENV_ALLOWED
931945
): Options {
932-
const allowed = new Set([
933-
'cwd',
934-
'preferLocal',
935-
'detached',
936-
'verbose',
937-
'quiet',
938-
'timeout',
939-
'timeoutSignal',
940-
'killSignal',
941-
'prefix',
942-
'postfix',
943-
'shell',
944-
])
945-
946946
return Object.entries(env).reduce<Options>((m, [k, v]) => {
947947
if (v && k.startsWith(prefix)) {
948948
const _k = toCamelCase(k.slice(prefix.length))

src/goods.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ type ArgvOpts = minimist.Opts & { camelCase?: boolean; parseBoolean?: boolean }
3838

3939
export const parseArgv = (
4040
args: string[] = process.argv.slice(2),
41-
opts: ArgvOpts = {}
41+
opts: ArgvOpts = {},
42+
defs: Record<string, any> = {}
4243
): minimist.ParsedArgs =>
4344
Object.entries(minimist(args, opts)).reduce<minimist.ParsedArgs>(
4445
(m, [k, v]) => {
@@ -48,7 +49,7 @@ export const parseArgv = (
4849
m[_k] = _v
4950
return m
5051
},
51-
{} as minimist.ParsedArgs
52+
{ ...defs } as minimist.ParsedArgs
5253
)
5354

5455
export function updateArgv(args?: string[], opts?: ArgvOpts) {

test/goods.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ describe('goods', () => {
156156
camelCase: true,
157157
parseBoolean: true,
158158
alias: { a: 'aaa' },
159+
},
160+
{
161+
def: 'def',
159162
}
160163
),
161164
{
@@ -170,6 +173,7 @@ describe('goods', () => {
170173
b4: false,
171174
b5: true,
172175
b6: true,
176+
def: 'def',
173177
}
174178
)
175179
})

0 commit comments

Comments
 (0)