Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"nx": "22.7.0",
"oclif": "4.23.0",
"octokit-plugin-create-pull-request": "^3.12.2",
"pathe": "1.1.1",
"pathe": "2.0.3",
"pin-github-action": "^3.3.1",
"rimraf": "^6.1.3",
"ts-node": "^10.9.1",
Expand Down
3 changes: 1 addition & 2 deletions packages/cli-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,12 @@
"liquidjs": "10.26.0",
"lodash": "4.17.23",
"macaddress": "0.5.3",
"minimatch": "9.0.8",
"mrmime": "1.0.1",
"network-interfaces": "1.1.0",
"node-abort-controller": "3.1.1",
"node-fetch": "3.3.2",
"open": "8.4.2",
"pathe": "1.1.2",
"pathe": "2.0.3",
"react": "19.2.4",
"semver": "7.6.3",
"stacktracey": "2.1.8",
Expand Down
36 changes: 36 additions & 0 deletions packages/cli-kit/src/public/node/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
copyDirectoryContents,
symlink,
fileRealPath,
matchGlob,
} from './fs.js'
import {joinPath, normalizePath} from './path.js'
import * as array from '../common/array.js'
Expand Down Expand Up @@ -319,6 +320,41 @@ describe('glob', () => {
})
})

describe('matchGlob', () => {
test('matches liquid file patterns with native Node glob semantics', () => {
expect(matchGlob('theme.liquid', '*.liquid')).toBe(true)
expect(matchGlob('layout/theme.liquid', '*.liquid')).toBe(false)
expect(matchGlob('layout/theme.liquid', '**/*.liquid')).toBe(true)
expect(matchGlob('assets/theme.css', '*.liquid')).toBe(false)
})

test('matches watch-style globs used by extension file watching', () => {
expect(matchGlob('src/main.rs', 'src/**/*.rs')).toBe(true)
expect(matchGlob('src/lib/main.rs', 'src/**/*.rs')).toBe(true)
expect(matchGlob('src/main.ts', 'src/**/*.rs')).toBe(false)
expect(matchGlob('/tmp/my-function/src/main.rs', '/tmp/my-function/src/**/*.rs')).toBe(true)
expect(matchGlob('/tmp/my-function/src/lib/main.rs', '/tmp/my-function/src/**/*.rs')).toBe(true)
})

test('matches project config selection globs', () => {
expect(matchGlob('extensions/my-ext/shopify.extension.toml', 'extensions/*/*.extension.toml')).toBe(true)
expect(matchGlob('extensions/nested/my-ext/shopify.extension.toml', 'extensions/*/*.extension.toml')).toBe(false)
expect(matchGlob('web/shopify.web.toml', '**/shopify.web.toml')).toBe(true)
})

test('supports matchBase for theme ignore patterns', () => {
expect(matchGlob('assets/theme.css', '*.css', {matchBase: true, noglobstar: true})).toBe(true)
expect(matchGlob('assets/theme.css', '*.liquid', {matchBase: true, noglobstar: true})).toBe(false)
})

test('supports noglobstar for theme ignore patterns', () => {
expect(
matchGlob('templates/customers/account.json', 'templates/**/*.json', {matchBase: true, noglobstar: true}),
).toBe(true)
expect(matchGlob('templates/404.json', 'templates/**/*.json', {matchBase: true, noglobstar: true})).toBe(false)
})
})

describe('detectEOL', () => {
test('detects the EOL of a file', async () => {
// Given
Expand Down
37 changes: 27 additions & 10 deletions packages/cli-kit/src/public/node/fs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {outputContent, outputToken, outputDebug} from './output.js'
import {joinPath, normalizePath, resolvePath} from './path.js'
import {basename, joinPath, matchesGlob, normalizePath, resolvePath, sep} from './path.js'
import {OverloadParameters} from '../../private/common/ts/overloaded-parameters.js'
import {getRandomName, RandomNameFamily} from '../common/string.js'
import {systemTempDir} from '../../private/node/temp-dir.js'
Expand All @@ -14,9 +14,7 @@ import {
// @ts-ignore
} from 'fs-extra/esm'

import {sep, join} from 'pathe'
import {findUp as internalFindUp, findUpSync as internalFindUpSync} from 'find-up'
import {minimatch} from 'minimatch'
import fastGlobLib from 'fast-glob'
import {
mkdirSync as fsMkdirSync,
Expand Down Expand Up @@ -67,7 +65,7 @@ import type {Pattern, Options as GlobOptions} from 'fast-glob'
*/
export function stripUpPath(path: string, strip: number): string {
const parts = path.split(sep)
return join(...parts.slice(strip))
return joinPath(...parts.slice(strip))
}

/**
Expand All @@ -76,7 +74,7 @@ export function stripUpPath(path: string, strip: number): string {
* @param callback - The callback that receives the temporary directory.
*/
export async function inTemporaryDirectory<T>(callback: (tmpDir: string) => T | Promise<T>): Promise<T> {
const tmpDir = await fsMkdtemp(join(systemTempDir, 'tmp-'))
const tmpDir = await fsMkdtemp(joinPath(systemTempDir, 'tmp-'))
try {
return await callback(tmpDir)
} finally {
Expand All @@ -89,7 +87,7 @@ export async function inTemporaryDirectory<T>(callback: (tmpDir: string) => T |
* @returns - The path to the temporary directory.
*/
export function tempDirectory(): string {
return fsMkdtempSync(join(systemTempDir, 'tmp-'))
return fsMkdtempSync(joinPath(systemTempDir, 'tmp-'))
}

/**
Expand Down Expand Up @@ -688,8 +686,8 @@ export function findPathUpSync(
}

export interface MatchGlobOptions {
matchBase: boolean
noglobstar: boolean
matchBase?: boolean
noglobstar?: boolean
}

/**
Expand All @@ -699,8 +697,27 @@ export interface MatchGlobOptions {
* @param options - The options to refine the matching approach.
* @returns true if the key matches the pattern, false otherwise.
*/
export function matchGlob(key: string, pattern: string, options?: MatchGlobOptions): boolean {
return minimatch(key, pattern, options)
export function matchGlob(key: string, pattern: string, options: MatchGlobOptions = {}): boolean {
const effectivePattern = options.noglobstar ? patternWithoutGlobstars(pattern) : pattern

if (matchesGlob(key, effectivePattern)) return true

if (options.matchBase && !hasPathSeparator(effectivePattern)) {
return matchesGlob(basename(key), effectivePattern)
}

return false
}

function patternWithoutGlobstars(pattern: string): string {
return pattern
.split(/([/\\])/)
.map((part) => (part === '**' ? '*' : part))
.join('')
}

function hasPathSeparator(path: string): boolean {
return path.includes('/') || path.includes('\\')
}

/**
Expand Down
18 changes: 18 additions & 0 deletions packages/cli-kit/src/public/node/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
extname as extnamePathe,
parse,
isAbsolute,
matchesGlob as matchesGlobPathe,
sep as patheSep,
} from 'pathe'
import {fileURLToPath} from 'url'
// eslint-disable-next-line n/prefer-global/url
Expand Down Expand Up @@ -95,6 +97,22 @@ export function extname(path: string): string {
return extnamePathe(path)
}

/**
* The platform-agnostic path segment separator (always `/`, via pathe).
*/
export const sep = patheSep

/**
* Determines whether a path matches a glob pattern.
*
* @param path - The path to match.
* @param pattern - The glob pattern (or patterns) to match against.
* @returns Whether the path matches the pattern.
*/
export function matchesGlob(path: string, pattern: string | string[]): boolean {
return matchesGlobPathe(path, pattern)
}

/**
* Parses a path into its components (root, dir, base, ext, name).
*
Expand Down
23 changes: 5 additions & 18 deletions pnpm-lock.yaml

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

Loading