Skip to content

Commit

Permalink
validate setting name
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Nov 24, 2024
1 parent 79320fd commit 1026fc2
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { existsSync, readFileSync, writeFileSync } from 'fs'
import { exit, verifyDirectory, resolve } from './utils'
import yaml from 'js-yaml'
import * as path from 'path'
import { verifyDirectory, resolve } from './utils'

const VALID_NAMES = [ 'services_home' ]

function filename(): string {
return path.join(process.env.STAX_HOME, 'settings.yaml')
Expand All @@ -11,15 +13,23 @@ 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: function() { return load() },

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

write: function(name, value): any {
write: function(name: string, value): any {
validateName(name)

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

const settings = { ...load() }
Expand Down

0 comments on commit 1026fc2

Please sign in to comment.