Skip to content

Commit

Permalink
Enable glob pattern matching when ignoring files on theme pull
Browse files Browse the repository at this point in the history
Co-authored-by: Guilherme Carreiro <karreiro@gmail.com>
  • Loading branch information
lukeh-shopify and karreiro committed Mar 13, 2024
1 parent f553169 commit ab752de
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-candles-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-kit': patch
---

Add function to perform glob pattern matching
5 changes: 5 additions & 0 deletions .changeset/orange-bears-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/theme': patch
---

Fix file pattern matching to obey glob patterns on beta version of `theme pull`
1 change: 1 addition & 0 deletions packages/cli-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"liquidjs": "10.9.2",
"lodash": "4.17.21",
"macaddress": "0.5.3",
"minimatch": "9.0.3",
"mrmime": "1.0.1",
"node-abort-controller": "3.1.1",
"node-fetch": "3.3.2",
Expand Down
11 changes: 11 additions & 0 deletions packages/cli-kit/src/public/node/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import {temporaryDirectoryTask} from 'tempy'
import {sep, join} from 'pathe'
import {findUp as internalFindUp} from 'find-up'
import {minimatch} from 'minimatch'
import {
mkdirSync as fsMkdirSync,
readFileSync as fsReadFileSync,
Expand Down Expand Up @@ -509,3 +510,13 @@ export async function findPathUp(
const got = await internalFindUp(matcher as any, options)
return got ? normalizePath(got) : undefined
}

/**
* Matches a key against a glob pattern.
* @param key - The key to match.
* @param pattern - The glob pattern to match against.
* @returns True if the key matches the pattern, false otherwise.
*/
export function matchGlob(key: string, pattern: string): boolean {
return minimatch(key, pattern)
}
21 changes: 14 additions & 7 deletions packages/theme/src/cli/utilities/asset-ignore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import {ReadOptions, fileExists, readFile} from '@shopify/cli-kit/node/fs'
import {joinPath} from '@shopify/cli-kit/node/path'
import {test, describe, beforeEach, vi, expect} from 'vitest'

vi.mock('@shopify/cli-kit/node/fs')
vi.mock('@shopify/cli-kit/node/fs', async () => {
const originalFs: any = await vi.importActual('@shopify/cli-kit/node/fs')
return {
...originalFs,
matchGlob: originalFs.matchGlob,
readFile: vi.fn(),
fileExists: vi.fn(),
}
})

vi.mock('@shopify/cli-kit/node/path')

describe('applyIgnoreFilters', () => {
Expand Down Expand Up @@ -43,19 +52,17 @@ describe('applyIgnoreFilters', () => {
vi.mocked(readFileFn).mockResolvedValue(`
# assets/basic.css
assets/complex.css
assets/*.png
sections/*
config/*_data.json
.*settings_schema.json
`)

// When
const actualChecksums = await applyIgnoreFilters(checksums, themeFileSystem)

// Then
expect(actualChecksums).toEqual([
{key: 'assets/basic.css', checksum: '00000000000000000000000000000000'},
{key: 'assets/image.png', checksum: '22222222222222222222222222222222'},
{key: 'config/settings_data.json', checksum: '33333333333333333333333333333333'},
{key: 'config/settings_schema.json', checksum: '44444444444444444444444444444444'},
])
expect(actualChecksums).toEqual([{key: 'assets/basic.css', checksum: '00000000000000000000000000000000'}])
})

test(`returns the proper checksums ignoring files specified by the 'ignore' option`, async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/theme/src/cli/utilities/asset-ignore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {fileExists, readFile} from '@shopify/cli-kit/node/fs'
import {fileExists, readFile, matchGlob} from '@shopify/cli-kit/node/fs'
import {outputDebug} from '@shopify/cli-kit/node/output'
import {joinPath} from '@shopify/cli-kit/node/path'
import {Checksum, ThemeFileSystem} from '@shopify/cli-kit/node/themes/types'
Expand All @@ -22,7 +22,7 @@ function filterBy(patterns: string[] | undefined, type: string, invertMatch = fa
return ({key}: Checksum) => {
if (!patterns) return true

const match = patterns.some((pattern) => key.match(pattern))
const match = patterns.some((pattern) => matchGlob(key, pattern) || key.match(pattern))
const shouldIgnore = invertMatch ? !match : match

if (shouldIgnore) {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ab752de

Please sign in to comment.