diff --git a/index.js b/index.js index 654c56f..793a413 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ const path = require('path'); const YUIDocsGenerator = require('ember-cli-addon-docs-yuidoc/lib/broccoli/generator'); const Funnel = require('broccoli-funnel'); const mergeTrees = require('broccoli-merge-trees'); -const { parse, generatePreviewHead } = require('./lib/util'); +const { parse, generatePreviewHead, overrideEnvironment } = require('./lib/util'); module.exports = { name: require('./package').name, @@ -74,6 +74,7 @@ module.exports = { const previewHeadFilePath = path.resolve(process.cwd(), '.storybook/preview-head.html'); const previewHeadDirectory = path.dirname(previewHeadFilePath); const envFilePath = path.resolve(process.cwd(), '.env'); + const environmentOverridePath = path.resolve(process.cwd(), '.storybook/environment.js'); let fileContents = ''; @@ -93,6 +94,22 @@ module.exports = { this.ui.writeDebugLine('Generating preview-head.html'); + const environment = parsedConfig.meta.find(meta => meta.name.endsWith('config/environment')); + + if (environment) { + // From the Ember App's environment.js file + const original = JSON.parse(decodeURIComponent(environment.content)); + + // When rootURL is anything other than "/" routing can't be started without erroring, so + // this is a sensible default. + const defaultOverride = { rootURL: '/' }; + + // Allow arbitrary overriding in the storybook environment + const environmentOverride = fs.existsSync(environmentOverridePath) && require(environmentOverridePath)(process.env); + + environment.content = encodeURIComponent(JSON.stringify(overrideEnvironment(original, defaultOverride, environmentOverride))); + } + if(config) { this.ui.writeDebugLine('Setting up overrides.'); diff --git a/lib/util.js b/lib/util.js index 46c55c6..89785c5 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,4 +1,5 @@ const cheerio = require('cheerio'); +const merge = require('lodash.merge'); const lookupTable = { meta: [{ @@ -146,9 +147,14 @@ function generatePreviewHead(parsedConfig) { return doc.join('\n') } +function overrideEnvironment(env, ...overrides) { + return merge(env, ...overrides); +} + module.exports = { getDocumentValues, parse, objectToHTMLAttributes, generatePreviewHead, + overrideEnvironment, }; diff --git a/package.json b/package.json index e9a0891..9ac1eec 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,8 @@ "cheerio": "^1.0.0-rc.2", "ember-cli-addon-docs-yuidoc": "^1.0.0", "ember-cli-babel": "^7.23.0", - "ember-cli-htmlbars": "^5.3.1" + "ember-cli-htmlbars": "^5.3.1", + "lodash.merge": "^4.6.2" }, "publishConfig": { "access": "public"