Skip to content

Commit

Permalink
Merge pull request #1090 from maizzle/refactor-config
Browse files Browse the repository at this point in the history
  • Loading branch information
cossssmin authored Nov 7, 2023
2 parents 22b013b + d82c1d2 commit 8c7ccb8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
15 changes: 12 additions & 3 deletions src/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,12 @@ const serve = async (env = 'local', config = {}) => {

// Watch for changes in config files
browsersync()
.watch('config*.js')
.watch('{maizzle.config*,config*}.{js,cjs}')
.on('change', async file => {
const parsedEnv = path.parse(file).name.split('.')[1] || 'local'
const fileName = path.parse(file).base
const match = fileName.match(/\.?config\.(.+?)\./)

const parsedEnv = match ? match[1] : env || 'local'

Config
.getMerged(parsedEnv)
Expand Down Expand Up @@ -180,7 +183,13 @@ const serve = async (env = 'local', config = {}) => {
},
tunnel: false,
ui: {port: 3001},
logFileChanges: false
logFileChanges: false,
watchOptions: {
awaitWriteFinish: {
stabilityThreshold: 150,
pollInterval: 25
}
}
},
get(config, 'build.browsersync', {})
), () => {})
Expand Down
23 changes: 18 additions & 5 deletions src/generators/config.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 8c7ccb8

Please sign in to comment.