merge-anything
merge-anything copied to clipboard
merged missing keys
So I've verified the toMerge
, has the expected object, with the expected key, seems though I'm only getting the keys from process.env
in my resulting merged object, even though the keys in toMerge
do not exist in process.env
. the key is just LOG_LEVEL: "silly"
, error is AssertionError [ERR_ASSERTION]: LOG_LEVEL is not set
import { DI, IContainer, IRegistration, IResolver, Injectable, Resolved } from '@aurelia/kernel';
import assert from 'assert';
import { constantCase } from 'change-case';
import { env } from 'process';
import { merge } from 'merge-anything';
import dotenv, { DotenvParseOutput } from 'dotenv';
import path from 'path';
import findup from 'find-up';
import fs from 'fs';
function pushEnv(filepath: string, toMerge: DotenvParseOutput[]): void {
const path = filepath.trim() === '' ? undefined : filepath.trim();
if (path && fs.existsSync(path)) {
const buffer = fs.readFileSync(path);
const envMap = dotenv.parse(buffer);
toMerge.push(envMap);
}
}
export class EnvironmentResolver implements IResolver<unknown>, IRegistration {
static required(key: string): IResolver {
return new EnvironmentResolver(key, true);
}
static optional(key: string): IResolver {
return new EnvironmentResolver(key, false);
}
private constructor(private readonly key: string, required: boolean) {
const toMerge: DotenvParseOutput[] = [];
const envFile = findup.sync('.env');
const envRoot = envFile ? path.dirname(envFile) : undefined;
if (envRoot) {
pushEnv(`${envRoot}/.env.defaults`, toMerge);
pushEnv(`${envRoot}/.env`, toMerge);
pushEnv(`${envRoot}/.env.${env['NODE_ENV']}`, toMerge);
}
pushEnv(`${env['DOTENV_PATH']}`, toMerge);
const merged = merge({} as DotenvParseOutput, ...toMerge, env);
const envVar = constantCase(key);
if (required) {
assert(merged[envVar], `${envVar} is not set`);
}
}
readonly $isResolver: true = true;
register(container: IContainer): IResolver<void> {
return container.registerResolver(this.key, this);
}
resolve(): Resolved<unknown> {
const envVar = constantCase(this.key);
return env[envVar];
}
}
yarn info -A merge-anything
└─ merge-anything@npm:3.0.6
├─ Version: 3.0.6
│
└─ Dependencies
├─ is-what@npm:^3.11.2 → npm:3.11.2
└─ ts-toolbelt@npm:8.0 → npm:8.0.7
Can you show me how the DotenvParseOutput
type looks
Also, can you show me a screenshot of the exact error with some lines leading up to the error ? Or if it's an inline VSCode error, show me a screenshot where you get the red squiggly lines.
export interface DotenvParseOutput {
[name: string]: string;
}
exact error with some lines leading up to the error ?
I'm not entirely certain what you mean, the error that is emitted is my own saying the key is missing, and it's in the code I pasted above. that's the entirety of the code.
I'm not sure I understand the issue : (
the issue is that the merge didn't work... at all... honestly I suspect (after further dealings with it) that this has something to do with dotenv.parse
returning a proxy. Which makes me a little uncertain if this is a bug on their end or your end, as I'm not certain how you're attempting to access the keys or whether or not this should work.
why don't you use the debugger to check at this line? const merged = merge({} as DotenvParseOutput, ...toMerge, env);
I did... that's when I opened the bug.
with the debugger you should be able to determine at exactly which line what is going wrong
ok, I have a minimal (I think) reproduction of this, with a test https://github.com/xenoterracide/merge-anything-compilation-issue,
before you can work with this, you'll have to fix the compilation issue mentioned in #13. You can just edit the .yarnrc.yml
file, and uncomment the merge-anything
lines, and then run yarn && yarn test
.
@xenoterracide can you try on the latest version, I rewrote all the typescript myself and removed ts-toolbelt! 🎉
It's been so long... I've moved on twice since I opened this bug.
@xenoterracide feel free to open a new issue if you get stuck again! 😁