JavaScript: node:parseEnv sucks really badly.
This is my version.
Loads all ENV vars to the process.env
- ignore comments (any amount of spaces then
#
) - any
=
after the first is part of the value - keys will always be uppercase and spaces replaced with
_
(await readFile(RC_ENVFILE, 'utf-8').catch(_ => ''))
.split('\n').map(line => line.trim()).filter(line => line.length > 0 && !line.startsWith('#'))
.map(line => line.split(/=(.*)/).map(part => part.trim()) )
.filter(([key,val]) => key && val)
.forEach(([key,val]) => process.env[key!.toUpperCase().replaceAll(' ', '_')] = val);