node
node copied to clipboard
Capture `${...}` pattern less strict to throw explicitly
In the hydrate config step, we use src/utils/substitute-with-env.ts
to substitute string patterns like ${<VAR>}
with process.env[<VAR>]
in case it's not nil. We can improve the user experience here and catch invalid formats of <VAR>
.
Examples:
- In case we find
$VAR
(no curly braces), we can at least warn the user that there might be a wrong format. - In case we find
${}
, we can at least warn that the user probably entered a wrong value. - In case we find
${1ABC}
or${A-BC}
, we can at least warn since both are invalid.
More technical context:
Currently, we capture for /\${(\w+)}/g
(1), which substitutes 1+ word characters. Instead we can capture for a less strict /\${(.*)}/g
so that we can explicitly throw when a matched group doesn't suffice constraints of valid env var names, which are /[a-zA-Z_]+[a-zA-Z0-9_]*/
(2)
This improves the usability in a way that users are made aware of potentially invalid template strings, e.g. $VAR
, ${}
, ${1ABC}
, ${A-BC}
. These examples are captured with (1) but fail validation with (2) on which we can provide good error messages.
More info: https://stackoverflow.com/a/2821201/3626075