Skip to content

Commit

Permalink
allow any setting name
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Nov 29, 2024
1 parent fd0f6a6 commit 04a9914
Showing 1 changed file with 2 additions and 12 deletions.
14 changes: 2 additions & 12 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { exit, verifyDirectory, resolve } from './utils'
import yaml from 'js-yaml'
import * as path from 'path'

const VALID_NAMES = [ 'aliases','services_home', 'editor', 'environment' ]

function filename(): string {
return path.join(process.env.STAX_HOME, 'settings.yaml')
}
Expand All @@ -13,25 +11,17 @@ function load() {
return (existsSync(filename()) && yaml.load(readFileSync(filename(), 'utf-8'))) || {}
}

function validateName(name: string) {
if (!VALID_NAMES.includes(name))
exit(1, { message: `Invalid setting name: ${name}\nValid names are: ${VALID_NAMES.join(', ')}` } )
}

const settings = {
all: load,

isValidName: (name: string) => VALID_NAMES.includes(name),
isSettingName: (name: string) => Object.keys(load()).includes(name),

read: (name: string, defaultValue: any | undefined = undefined) => {
validateName(name)
const settings = load()
return settings[name] || defaultValue
},

write: (name: string, value: any) => {
validateName(name)

if (name === 'services_home') verifyDirectory(value = resolve(value))

const settings = { ...load() }
Expand All @@ -41,7 +31,7 @@ const settings = {
},

interpolate: (filePath: string) => {
return filePath.replace(/\$(\w+)/g, (match, name) => settings.isValidName(name) ? settings.read(name) : match)
return filePath.replace(/\$(\w+)/g, (match, name) => settings.isSettingName(name) ? settings.read(name) : match)
}
}

Expand Down

0 comments on commit 04a9914

Please sign in to comment.