Commit a926e98d authored by Dan Allen's avatar Dan Allen

merge !41

resolves #52 camelCase keys when exporting playbook model from convict config
parents 9459f84f 2764cf23
Pipeline #14727260 passed with stages
in 2 minutes and 48 seconds
......@@ -34,6 +34,7 @@
},
"dependencies": {
"asciidoctor.js": "^1.5.6-preview.4",
"camelcase-keys": "^4.2.0",
"convict": "^4.0.2",
"cson-parser": "^2.0.0",
"deep-freeze": "^0.0.1",
......
......@@ -43,7 +43,7 @@ module.exports = {
arg: 'google-analytics-key',
},
swiftype: {
doc: 'The key to activate the SwiftType widget.',
doc: 'The key to activate the Swiftype widget.',
format: String,
default: undefined,
arg: 'swiftype-key',
......@@ -107,15 +107,15 @@ module.exports = {
},
},
urls: {
htmlExtensionStyle: {
html_extension_style: {
doc: 'Controls how the URL extension for HTML pages is handled (default, drop, or indexify).',
format: ['default', 'drop', 'indexify'],
default: 'default',
arg: 'html-url-extension-style',
},
aspectPageStrategy: {
aspect_page_strategy: {
doc: 'Controls how links to pages in aspect domains are generated (path or query).',
format: String,
format: ['path', 'query'],
default: 'path',
arg: 'aspect-page-url-strategy',
},
......
'use strict'
const camelCaseKeys = require('camelcase-keys')
const convict = require('./solitary-convict')
const cson = require('cson-parser')
const freeze = require('deep-freeze')
const freezeDeep = require('deep-freeze')
const fs = require('fs')
const path = require('path')
const yaml = require('js-yaml')
......@@ -25,22 +26,26 @@ const parseSpecFile = (specFilePath) => {
}
}
const exportPlaybookModel = (config) => {
const playbook = camelCaseKeys(config.getProperties(), { deep: true })
// playbook property is private; should not leak
delete playbook.playbook
return freezeDeep(playbook)
}
module.exports = (args, env, schema) => {
const config = loadConvictConfig(args, env, schema)
const specFileRelPath = config.get('playbook')
if (!specFileRelPath) {
if (specFileRelPath) {
let specFileAbsPath = path.resolve(process.cwd(), specFileRelPath)
if (!path.extname(specFileAbsPath)) specFileAbsPath += '.yml'
config.load(parseSpecFile(specFileAbsPath))
} else {
throw new Error('Spec file for playbook not specified')
}
let specFileAbsPath = path.resolve(process.cwd(), specFileRelPath)
if (!path.extname(specFileAbsPath)) specFileAbsPath += '.yml'
config.load(parseSpecFile(specFileAbsPath))
config.validate({ allowed: 'strict' })
const playbook = config.getProperties()
// playbook property is private; should not leak
delete playbook.playbook
return freeze(playbook)
return exportPlaybookModel(config)
}
site:
url: https://example.com
title: Example site
keys:
google_analytics: 'XX-123456'
......@@ -27,6 +27,11 @@ describe('buildPlaybook()', () => {
format: String,
default: 'default-value',
},
widget_key: {
format: String,
default: undefined,
env: 'WIDGET_KEY',
},
},
two: {
format: Number,
......@@ -49,6 +54,7 @@ describe('buildPlaybook()', () => {
expectedPlaybook = {
one: {
two: 'default-value',
widgetKey: undefined,
},
two: 42,
three: false,
......@@ -119,6 +125,12 @@ describe('buildPlaybook()', () => {
expect(playbook.one.one).to.equal('the-args-value')
})
it('should convert properties of playbook to camelCase', () => {
const env = { PLAYBOOK: ymlSpec, WIDGET_KEY: 'xxxyyyzzz' }
const playbook = buildPlaybook([], env, schema)
expect(playbook.one.widgetKey).to.equal('xxxyyyzzz')
})
it('should coerce Number values in spec file', () => {
const playbook = buildPlaybook([], { PLAYBOOK: ymlSpec }, schema)
expect(playbook.two).to.equal(42)
......@@ -178,6 +190,7 @@ describe('buildPlaybook()', () => {
const playbook = buildPlaybook([], { PLAYBOOK: defaultSchemaSpec })
expect(playbook.site.url).to.equal('https://example.com')
expect(playbook.site.title).to.equal('Example site')
expect(playbook.site.keys.googleAnalytics).to.equal('XX-123456')
})
it('is decoupled from the process environment', () => {
......
......@@ -492,6 +492,14 @@ camelcase-keys@^4.1.0:
map-obj "^2.0.0"
quick-lru "^1.0.0"
camelcase-keys@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/camelcase-keys/-/camelcase-keys-4.2.0.tgz#a2aa5fb1af688758259c32c141426d78923b9b77"
dependencies:
camelcase "^4.1.0"
map-obj "^2.0.0"
quick-lru "^1.0.0"
camelcase@^1.0.2:
version "1.2.1"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-1.2.1.tgz#9bb5304d2e0b56698b2c758b08a3eaa9daa58a39"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment