Skip to content

Commit

Permalink
Fix argument/settings parsing
Browse files Browse the repository at this point in the history
Before, both structs were being polluted with the environment variables.
The way it's done now, it is no longer poluted and (in my opinion) is a
bit cleaner as well.
  • Loading branch information
Noodlez1232 committed Oct 29, 2024
1 parent cd53feb commit f67d4a3
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/lib/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,29 @@ fApi.extend({
},
});

export const settings = db<Settings>("./db/settings.json", { template: defaultSettings, pretty: true, forceCreate: true, updateOnExternalChanges: true });
recursiveUpdate(settings, defaultSettings);

const argv = ARGV(process.argv.slice(2))<PartialArgs>({});
rebuildTypes(argv, { ...defaultSettings, ...defaultArgs });
recursiveUpdate(settings, argv, { setUndefined: false, setDefined: true });

const env = getEnv();
rebuildTypes(env, { ...defaultSettings, ...defaultArgs });

export const args = defaultArgs;
recursiveUpdate(args, env, { setUndefined: false, setDefined: true });
recursiveUpdate(args, argv, { setUndefined: false, setDefined: true });

export const settings = defaultSettings;
recursiveUpdate(settings,
db<Settings>("./db/settings.json", { template: defaultSettings, pretty: true, forceCreate: true, updateOnExternalChanges: true }),
{ setUndefined: true, setDefined: true });

recursiveUpdate(settings, argv, { setUndefined: false, setDefined: true });

if (env.__FPDSettings !== undefined) {
if (typeof env.__FPDSettings !== "string") throw new Error("The __FPDSettings environment variable cannot be parsed!");
recursiveUpdate(settings, parse(env.__FPDSettings.replaceAll('\\"', '"')), { setUndefined: false, setDefined: true });
}

recursiveUpdate(settings, env, { setUndefined: false, setDefined: true });
console.log(settings);


export const args = { ...argv, ...env };

// eslint-disable-next-line no-control-regex
const headlessStdoutRegex = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
Expand Down

0 comments on commit f67d4a3

Please sign in to comment.