Skip to content

Commit c276b1f

Browse files
committed
warn instead of rewrite
1 parent cea6bd3 commit c276b1f

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

src/util/log.js

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ function log(chalk, messages, key) {
1212
messages.forEach((message) => console.warn(chalk, '-', message))
1313
}
1414

15+
export function dim(input) {
16+
return chalk.dim(input)
17+
}
18+
1519
export default {
1620
info(key, messages) {
1721
log(chalk.bold.cyan('info'), ...(Array.isArray(key) ? [key] : [messages, key]))

src/util/normalizeConfig.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import log from './log'
1+
import log, { dim } from './log'
22

33
export function normalizeConfig(config) {
44
// Quick structure validation
@@ -245,12 +245,18 @@ export function normalizeConfig(config) {
245245
})(),
246246
}
247247

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

255261
return config
256262
}

tests/normalize-config.test.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ it('should keep content files with globs', () => {
110110
})
111111
})
112112

113-
it('should rewrite globs with incorrect bracket expansion', () => {
113+
it('should warn when we detect invalid globs with incorrect brace expansion', () => {
114+
let log = require('../src/util/log')
115+
let spy = jest.spyOn(log.default, 'warn')
116+
114117
let config = {
115118
content: [
116119
'./{example-folder}/**/*.{html,js}',
@@ -119,13 +122,20 @@ it('should rewrite globs with incorrect bracket expansion', () => {
119122
],
120123
}
121124

125+
// No rewrite happens
122126
expect(normalizeConfig(resolveConfig(config)).content).toEqual({
123127
files: [
124-
'./example-folder/**/*.{html,js}',
125-
'./example-folder/**/*.html',
126-
'./example-folder/**/*.html',
128+
'./{example-folder}/**/*.{html,js}',
129+
'./{example-folder}/**/*.{html}',
130+
'./example-folder/**/*.{html}',
127131
],
128132
extract: {},
129133
transform: {},
130134
})
135+
136+
// But a warning should happen
137+
expect(spy).toHaveBeenCalledTimes(2)
138+
expect(spy.mock.calls.map((x) => x[0])).toEqual(['invalid-glob-braces', 'invalid-glob-braces'])
139+
140+
spy.mockClear()
131141
})

0 commit comments

Comments
 (0)