-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/973 provide configuration via env vars #988
Changes from 16 commits
7cb0b63
c22cd7b
3589408
3dbeb88
ffae30f
d4ea3b2
808e065
2f5d75e
d53cc00
b156882
46c201e
c8c0597
3e8a4d4
6baa606
8fb923a
655b771
0b86a0b
15e2770
a4bd346
fdfb0b6
a6ab3b4
c05c487
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ import { | |
proxyOverride, | ||
quote, | ||
quotePowerShell, | ||
snakeToCamel, | ||
} from './util.js' | ||
|
||
const CWD = Symbol('processCwd') | ||
|
@@ -81,7 +82,7 @@ export interface Options { | |
verbose: boolean | ||
sync: boolean | ||
env: NodeJS.ProcessEnv | ||
shell: string | boolean | ||
shell: string | true | ||
nothrow: boolean | ||
prefix: string | ||
postfix: string | ||
|
@@ -97,29 +98,65 @@ export interface Options { | |
killSignal?: NodeJS.Signals | ||
halt?: boolean | ||
} | ||
// prettier-ignore | ||
export const defaults: Options = { | ||
[CWD]: process.cwd(), | ||
[SYNC]: false, | ||
verbose: false, | ||
env: process.env, | ||
sync: false, | ||
shell: true, | ||
stdio: 'pipe', | ||
nothrow: false, | ||
quiet: false, | ||
prefix: '', | ||
postfix: '', | ||
detached: false, | ||
preferLocal: false, | ||
spawn, | ||
spawnSync, | ||
log, | ||
kill, | ||
killSignal: SIGTERM, | ||
timeoutSignal: SIGTERM, | ||
|
||
export function getZxDefaults( | ||
defs: Options, | ||
prefix: string = 'ZX_', | ||
env = process.env | ||
) { | ||
const types: Record<PropertyKey, Array<'string' | 'boolean'>> = { | ||
cwd: ['string'], | ||
shell: ['string', 'boolean'], | ||
halt: ['boolean'], | ||
preferLocal: ['string', 'boolean'], | ||
input: ['string'], | ||
} | ||
|
||
const o = Object.entries(env).reduce<Record<string, string | boolean>>( | ||
(m, [k, v]) => { | ||
if (k.startsWith(prefix) && v) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor optimization: |
||
const _k = snakeToCamel(k.slice(prefix.length)) | ||
const _v = { true: true, false: false }[v.toLowerCase()] ?? v | ||
if ( | ||
[typeof defs[_k as keyof Options], ...(types[_k] ?? [])].includes( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought the same, but output from my Browser console
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but in your case it works)
|
||
typeof _v | ||
) | ||
) { | ||
m[_k] = _v | ||
} | ||
} | ||
return m | ||
}, | ||
{} | ||
) | ||
return Object.assign(defs, o) | ||
} | ||
|
||
export const defaults: Options = getZxDefaults( | ||
// prettier-ignore | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep the original formatting here for git blame There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I restored formatting, but it didn't help (: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was there and extended only to the closest expression - the function call... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was hard, but I did it. |
||
[CWD]: process.cwd(), | ||
[SYNC]: false, | ||
verbose: false, | ||
env: process.env, | ||
sync: false, | ||
shell: true, | ||
stdio: 'pipe', | ||
nothrow: false, | ||
quiet: false, | ||
prefix: '', | ||
postfix: '', | ||
detached: false, | ||
preferLocal: false, | ||
spawn, | ||
spawnSync, | ||
log, | ||
kill, | ||
killSignal: SIGTERM, | ||
timeoutSignal: SIGTERM, | ||
} | ||
) | ||
|
||
// prettier-ignore | ||
export interface Shell< | ||
S = false, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we should restrict the accepted env opts:
sync, nothrow, input, halt, stdio
can break the script logic, whileverbose, shell, quiet, preferLocal
just change its behaviour. Moreover,timeout
does not belong todefaults
, but it looks reasonable to set externally.And if somebody is setting
ZX_CWD=true
, wrong typecast to bool is the least of problems. So maybe we can skip the type check at allThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plz clarify what you suggest:
determine only one list of acceptable properties (
verbose, shell, quiet, preferLocal
) and try it extract from env with type-checkingdetermine two lists:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You insistently suggest pass Options to
reduce
method - it isn't work in this placem[_k] = _v
. Since_k
is random key of Options,m[_k]
has typenever
as union ofstring
,boolean
,StdioInput
and other values of Options.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case we can assign to
verbose
andquiet
some string value. It's not good, in my opinion.So, I combined 'allowed' and type-check together into
types
and use only it.