Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: google/zx
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: antongolub/zx
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: cli-env-fallback
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 1 commit
  • 5 files changed
  • 1 contributor

Commits on Mar 16, 2025

  1. feat(cli): use ZX_-prefixed envars as default flags

    antongolub committed Mar 16, 2025

    Verified

    This commit was signed with the committer’s verified signature.
    antongolub Anton Golub
    Copy the full SHA
    997ee33 View commit details
Showing with 24 additions and 19 deletions.
  1. +1 −1 .size-limit.json
  2. +1 −1 src/cli.ts
  3. +15 −15 src/core.ts
  4. +3 −2 src/goods.ts
  5. +4 −0 test/goods.test.js
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
{
"name": "zx/core",
"path": ["build/core.cjs", "build/util.cjs", "build/vendor-core.cjs"],
"limit": "77.6 kB",
"limit": "77.3 kB",
"brotli": false,
"gzip": false
},
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -89,7 +89,7 @@ export const argv: minimist.ParsedArgs = parseArgv(process.argv.slice(2), {
stopEarly: true,
parseBoolean: true,
camelCase: true,
})
}, resolveDefaults({} as any, 'ZX_', process.env, new Set(['env', 'eval', 'install', 'registry'])))

export async function main(): Promise<void> {
await import('./globals.js')
30 changes: 15 additions & 15 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -71,6 +71,19 @@ const EOL = Buffer.from(_EOL)
const BR_CC = '\n'.charCodeAt(0)
const SIGTERM = 'SIGTERM'
const ENV_PREFIX = 'ZX_'
const ENV_ALLOWED: Set<string> = new Set([
'cwd',
'preferLocal',
'detached',
'verbose',
'quiet',
'timeout',
'timeoutSignal',
'killSignal',
'prefix',
'postfix',
'shell',
])
const storage = new AsyncLocalStorage<Options>()

function getStore() {
@@ -927,22 +940,9 @@ const promisifyStream = <S extends Writable>(
export function resolveDefaults(
defs: Options = defaults,
prefix: string = ENV_PREFIX,
env = process.env
env = process.env,
allowed = ENV_ALLOWED
): Options {
const allowed = new Set([
'cwd',
'preferLocal',
'detached',
'verbose',
'quiet',
'timeout',
'timeoutSignal',
'killSignal',
'prefix',
'postfix',
'shell',
])

return Object.entries(env).reduce<Options>((m, [k, v]) => {
if (v && k.startsWith(prefix)) {
const _k = toCamelCase(k.slice(prefix.length))
5 changes: 3 additions & 2 deletions src/goods.ts
Original file line number Diff line number Diff line change
@@ -38,7 +38,8 @@ type ArgvOpts = minimist.Opts & { camelCase?: boolean; parseBoolean?: boolean }

export const parseArgv = (
args: string[] = process.argv.slice(2),
opts: ArgvOpts = {}
opts: ArgvOpts = {},
defs: Record<string, any> = {}
): minimist.ParsedArgs =>
Object.entries(minimist(args, opts)).reduce<minimist.ParsedArgs>(
(m, [k, v]) => {
@@ -48,7 +49,7 @@ export const parseArgv = (
m[_k] = _v
return m
},
{} as minimist.ParsedArgs
{ ...defs } as minimist.ParsedArgs
)

export function updateArgv(args?: string[], opts?: ArgvOpts) {
4 changes: 4 additions & 0 deletions test/goods.test.js
Original file line number Diff line number Diff line change
@@ -156,6 +156,9 @@ describe('goods', () => {
camelCase: true,
parseBoolean: true,
alias: { a: 'aaa' },
},
{
def: 'def',
}
),
{
@@ -170,6 +173,7 @@ describe('goods', () => {
b4: false,
b5: true,
b6: true,
def: 'def',
}
)
})