Skip to content

Commit

Permalink
feat: add support for maizzle.config.js files
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin committed Nov 7, 2023
1 parent 22b013b commit c97f5a8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/generators/config.js
@@ -1,6 +1,17 @@
const path = require('path')
const {merge, requireUncached} = require('../utils/helpers')

const baseConfigFileNames = [
'./maizzle.config.js',
'./maizzle.config.cjs',
'./maizzle.config.local.js',
'./maizzle.config.local.cjs',
'./config.js',
'./config.cjs',
'./config.local.js',
'./config.local.cjs'
]

module.exports = {
getMerged: async (env = 'local') => {
if (typeof env !== 'string') {
Expand All @@ -10,17 +21,19 @@ module.exports = {
let baseConfig = {env}
let envConfig = {env}

const cwd = env === 'maizzle-ci' ? './test/stubs/config' : process.cwd()
const cwd = ['maizzle-ci', 'test'].includes(env) ? './test/stubs/config' : process.cwd()

for (const module of ['./config', './config.cjs', './config.local', './config.local.cjs']) {
for (const module of baseConfigFileNames) {
try {
baseConfig = merge(baseConfig, requireUncached(path.resolve(cwd, module)))
} catch {}
}

if (typeof env === 'string' && env !== 'local') {
if (env !== 'local') {
let loaded = false
for (const module of [`./config.${env}`, `./config.${env}.cjs`]) {
const modulesToTry = [`./maizzle.config.${env}.js`, `./maizzle.config.${env}.cjs`, `./config.${env}.js`, `./config.${env}.cjs`]

for (const module of modulesToTry) {
try {
envConfig = merge(envConfig, requireUncached(path.resolve(cwd, module)))
loaded = true
Expand All @@ -29,7 +42,7 @@ module.exports = {
}

if (!loaded) {
throw new Error(`could not load config.${env}.js`)
throw new Error(`Failed to load config file for \`${env}\` environment, do you have one of these files in your project root?\n\n${modulesToTry.join('\n')}`)
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/stubs/config/maizzle.config.test.js
@@ -0,0 +1,11 @@
module.exports = {
file: 'maizzle.config.test.js',
build: {
templates: {
source: '../templates',
destination: {
path: 'build_test'
}
}
}
}
7 changes: 6 additions & 1 deletion test/test-config.js
Expand Up @@ -15,5 +15,10 @@ test('throws if env name is not a string', async t => {
test('throws if a config could not be loaded for the specified environment', async t => {
await t.throwsAsync(async () => {
await Config.getMerged('fake')
}, {instanceOf: Error, message: `could not load config.fake.js`})
}, {instanceOf: Error, message: `Failed to load config file for \`fake\` environment, do you have one of these files in your project root?\n\n./maizzle.config.fake.js\n./maizzle.config.fake.cjs\n./config.fake.js\n./config.fake.cjs`})
})

test('supports maizzle.config.js file names', async t => {
const config = await Config.getMerged('test')
t.is(config.file, 'maizzle.config.test.js')
})
2 changes: 1 addition & 1 deletion test/test-todisk.js
Expand Up @@ -17,7 +17,7 @@ test.afterEach.always(async t => {
test('throws if config cannot be computed', async t => {
await t.throwsAsync(async () => {
await Maizzle.build('missing')
}, {instanceOf: Error, message: `could not load config.missing.js`})
}, {instanceOf: Error})
})

test('skips if no templates found', async t => {
Expand Down

0 comments on commit c97f5a8

Please sign in to comment.