Skip to content

Commit

Permalink
feat-fix: only load flowr config from disk through the repl
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellpeck committed Sep 19, 2024
1 parent c9d6aea commit 5dd51e2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
5 changes: 3 additions & 2 deletions src/cli/flowr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { log, LogLevel } from '../util/log';
import { bold, ColorEffect, Colors, FontStyles, formatter, italic, setFormatter, voidFormatter } from '../util/ansi';
import commandLineArgs from 'command-line-args';
import commandLineUsage from 'command-line-usage';
import { defaultConfigFile, setConfigFile } from '../config';
import { setConfigFile } from '../config';
import { guard } from '../util/assert';
import type { ScriptInformation } from './common/scripts-info';
import { scripts } from './common/scripts-info';
Expand Down Expand Up @@ -99,7 +99,8 @@ if(options['no-ansi']) {
setFormatter(voidFormatter);
}

setConfigFile(undefined, options['config-file'] ?? defaultConfigFile, true);
export const defaultConfigFile = 'flowr.json';
setConfigFile(options['config-file'] ?? defaultConfigFile, undefined, true);

function retrieveShell(): RShell {
// we keep an active shell session to allow other parse investigations :)
Expand Down
57 changes: 30 additions & 27 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ export const defaultConfigOptions: FlowrConfigOptions = {
ignoreSourceCalls: false,
rPath: undefined
};
export const defaultConfigFile = 'flowr.json';

const schema = Joi.object({
ignoreSourceCalls: Joi.boolean().optional()
ignoreSourceCalls: Joi.boolean().optional(),
rPath: Joi.string().optional()
});

// we don't load from a config file at all by default unless setConfigFile is called
let configFile: string | undefined = undefined;
let configWorkingDirectory = process.cwd();
let configFile = defaultConfigFile;
let currentConfig: FlowrConfigOptions | undefined;

export function setConfigFile(workingDirectory = process.cwd(), file = defaultConfigFile, forceLoad = false) {
configWorkingDirectory = workingDirectory;
export function setConfigFile(file: string | undefined, workingDirectory = process.cwd(), forceLoad = false) {
configFile = file;
configWorkingDirectory = workingDirectory;

// reset the config so it gets reloaded
currentConfig = undefined;
Expand All @@ -49,35 +50,37 @@ export function setConfig(config: FlowrConfigOptions) {
export function getConfig(): FlowrConfigOptions {
// lazy-load the config based on the current settings
if(currentConfig === undefined) {
setConfig(parseConfigOptions(configWorkingDirectory, configFile));
setConfig(parseConfigOptions(configFile, configWorkingDirectory));
}
return currentConfig as FlowrConfigOptions;
}

function parseConfigOptions(workingDirectory: string, configFile: string): FlowrConfigOptions {
let searchPath = path.resolve(workingDirectory);
do{
const configPath = path.join(searchPath, configFile);
if(fs.existsSync(configPath)) {
try {
const text = fs.readFileSync(configPath,{ encoding: 'utf-8' });
const parsed = JSON.parse(text) as FlowrConfigOptions;
const validate = schema.validate(parsed);
if(!validate.error) {
function parseConfigOptions(configFile: string | undefined, workingDirectory: string): FlowrConfigOptions {
if(configFile !== undefined) {
let searchPath = path.resolve(workingDirectory);
do{
const configPath = path.join(searchPath, configFile);
if(fs.existsSync(configPath)) {
try {
const text = fs.readFileSync(configPath,{ encoding: 'utf-8' });
const parsed = JSON.parse(text) as FlowrConfigOptions;
const validate = schema.validate(parsed);
if(!validate.error) {
// assign default values to all config options except for the specified ones
const ret = deepMergeObject(defaultConfigOptions, parsed);
log.info(`Using config ${JSON.stringify(ret)} from ${configPath}`);
return ret;
} else {
log.error(`Failed to validate config file at ${configPath}: ${validate.error.message}`);
const ret = deepMergeObject(defaultConfigOptions, parsed);
log.info(`Using config ${JSON.stringify(ret)} from ${configPath}`);
return ret;
} else {
log.error(`Failed to validate config file at ${configPath}: ${validate.error.message}`);
}
} catch(e) {
log.error(`Failed to parse config file at ${configPath}: ${(e as Error).message}`);
}
} catch(e) {
log.error(`Failed to parse config file at ${configPath}: ${(e as Error).message}`);
}
}
// move up to parent directory
searchPath = getParentDirectory(searchPath);
} while(fs.existsSync(searchPath));
// move up to parent directory
searchPath = getParentDirectory(searchPath);
} while(fs.existsSync(searchPath));
}

log.info(`Using default config ${JSON.stringify(defaultConfigOptions)}`);
return defaultConfigOptions;
Expand Down

0 comments on commit 5dd51e2

Please sign in to comment.