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

Render warning when using folder/ directory pattern #5251

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
90 changes: 90 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,94 @@ 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 using proper glob pattern`, () => {
const options = {
only: ['templates/*'],
}

const actualChecksums = applyIgnoreFilters(checksums, options)

expect(actualChecksums).toEqual([
{key: 'templates/404.json', checksum: '6666666666666666666666666666666'},
{key: 'templates/customers/account.json', checksum: '7777777777777777777777777777777'},
])
})
})

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 using proper glob 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'},
])
})
})
})
11 changes: 11 additions & 0 deletions packages/theme/src/cli/utilities/asset-ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import {uniqBy} from '@shopify/cli-kit/common/array'
import {fileExists, readFile, matchGlob as originalMatchGlob} from '@shopify/cli-kit/node/fs'
import {outputDebug} from '@shopify/cli-kit/node/output'
import {joinPath} from '@shopify/cli-kit/node/path'
import {renderWarning} from '@shopify/cli-kit/node/ui'

const SHOPIFY_IGNORE = '.shopifyignore'
const templatesRegex = /templates\/\*(\.(json|liquid))?$/
const warnedPatterns = new Set<string>()

export function applyIgnoreFilters<T extends {key: string}>(
files: T[],
Expand Down Expand Up @@ -87,6 +89,15 @@ function matchGlob(key: string, pattern: string) {

if (result) return true

if (!pattern.includes('*') && pattern.endsWith('/') && !warnedPatterns.has(pattern)) {
warnedPatterns.add(pattern)
renderWarning({
headline: 'Directory pattern not supported.',
body: `Try using ${pattern}* or ${pattern}*.filename instead.`,
})
return false
}

// 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