Skip to content

Commit

Permalink
Fixed directory only pattern when using --only flag
Browse files Browse the repository at this point in the history
This commit fixes an issue in the matchGlob to match directories when filtering through files
  • Loading branch information
EvilGenius13 committed Jan 21, 2025
1 parent da30052 commit c795653
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-kings-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli': patch
---

Fixed directory patterns in --only flag
92 changes: 92 additions & 0 deletions packages/theme/src/cli/utilities/asset-ignore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,96 @@ describe('asset-ignore', () => {
])
})
})

describe('applyIgnoreFilters with only options', () => {
test(`should return single file when only option is a single file`, () => {
const options = {
only: ['assets/basic.css'],
}

const actualChecksums = applyIgnoreFilters(checksums, options)

expect(actualChecksums).toEqual([{key: 'assets/basic.css', checksum: '00000000000000000000000000000000'}])
})

test(`should return all files in a directory matching the pattern`, () => {
const options = {
only: ['assets/*.css'],
}

const actualChecksums = applyIgnoreFilters(checksums, options)

expect(actualChecksums).toEqual([
{key: 'assets/basic.css', checksum: '00000000000000000000000000000000'},
{key: 'assets/complex.css', checksum: '11111111111111111111111111111111'},
])
})

test(`should return all files in a directory when only option is a directory pattern`, () => {
const options = {
only: ['assets/'],
}

const actualChecksums = applyIgnoreFilters(checksums, options)

expect(actualChecksums).toEqual([
{key: 'assets/basic.css', checksum: '00000000000000000000000000000000'},
{key: 'assets/complex.css', checksum: '11111111111111111111111111111111'},
{key: 'assets/image.png', checksum: '22222222222222222222222222222222'},
])
})
})

describe('applyIgnoreFilters with ignore options', () => {
test(`should ignore single file when ignore option is a single file`, () => {
const options = {
ignore: ['assets/basic.css'],
}

const actualChecksums = applyIgnoreFilters(checksums, options)

expect(actualChecksums).toEqual([
{key: 'assets/complex.css', checksum: '11111111111111111111111111111111'},
{key: 'assets/image.png', checksum: '22222222222222222222222222222222'},
{key: 'config/settings_data.json', checksum: '33333333333333333333333333333333'},
{key: 'config/settings_schema.json', checksum: '44444444444444444444444444444444'},
{key: 'sections/announcement-bar.liquid', checksum: '55555555555555555555555555555555'},
{key: 'templates/404.json', checksum: '6666666666666666666666666666666'},
{key: 'templates/customers/account.json', checksum: '7777777777777777777777777777777'},
])
})

test(`should ignore all files in a directory matching the pattern`, () => {
const options = {
ignore: ['assets/*.css'],
}

const actualChecksums = applyIgnoreFilters(checksums, options)

expect(actualChecksums).toEqual([
{key: 'assets/image.png', checksum: '22222222222222222222222222222222'},
{key: 'config/settings_data.json', checksum: '33333333333333333333333333333333'},
{key: 'config/settings_schema.json', checksum: '44444444444444444444444444444444'},
{key: 'sections/announcement-bar.liquid', checksum: '55555555555555555555555555555555'},
{key: 'templates/404.json', checksum: '6666666666666666666666666666666'},
{key: 'templates/customers/account.json', checksum: '7777777777777777777777777777777'},
])
})

test(`should ignore all files in a directory when ignore option is a directory pattern`, () => {
const options = {
ignore: ['assets/'],
}

const actualChecksums = applyIgnoreFilters(checksums, options)

expect(actualChecksums).toEqual([
{key: 'config/settings_data.json', checksum: '33333333333333333333333333333333'},
{key: 'config/settings_schema.json', checksum: '44444444444444444444444444444444'},
{key: 'sections/announcement-bar.liquid', checksum: '55555555555555555555555555555555'},
{key: 'templates/404.json', checksum: '6666666666666666666666666666666'},
{key: 'templates/customers/account.json', checksum: '7777777777777777777777777777777'},
])
})
})
})
6 changes: 6 additions & 0 deletions packages/theme/src/cli/utilities/asset-ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function filterBy(patterns: string[], type: string, invertMatch = false) {
const match = patterns.some(
(pattern) => matchGlob(key, pattern) || (isRegex(pattern) && regexMatch(key, asRegex(pattern))),
)

const shouldIgnore = invertMatch ? !match : match

if (shouldIgnore) {
Expand Down Expand Up @@ -87,6 +88,11 @@ function matchGlob(key: string, pattern: string) {

if (result) return true

if (!pattern.includes('*') && pattern.endsWith('/')) {
const directoryPattern = `${pattern}**`
return originalMatchGlob(key, directoryPattern, matchOpts)
}

// When the the standard match fails and the pattern includes '/*.', we
// replace '/*.' with '/**/*.' to emulate Shopify CLI 2.x behavior, as it was
// based on 'File.fnmatch'.
Expand Down

0 comments on commit c795653

Please sign in to comment.