Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move various node dependencies into flowr-cli #756

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 0 additions & 63 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@
"@types/chai": "^4.3.11",
"@types/chai-as-promised": "^7.1.8",
"@types/mocha": "^10.0.6",
"@types/n-readlines": "^1.0.6",
"@types/n3": "^1.16.4",
"@types/object-hash": "^3.0.6",
"@types/semver": "^7.5.6",
Expand All @@ -254,8 +253,6 @@
"@j-ulrich/release-it-regex-bumper": "^5.1.0"
},
"dependencies": {
"joi": "^17.11.0",
"n-readlines": "^1.0.1",
"n3": "^1.17.2",
"object-hash": "^3.0.0",
"rotating-file-stream": "^3.1.1",
Expand Down
63 changes: 1 addition & 62 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import type { MergeableRecord } from './util/objects'
import { deepMergeObject } from './util/objects'
import path from 'path'
import fs from 'fs'
import { log, LogLevel } from './util/log'
import { getParentDirectory } from './util/files'
import Joi from 'joi'

export interface FlowrConfigOptions extends MergeableRecord {
/**
Expand All @@ -21,68 +15,13 @@ export const defaultConfigOptions: FlowrConfigOptions = {
ignoreSourceCalls: false,
rPath: undefined
}
export const defaultConfigFile = 'flowr.json'

const schema = Joi.object({
ignoreSourceCalls: Joi.boolean().optional()
})

let configWorkingDirectory = process.cwd()
let configFile = defaultConfigFile
let currentConfig: FlowrConfigOptions | undefined

export function setConfigFile(workingDirectory = process.cwd(), file = defaultConfigFile, forceLoad = false) {
configWorkingDirectory = workingDirectory
configFile = file

// reset the config so it gets reloaded
currentConfig = undefined
if(forceLoad) {
getConfig()
}
}

export function setConfig(config: FlowrConfigOptions) {
currentConfig = config
}

export function getConfig(): FlowrConfigOptions {
// lazy-load the config based on the current settings
if(currentConfig === undefined) {
setConfig(parseConfigOptions(configWorkingDirectory, configFile))
}
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) {
// assign default values to all config options except for the specified ones
const ret = deepMergeObject(defaultConfigOptions, parsed)
if(log.settings.minLevel <= LogLevel.Info) {
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}`)
}
}
// move up to parent directory
searchPath = getParentDirectory(searchPath)
} while(fs.existsSync(searchPath))

if(log.settings.minLevel <= LogLevel.Info) {
log.info(`Using default config ${JSON.stringify(defaultConfigOptions)}`)
}
return defaultConfigOptions
return currentConfig ?? defaultConfigOptions
}
139 changes: 0 additions & 139 deletions src/util/files.ts

This file was deleted.

Loading