Skip to content

Commit cee1bfc

Browse files
committed
feat(entry): support multiple patterns with same base
closes #675
1 parent 90cd66b commit cee1bfc

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

src/features/entry.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,10 @@ describe('toObjectEntry', () => {
134134
expect(Object.keys(result)).not.toContain('hooks/index')
135135
})
136136

137-
test('object entry with multiple negation patterns', async (context) => {
137+
test('object entry with multiple patterns', async (context) => {
138138
const { testDir } = await writeFixtures(context, {
139139
'src/utils/index.ts': '',
140+
'src/utils/tsx.tsx': '',
140141
'src/utils/internal.ts': '',
141142
'src/utils/helper.ts': '',
142143
'src/utils/format.ts': '',
@@ -145,19 +146,21 @@ describe('toObjectEntry', () => {
145146
{
146147
'utils/*': [
147148
'src/utils/*.ts',
149+
'src/utils/*.tsx',
148150
'!src/utils/index.ts',
149151
'!src/utils/internal.ts',
150152
],
151153
},
152154
testDir,
153155
)
154156
expect(result).toEqual({
157+
'utils/tsx': path.join(testDir, 'src/utils/tsx.tsx'),
155158
'utils/helper': path.join(testDir, 'src/utils/helper.ts'),
156159
'utils/format': path.join(testDir, 'src/utils/format.ts'),
157160
})
158161
})
159162

160-
test('object entry with multiple positive patterns should throw', async (context) => {
163+
test('object entry with different patterns base should throw', async (context) => {
161164
const { testDir } = await writeFixtures(context, {
162165
'src/hooks/useAuth.ts': '',
163166
'src/utils/helper.ts': '',
@@ -169,7 +172,9 @@ describe('toObjectEntry', () => {
169172
},
170173
testDir,
171174
),
172-
).rejects.toThrow(/multiple positive patterns/)
175+
).rejects.toThrow(
176+
'all value glob patterns must have the same base directory',
177+
)
173178
})
174179

175180
test('object entry with no positive pattern should throw', async (context) => {
@@ -183,6 +188,6 @@ describe('toObjectEntry', () => {
183188
},
184189
testDir,
185190
),
186-
).rejects.toThrow(/no positive pattern/)
191+
).rejects.toThrow('Cannot find files')
187192
})
188193
})

src/features/entry.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,41 @@ export async function toObjectEntry(
6262
}
6363

6464
const patterns = toArray(value)
65-
66-
const positivePatterns = patterns.filter((p) => !p.startsWith('!'))
67-
if (positivePatterns.length === 0) {
68-
throw new TypeError(
69-
`Object entry "${key}" has no positive pattern. At least one positive pattern is required.`,
65+
const files = await glob(patterns, {
66+
cwd,
67+
expandDirectories: false,
68+
})
69+
if (!files.length) {
70+
throw new Error(
71+
`Cannot find files for entry key "${key}" with patterns: ${JSON.stringify(
72+
patterns,
73+
)}`,
7074
)
7175
}
7276

73-
if (positivePatterns.length > 1) {
74-
throw new TypeError(
75-
`Object entry "${key}" has multiple positive patterns: ${positivePatterns.join(', ')}. ` +
76-
`Only one positive pattern is allowed. Use negation patterns (prefixed with "!") to exclude files.`,
77+
let valueGlobBase: string | undefined
78+
for (const pattern of patterns) {
79+
if (pattern.startsWith('!')) continue
80+
const base = picomatch.scan(pattern).base
81+
if (valueGlobBase === undefined) {
82+
valueGlobBase = base
83+
} else if (valueGlobBase !== base) {
84+
throw new Error(
85+
`When using object entry with glob pattern key "${key}", all value glob patterns must have the same base directory.`,
86+
)
87+
}
88+
}
89+
if (valueGlobBase === undefined) {
90+
throw new Error(
91+
`Cannot determine base directory for value glob patterns of key "${key}".`,
7792
)
7893
}
7994

80-
const valueGlob = picomatch.scan(positivePatterns[0])
81-
const files = await glob(patterns, {
82-
cwd,
83-
expandDirectories: false,
84-
})
85-
8695
return files.map((file) => [
8796
slash(
8897
key.replaceAll(
8998
'*',
90-
stripExtname(path.relative(valueGlob.base, file)),
99+
stripExtname(path.relative(valueGlobBase, file)),
91100
),
92101
),
93102
path.resolve(cwd, file),

0 commit comments

Comments
 (0)