Skip to content

Commit

Permalink
✨ Add support for env variables as config
Browse files Browse the repository at this point in the history
  • Loading branch information
BetaHuhn committed Jan 13, 2021
1 parent d80b36b commit b5ecb58
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
src/config.json
report.json
report.xml
report.xml
.env
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"axios": "^0.21.0",
"commander": "^6.2.1",
"configstore": "^5.0.1",
"dotenv": "^8.2.0",
"ejs": "^3.1.5",
"feed": "^4.2.1",
"nodemailer": "^6.4.17",
Expand Down
30 changes: 28 additions & 2 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@ const Configstore = require('configstore')
const prompt = require('prompt-sync')({ sigint: true })
const packageJson = require('../package.json')

require('dotenv').config()

const config = new Configstore(packageJson.name, {})

const fields = [ 'ackee.server', 'email.host', 'email.port', 'email.username', 'email.password', 'email.from' ]

const getEnv = (key) => {
const envKey = key.split('.').join('_').toUpperCase()
return process.env[envKey]
}

const verifyField = (field) => {
const existing = config.get(field)
if (existing) return existing

const env = getEnv(field)
if (env !== undefined) {
config.set(field, env)
return
}

const text = field.split('.').join(' ')
const required = !field.includes('email')
const output = text + (required ? ' (required):' : ' (press enter to skip):')
Expand All @@ -22,8 +35,17 @@ const verifyField = (field) => {
return value
}

const loadConfig = function() {
if (!(config.get('ackee.token') || (config.get('ackee.username') && config.get('ackee.password')))) {
const verifyAuth = () => {
const envToken = getEnv('ackee.token')
if (envToken !== undefined) {
config.set('ackee.token', envToken)
return
}

const configHasToken = config.get('ackee.token') !== undefined
const configHasCreds = config.get('ackee.username') !== undefined && config.get('ackee.password') !== undefined

if (configHasToken === false && configHasCreds === false) {
const token = prompt('ackee token (press enter to skip): ')
if (token.length < 1) {

Expand All @@ -34,6 +56,10 @@ const loadConfig = function() {
config.set('ackee.token', token)
}
}
}

const loadConfig = function() {
verifyAuth()

fields.forEach((field) => {
verifyField(field)
Expand Down

0 comments on commit b5ecb58

Please sign in to comment.