From 3c18d509bf35bfc251d0ca43200dfb3b118148ba Mon Sep 17 00:00:00 2001 From: Josh Faigan Date: Thu, 23 Jan 2025 15:45:09 -0500 Subject: [PATCH] add warning for folder/ directory pattern --- .../src/cli/utilities/asset-ignore.test.ts | 90 +++++++++++++++++++ .../theme/src/cli/utilities/asset-ignore.ts | 11 +++ 2 files changed, 101 insertions(+) diff --git a/packages/theme/src/cli/utilities/asset-ignore.test.ts b/packages/theme/src/cli/utilities/asset-ignore.test.ts index c4ba7ea1eae..c3c6058cfc9 100644 --- a/packages/theme/src/cli/utilities/asset-ignore.test.ts +++ b/packages/theme/src/cli/utilities/asset-ignore.test.ts @@ -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'}, + ]) + }) + }) }) diff --git a/packages/theme/src/cli/utilities/asset-ignore.ts b/packages/theme/src/cli/utilities/asset-ignore.ts index 8bba79a5a68..8688b193e9d 100644 --- a/packages/theme/src/cli/utilities/asset-ignore.ts +++ b/packages/theme/src/cli/utilities/asset-ignore.ts @@ -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() export function applyIgnoreFilters( files: T[], @@ -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'.