Skip to content

Commit

Permalink
warn instead of rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Dec 13, 2021
1 parent cea6bd3 commit 64c3c50
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/util/log.js
Expand Up @@ -12,6 +12,10 @@ function log(chalk, messages, key) {
messages.forEach((message) => console.warn(chalk, '-', message))
}

export function dim(input) {
return chalk.dim(input)
}

export default {
info(key, messages) {
log(chalk.bold.cyan('info'), ...(Array.isArray(key) ? [key] : [messages, key]))
Expand Down
18 changes: 12 additions & 6 deletions src/util/normalizeConfig.js
@@ -1,4 +1,4 @@
import log from './log'
import log, { dim } from './log'

export function normalizeConfig(config) {
// Quick structure validation
Expand Down Expand Up @@ -245,12 +245,18 @@ export function normalizeConfig(config) {
})(),
}

// Rewrite globs to prevent bogus globs.
// Validate globs to prevent bogus globs.
// E.g.: `./src/*.{html}` is invalid, the `{html}` should just be `html`
config.content.files = config.content.files.map((file) => {
if (typeof file !== 'string') return file
return file.replace(/{([^,]*?)}/g, '$1')
})
for (let file of config.content.files) {
if (typeof file === 'string' && /{([^,]*?)}/g.test(file)) {
log.warn('invalid-glob-braces', [
`The global pattern ${dim(file)} in your config is invalid.`,
` Update it to ${dim(file.replace(/{([^,]*?)}/g, '$1'))} to silence this warning.`,
// TODO: Add https://tw.wtf/invalid-glob-braces
])
break
}
}

return config
}
18 changes: 14 additions & 4 deletions tests/normalize-config.test.js
Expand Up @@ -110,7 +110,10 @@ it('should keep content files with globs', () => {
})
})

it('should rewrite globs with incorrect bracket expansion', () => {
it('should warn when we detect invalid globs with incorrect brace expansion', () => {
let log = require('../src/util/log')
let spy = jest.spyOn(log.default, 'warn')

let config = {
content: [
'./{example-folder}/**/*.{html,js}',
Expand All @@ -119,13 +122,20 @@ it('should rewrite globs with incorrect bracket expansion', () => {
],
}

// No rewrite happens
expect(normalizeConfig(resolveConfig(config)).content).toEqual({
files: [
'./example-folder/**/*.{html,js}',
'./example-folder/**/*.html',
'./example-folder/**/*.html',
'./{example-folder}/**/*.{html,js}',
'./{example-folder}/**/*.{html}',
'./example-folder/**/*.{html}',
],
extract: {},
transform: {},
})

// But a warning should happen
expect(spy).toHaveBeenCalledTimes(2)
expect(spy.mock.calls.map((x) => x[0])).toEqual(['invalid-glob-braces', 'invalid-glob-braces'])

spy.mockClear()
})

0 comments on commit 64c3c50

Please sign in to comment.