Last active
November 8, 2018 18:34
-
-
Save rawnly/53c5a0173868ba99b10ad2783da61565 to your computer and use it in GitHub Desktop.
Simple settings manager for nodejs apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Settings = require('./Settings').shared; | |
// or | |
const Settings = require('./Settings')('~/my-app-settings.json'); | |
const { prompt } = require('inquirer'); | |
(async () => { | |
const wait = delay => new Promise(resolve => setTimeout(() => resolve(), delay)); | |
try { | |
const answers = await prompt([{ | |
name: 'firstName', | |
message: 'What\'s your name?', | |
prefix: 'i.', | |
default: 'John', | |
filter: input => input.trim(), | |
validate: input => input.length > 0 | |
}, { | |
name: 'lastName', | |
message: 'And your last name?', | |
prefix: 'ii.', | |
default: 'Doe', | |
filter: input => input.trim(), | |
validate: input => input.length > 0 | |
}, { | |
name: 'bday', | |
message: 'When are you born?', | |
prefix: 'iii.', | |
default: new Date().toLocaleString(), | |
when: a => a.firstName && a.lastName | |
}]); | |
const user = {}; | |
user.first = answers.firstName; | |
user.last = answers.lastName; | |
user.bday = new Date(answers.bday); | |
user.age = Math.abs(new Date().getFullYear() - new Date(answers.bday).getFullYear()); | |
Settings.set('user', user); | |
await wait(500); | |
console.log({ | |
exists: Settings.has('user'), | |
content: Settings.get('user') | |
}); | |
await wait(500); | |
Settings.delete('user'); | |
console.log({ | |
exists: Settings.has('user'), | |
content: Settings.get('user') | |
}); | |
} catch (error) { | |
throw error; | |
} | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { existsSync, writeFileSync, readFileSync } = require('fs'); | |
const { join } = require('path') | |
class Settings { | |
static shared = new Settings() | |
filepath = path.join(__dirname, 'settings.json') | |
constructor(filePath) { | |
this.initialize() | |
if (filePath) { | |
this.setFilePath(filePath) | |
} else { | |
this.setFilePath(path.join(__dirname, 'settings.json'); | |
} | |
} | |
setFilePath(fp) { | |
this.filepath = fp | |
} | |
initialize() { | |
if (existsSync(this.filepath) === false) { | |
writeFileSync(this.filepath, JSON.stringify({}, null, 2)); | |
} | |
} | |
static has(key) { | |
const settings = JSON.parse(readFileSync(this.filepath, 'utf8')); | |
return settings[key] !== undefined; | |
} | |
static get(key) { | |
const settings = JSON.parse(readFileSync(this.filepath, 'utf8')); | |
if (key) return settings[key]; | |
return settings; | |
} | |
static set(key, value) { | |
const settings = JSON.parse(readFileSync(this.filepath, 'utf8')); | |
const data = {}; | |
data[key] = value; | |
writeFileSync(this.getFilePath(), JSON.stringify(Object.assign({}, settings, data), null, 2)); | |
} | |
static delete(key) { | |
const settings = JSON.parse(readFileSync(this.filepath, 'utf8')); | |
delete settings[key]; | |
writeFileSync(this.getFilePath(), JSON.stringify(settings, null, 2)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment