ts-to-html
Version:
TS and SASS compiler for a HTML with live preview
39 lines (38 loc) • 1.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
function env() {
let path = (0, node_path_1.resolve)(process.cwd(), '.env');
const parse = (src) => {
// eslint-disable-next-line
const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg; // prettier-ignore
const obj = {};
let lines = src.toString('utf8').replace(/\r\n?/mg, '\n'), match;
while ((match = LINE.exec(lines))) {
let key = match[1], value = match[2] || "";
value = value.trim();
// Check if double quoted
const maybeQuote = value[0];
value = value.replace(/^(['"`])([\s\S]*)\1$/mg, '$2');
if (maybeQuote === '"')
value = value.replace(/\\n/g, '\n').replace(/\\r/g, '\r');
obj[key] = value;
}
return obj;
};
try {
const parsed = parse((0, node_fs_1.readFileSync)(path, { encoding: 'utf8' }));
// Don't override the existing arguments
Object.keys(parsed).forEach(key => {
if (!Object.prototype.hasOwnProperty.call(process.env, key)) {
process.env[key] = parsed[key];
}
});
return parsed;
}
catch (error) {
return { error };
}
}
exports.default = env;