Repository: maizzle/framework
Pull request #613 (2022-02-04 18:28:33)

feat: use config file in render method if available
<!--

Thank you for your interest in contributing to Maizzle!

**Please ask first before starting work on any significant new features.**

https://github.com/maizzle/framework/blob/master/.github/CONTRIBUTING.md

-->



Patch:
diff --git a/src/generators/output/to-string.js b/src/generators/output/to-string.js
index 204fcfb..6cfdb4d 100644
--- a/src/generators/output/to-string.js
+++ b/src/generators/output/to-string.js
@@ -4,6 +4,7 @@ const posthtml = require('../posthtml')
 const Tailwind = require('../tailwindcss')
 const Transformers = require('../../transformers')
 const posthtmlMSO = require('../../transformers/posthtmlMso')
+const Config = require('../config')
 
 module.exports = async (html, options) => {
   process.env.NODE_ENV = get(options, 'maizzle.env', 'local')
@@ -16,7 +17,9 @@ module.exports = async (html, options) => {
     throw new RangeError('received empty string')
   }
 
-  let config = get(options, 'maizzle', {})
+  const fileConfig = await Config.getMerged(process.env.NODE_ENV)
+
+  let config = merge(fileConfig, get(options, 'maizzle', {}))
 
   const tailwindConfig = get(options, 'tailwind.config', {})
   const cssString = get(options, 'tailwind.css', '@tailwind components; @tailwind utilities;')


Test patch:
diff --git a/test/expected/useConfig.html b/test/expected/useConfig.html
new file mode 100644
index 0000000..c4b16b3
--- /dev/null
+++ b/test/expected/useConfig.html
@@ -0,0 +1,9 @@
+<!doctype html>
+<html>
+  <head>
+    <title>test></title>
+  </head>
+  <body>
+    <div>build_production</div>
+  </body>
+</html>
diff --git a/test/fixtures/useConfig.html b/test/fixtures/useConfig.html
new file mode 100644
index 0000000..ca47b2d
--- /dev/null
+++ b/test/fixtures/useConfig.html
@@ -0,0 +1,9 @@
+<!doctype html>
+<html>
+  <head>
+    <title>test></title>
+  </head>
+  <body>
+    <div>{{ page.build.templates.destination.path }}</div>
+  </body>
+</html>
diff --git a/test/test-todisk.js b/test/test-todisk.js
index c598269..8e9f7ac 100644
--- a/test/test-todisk.js
+++ b/test/test-todisk.js
@@ -16,12 +16,12 @@ test.afterEach.always(async t => {
 
 test('throws if config cannot be computed', async t => {
   await t.throwsAsync(async () => {
-    await Maizzle.build('production')
-  }, {instanceOf: Error, message: `could not load config.production.js`})
+    await Maizzle.build('missing')
+  }, {instanceOf: Error, message: `could not load config.missing.js`})
 })
 
 test('skips if no templates found', async t => {
-  const {files} = await Maizzle.build('production', {
+  const {files} = await Maizzle.build('maizzle-ci', {
     build: {
       fail: 'silent',
       templates: {
@@ -43,7 +43,7 @@ test('skips if no templates found', async t => {
 })
 
 test('outputs files at the correct location', async t => {
-  const {files: string} = await Maizzle.build('production', {
+  const {files: string} = await Maizzle.build('maizzle-ci', {
     build: {
       fail: 'silent',
       templates: {
@@ -55,7 +55,7 @@ test('outputs files at the correct location', async t => {
     }
   })
 
-  const {files: array} = await Maizzle.build('production', {
+  const {files: array} = await Maizzle.build('maizzle-ci', {
     build: {
       fail: 'silent',
       templates: {
@@ -98,7 +98,7 @@ test('outputs files at the correct location if multiple template sources are use
 })
 
 test('copies all files in the `filetypes` option to destination', async t => {
-  const {files} = await Maizzle.build('production', {
+  const {files} = await Maizzle.build('maizzle-ci', {
     build: {
       fail: 'silent',
       templates: {
@@ -121,7 +121,7 @@ test('copies all files in the `filetypes` option to destination', async t => {
 })
 
 test('outputs files with the correct extension', async t => {
-  await Maizzle.build('production', {
+  await Maizzle.build('maizzle-ci', {
     build: {
       fail: 'silent',
       templates: {
@@ -143,7 +143,7 @@ test('outputs files with the correct extension', async t => {
 })
 
 test('outputs plaintext files', async t => {
-  const {files} = await Maizzle.build('production', {
+  const {files} = await Maizzle.build('maizzle-ci', {
     fail: 'silent',
     build: {
       templates: {
@@ -172,7 +172,7 @@ test('outputs plaintext files', async t => {
 })
 
 test('outputs plaintext files (custom path)', async t => {
-  const {files} = await Maizzle.build('production', {
+  const {files} = await Maizzle.build('maizzle-ci', {
     fail: 'silent',
     build: {
       templates: {
@@ -207,7 +207,7 @@ test('renders plaintext string', async t => {
 })
 
 test('copies assets to destination', async t => {
-  await Maizzle.build('production', {
+  await Maizzle.build('maizzle-ci', {
     build: {
       fail: 'silent',
       templates: {
@@ -233,7 +233,7 @@ test('copies assets to destination', async t => {
 })
 
 test('runs the `beforeCreate` event', async t => {
-  await Maizzle.build('production', {
+  await Maizzle.build('maizzle-ci', {
     build: {
       fail: 'silent',
       templates: {
@@ -261,7 +261,7 @@ test('runs the `beforeCreate` event', async t => {
 })
 
 test('runs the `afterBuild` event', async t => {
-  const {files} = await Maizzle.build('production', {
+  const {files} = await Maizzle.build('maizzle-ci', {
     build: {
       fail: 'silent',
       templates: {
@@ -289,7 +289,7 @@ test('runs the `afterBuild` event', async t => {
 })
 
 test('supports multiple asset paths', async t => {
-  await Maizzle.build('production', {
+  await Maizzle.build('maizzle-ci', {
     build: {
       fail: 'silent',
       templates: {
@@ -316,7 +316,7 @@ test('supports multiple asset paths', async t => {
 })
 
 test('warns if a template cannot be rendered and `fail` option is undefined', async t => {
-  const {files} = await Maizzle.build('production', {
+  const {files} = await Maizzle.build('maizzle-ci', {
     build: {
       templates: {
         source: 'test/stubs/breaking',
@@ -336,7 +336,7 @@ test('warns if a template cannot be rendered and `fail` option is undefined', as
 })
 
 test('warns if a template cannot be rendered and `fail` option is `verbose`', async t => {
-  const {files} = await Maizzle.build('production', {
+  const {files} = await Maizzle.build('maizzle-ci', {
     build: {
       fail: 'verbose',
       templates: {
@@ -357,7 +357,7 @@ test('warns if a template cannot be rendered and `fail` option is `verbose`', as
 })
 
 test('warns if a template cannot be rendered and `fail` option is `silent`', async t => {
-  const {files} = await Maizzle.build('production', {
+  const {files} = await Maizzle.build('maizzle-ci', {
     build: {
       fail: 'silent',
       templates: {
@@ -426,7 +426,7 @@ test('throws if it cannot spin up local development server', async t => {
 })
 
 test('works with templates.source defined as function (string paths)', async t => {
-  const {files} = await Maizzle.build('production', {
+  const {files} = await Maizzle.build('maizzle-ci', {
     build: {
       fail: 'silent',
       templates: {
@@ -443,7 +443,7 @@ test('works with templates.source defined as function (string paths)', async t =
 })
 
 test('works with templates.source defined as function (array paths)', async t => {
-  const {files} = await Maizzle.build('production', {
+  const {files} = await Maizzle.build('maizzle-ci', {
     build: {
       fail: 'silent',
       templates: {
diff --git a/test/test-tostring.js b/test/test-tostring.js
index 6a4b731..dba3912 100644
--- a/test/test-tostring.js
+++ b/test/test-tostring.js
@@ -17,6 +17,14 @@ test('compiles HTML string if no options are passed', async t => {
   t.is(html, source)
 })
 
+test('uses environment config file(s) if available', async t => {
+  const source = fixture('useConfig')
+
+  const html = await renderString(source, {maizzle: {env: 'maizzle-ci'}})
+
+  t.is(html, expected('useConfig'))
+})
+
 test('inheritance', async t => {
   let html = await renderString(fixture('inheritance'))
   html = html.replace(/[^\S\r\n]+$/gm, '').trim()