Skip to content

Commit

Permalink
fix(pluginutils): createFilter Windows regression (#141)
Browse files Browse the repository at this point in the history
* fix(pluginutils): createFilter windows regression

* chore: diagnose windows

* chore: remove comments
  • Loading branch information
shellscape authored Jan 5, 2020
1 parent 10a18cf commit d2daede
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/auto-install/test/fixtures/npm-bare/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
35 changes: 21 additions & 14 deletions packages/pluginutils/src/createFilter.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import { resolve, join, sep } from 'path';
import { resolve, sep } from 'path';

import mm from 'micromatch';

import { CreateFilter } from '../types';

import ensureArray from './utils/ensureArray';

const ESCAPE_IN_PATH = '()+@!';

function getMatcherString(id: string, resolutionBase: string | false | null | undefined) {
if (resolutionBase === false) {
return id;
}
let basePath = typeof resolutionBase === 'string' ? resolve(resolutionBase) : process.cwd();
for (const char of ESCAPE_IN_PATH) {
basePath = basePath.replace(new RegExp(`\\${char}`, 'g'), `\\${char}`);
}
return join(basePath, id);

// resolve('') is valid and will default to process.cwd()
const basePath = resolve(resolutionBase || '')
.split(sep)
.join('/')
// escape all possible (posix + win) path characters that might interfere with regex
.replace(/[-^$*+?.()|[\]{}]/g, '\\$&');
// this juggling is to join two paths:
// 1. the basePath which has been normalized to use /
// 2. the incoming glob (id) matcher, which uses /
// we can't use join or resolve here because Node will force backslash (\) on windows
const result = [...basePath.split('/'), ...id.split('/')].join('/');
return result;
}

const createFilter: CreateFilter = function createFilter(include?, exclude?, options?) {
Expand All @@ -26,12 +32,13 @@ const createFilter: CreateFilter = function createFilter(include?, exclude?, opt
id instanceof RegExp
? id
: {
test: mm.matcher(
getMatcherString(id, resolutionBase)
.split(sep)
.join('/'),
{ dot: true }
)
test: (what: string) => {
// this refactor is a tad overly verbose but makes for easy debugging
const pattern = getMatcherString(id, resolutionBase);
const fn = mm.matcher(pattern, { dot: true });
const result = fn(what);
return result;
}
};

const includeMatchers = ensureArray(include).map(getMatcher);
Expand Down

0 comments on commit d2daede

Please sign in to comment.