Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed directory only pattern when using --only flag #5251

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you test this with templates/ to test a nested folder structure? I think it'll break

}

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
Loading