From ca33df7e1eff3c8d33b80e9b4c22261c042c717a Mon Sep 17 00:00:00 2001 From: cenfun Date: Thu, 9 May 2024 10:20:01 +0800 Subject: [PATCH] fixed string to multiple patterns "{...}" for minimatch filter --- CHANGELOG.md | 1 + lib/converter/converter.js | 16 ++++++++++++---- lib/generate.js | 12 +++++++++--- lib/utils/util.js | 21 +++++++++++++++++++++ lib/v8/v8.js | 16 ++++++++++++---- 5 files changed, 55 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef051c5d..1299f731 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - 2.7.11 - fixed type style for UI + - fixed string to multiple patterns "{...}" for minimatch filter - 2.7.10 - fixed undefined error when reading mappings diff --git a/lib/converter/converter.js b/lib/converter/converter.js index 6e5331b1..a1b37d8b 100644 --- a/lib/converter/converter.js +++ b/lib/converter/converter.js @@ -943,7 +943,7 @@ const getOriginalDecodedMappings = (originalDecodedMap, sourceIndex, locator) => // ======================================================================================================== const getSourceFilter = (options) => { - const input = options.sourceFilter; + let input = options.sourceFilter; // for function handler if (typeof input === 'function') { @@ -952,9 +952,17 @@ const getSourceFilter = (options) => { // for single minimatch pattern if (input && typeof input === 'string') { - return (sourcePath) => { - return minimatch(sourcePath, input); - }; + // string to multiple patterns "{...}" + // mcr npx mocha --sourceFilter {'**/node_modules/**':false,'**/src/*.js':true} + // mcr npx mocha --sourceFilter "{'**/node_modules/**': false, '**/src/*.js': true}" + const obj = Util.strToObjPatterns(input); + if (obj) { + input = obj; + } else { + return (sourcePath) => { + return minimatch(sourcePath, input); + }; + } } // for patterns diff --git a/lib/generate.js b/lib/generate.js index de55f95b..ee9ce06e 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -501,9 +501,15 @@ const resolveAllFilter = (input) => { // for single minimatch pattern if (input && typeof input === 'string') { - return (filePath) => { - return minimatch(filePath, input); - }; + // string to multiple patterns "{...}" + const obj = Util.strToObjPatterns(input); + if (obj) { + input = obj; + } else { + return (filePath) => { + return minimatch(filePath, input); + }; + } } // for patterns diff --git a/lib/utils/util.js b/lib/utils/util.js index ec62117b..8aa4679a 100644 --- a/lib/utils/util.js +++ b/lib/utils/util.js @@ -60,6 +60,27 @@ const Util = { return str; }, + strToObjPatterns: (str) => { + if (typeof str === 'string') { + str = str.trim(); + if (str.startsWith('{') && str.endsWith('}')) { + let fun; + let err; + try { + fun = new Function(`return ${str}`); + } catch (e) { + err = e; + } + if (!err) { + const obj = fun(); + if (obj && typeof obj === 'object') { + return obj; + } + } + } + } + }, + calculateSha1: (buffer) => { const hash = crypto.createHash('sha1'); hash.update(buffer); diff --git a/lib/v8/v8.js b/lib/v8/v8.js index 5d1eaa20..b085b1a6 100644 --- a/lib/v8/v8.js +++ b/lib/v8/v8.js @@ -23,7 +23,7 @@ const getWrapperSource = (offset, source) => { }; const getEntryFilter = (options) => { - const input = options.entryFilter; + let input = options.entryFilter; // for function handler if (typeof input === 'function') { @@ -32,9 +32,17 @@ const getEntryFilter = (options) => { // for single minimatch pattern if (input && typeof input === 'string') { - return (entry) => { - return minimatch(entry.url, input); - }; + // string to multiple patterns "{...}" + // mcr npx mocha --entryFilter {'**/node_modules/**':false,'**/src/*.js':true} + // mcr npx mocha --entryFilter "{'**/node_modules/**': false, '**/src/*.js': true}" + const obj = Util.strToObjPatterns(input); + if (obj) { + input = obj; + } else { + return (entry) => { + return minimatch(entry.url, input); + }; + } } // for patterns