arg-env
Version:
Node.js package to work with `.env` files in the same way as docker and docker-compose via `--env-file` or `"env_file"` in package.json
27 lines (26 loc) • 1.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = void 0;
var parser = /^\s*([^\s=]+)=(?<quote>['"]?)(?<value>.*)(\k<quote>)/gm, commentsStripper = /\s#.*/, exprParse = /\$\{([^\}]+?)(:-([^\}]*))?\}/g;
function parse(src, scope, reserved) {
// TODO Line
var source = typeof src === "string" ? src : src.toString(), $return = {}, replacer = function (_, variable, __, $default) {
var _a;
if ($default === void 0) { $default = ""; }
return scope && variable in scope ? (_a = scope[variable]) !== null && _a !== void 0 ? _a : $default
: variable in $return ? $return[variable]
: $default;
};
var parsed;
while (parsed = parser.exec(source)) {
var _a = parsed, key = _a[1], value = _a["groups"].value;
if (reserved && key in reserved
|| scope && key in scope)
continue;
$return[key] = value
.replace(commentsStripper, "")
.replace(exprParse, replacer);
}
return $return;
}
exports.parse = parse;