From 58917fece99321883b6fccdac9520d867a95e617 Mon Sep 17 00:00:00 2001 From: isaacs Date: Fri, 24 Feb 2023 14:55:40 -0800 Subject: [PATCH 1/2] allow platform to be set with an option --- README.md | 8 ++++++++ src/index.ts | 26 +++++++++++++++++--------- test/unc.ts | 6 +++--- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 7572ca46..1a8173cb 100644 --- a/README.md +++ b/README.md @@ -320,6 +320,14 @@ is equivalent in all cases). string is first processed with `minimatch.levelTwoFileOptimize()` or similar. +### platform + +When set to `win32`, this will trigger all windows-specific +behaviors (special handling for UNC paths, and treating `\` as +separators in file paths for comparison.) + +Defaults to the value of `process.platform`. + ## Comparisons to other fnmatch/glob implementations While strict compliance with the existing standards is a diff --git a/src/index.ts b/src/index.ts index 03bc74cf..391769b0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,7 @@ export interface MinimatchOptions { flipNegate?: boolean preserveMultipleSlashes?: boolean optimizationLevel?: number + platform?: typeof process.platform } export const minimatch = ( @@ -87,19 +88,21 @@ const qmarksTestNoExtDot = ([$0]: RegExpMatchArray) => { return (f: string) => f.length === len && f !== '.' && f !== '..' } +type Platform = typeof process.platform /* c8 ignore start */ -const platform = +const defaultPlatform: Platform = ( typeof process === 'object' && process ? (typeof process.env === 'object' && process.env && process.env.__MINIMATCH_TESTING_PLATFORM__) || process.platform : 'posix' -const isWindows = platform === 'win32' -const path = isWindows ? { sep: '\\' } : { sep: '/' } +) as Platform +type Sep = '\\' | '/' +const path:{[k:string]:{sep:Sep}} = { win32: { sep: '\\' }, posix: { sep: '/' } } /* c8 ignore stop */ -export const sep = path.sep +export const sep = defaultPlatform === 'win32' ? path.win32.sep : path.posix.sep minimatch.sep = sep export const GLOBSTAR = Symbol('globstar **') @@ -306,6 +309,9 @@ export class Minimatch { globSet: string[] globParts: string[][] + isWindows: boolean + platform: typeof process.platform + regexp: false | null | MMRegExp constructor(pattern: string, options: MinimatchOptions = {}) { assertValidPattern(pattern) @@ -313,6 +319,8 @@ export class Minimatch { options = options || {} this.options = options this.pattern = pattern + this.platform = options.platform || defaultPlatform + this.isWindows = this.platform === 'win32' this.windowsPathsNoEscape = !!options.windowsPathsNoEscape || options.allowWindowsEscape === false if (this.windowsPathsNoEscape) { @@ -387,7 +395,7 @@ export class Minimatch { ) as ParseReturnFiltered[][] // do not treat the ? in UNC paths as magic - if (isWindows) { + if (this.isWindows) { for (let i = 0; i < this.set.length; i++) { const p = this.set[i] if ( @@ -710,7 +718,7 @@ export class Minimatch { // a UNC pattern like //?/c:/* can match a path like c:/x // and vice versa - if (isWindows) { + if (this.isWindows) { const fileUNC = file[0] === '' && file[1] === '' && @@ -1452,7 +1460,7 @@ export class Minimatch { // preserveMultipleSlashes is set to true. if (this.preserveMultipleSlashes) { return p.split('/') - } else if (isWindows && /^\/\/[^\/]+/.test(p)) { + } else if (this.isWindows && /^\/\/[^\/]+/.test(p)) { // add an extra '' for the one we lose return ['', ...p.split(/\/+/)] } else { @@ -1478,8 +1486,8 @@ export class Minimatch { const options = this.options // windows: need to use /, not \ - if (path.sep !== '/') { - f = f.split(path.sep).join('/') + if (this.isWindows) { + f = f.split('\\').join('/') } // treat the test path as a set of pathparts. diff --git a/test/unc.ts b/test/unc.ts index bcd98661..912a5abd 100644 --- a/test/unc.ts +++ b/test/unc.ts @@ -1,11 +1,10 @@ -process.env.__MINIMATCH_TESTING_PLATFORM__ = 'win32' import t from 'tap' import { minimatch, Minimatch, MinimatchOptions } from '../' t.test('UNC patterns do not lose their //', async t => { - const share = new Minimatch('//host/share/*') + const share = new Minimatch('//host/share/*', { platform: 'win32' }) t.match(share.set, [['', '', 'host', 'share', RegExp]]) - const uncPath = new Minimatch('//?/d:/*') + const uncPath = new Minimatch('//?/d:/*', { platform: 'win32' }) t.match(uncPath.set, [['', '', '?', 'd:', RegExp]]) }) @@ -34,6 +33,7 @@ const cases: Case[] = [ t.test('UNC drive letter paths match normal paths', async t => { for (const [file, pattern, expect, opt = {}] of cases) { + opt.platform = 'win32' t.test(`f=${file} p=${pattern}`, t => { t.test('/ only', t => { t.equal(minimatch(file, pattern, opt), expect) From 36c50b76a16f79b6862ecaf2e73b0eaa0ed30701 Mon Sep 17 00:00:00 2001 From: isaacs Date: Fri, 24 Feb 2023 14:56:40 -0800 Subject: [PATCH 2/2] 7.1.2 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index ef0254c6..d6cba7cd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "minimatch", - "version": "7.1.1", + "version": "7.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "minimatch", - "version": "7.1.1", + "version": "7.1.2", "license": "ISC", "dependencies": { "brace-expansion": "^2.0.1" diff --git a/package.json b/package.json index cff3ad30..7af30ef3 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Isaac Z. Schlueter (http://blog.izs.me)", "name": "minimatch", "description": "a glob matcher in javascript", - "version": "7.1.1", + "version": "7.1.2", "repository": { "type": "git", "url": "git://github.com/isaacs/minimatch.git"