From 05911c309f93edb3c0061d35fd7612e5d3b84055 Mon Sep 17 00:00:00 2001 From: "Rommel, Roman" Date: Tue, 21 May 2019 09:54:08 +0200 Subject: [PATCH] Add exclude array to appropriate constructors. Fix test for pattern excludes. --- lib/adapters/AbstractAdapter.js | 14 +++++++++++++- lib/adapters/FileSystem.js | 5 +++-- lib/adapters/Memory.js | 5 +++-- lib/resourceFactory.js | 6 +++--- test/lib/glob.js | 2 +- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/adapters/AbstractAdapter.js b/lib/adapters/AbstractAdapter.js index c6e1729f..0b64b03b 100644 --- a/lib/adapters/AbstractAdapter.js +++ b/lib/adapters/AbstractAdapter.js @@ -27,10 +27,14 @@ class AbstractAdapter extends AbstractReaderWriter { super(); this._virBasePath = virBasePath; this._virBaseDir = virBasePath.slice(0, -1); - this._excludes = excludes; + this._excludes = Array.isArray(excludes) ? excludes.map(this._negateGlob) : []; this._project = project; } + _negateGlob(glob) { + return /^!/.test(glob) ? glob : "!" + glob; + } + /** * Locates resources by glob. * @@ -44,14 +48,22 @@ class AbstractAdapter extends AbstractReaderWriter { * @returns {Promise} Promise resolving to list of resources */ _byGlob(virPattern, options = {nodir: true}, trace) { + // TODO: Should be passed as parameter + let excludes = this._excludes || []; + if (!(virPattern instanceof Array)) { virPattern = [virPattern]; } + + virPattern = Array.prototype.concat.apply(virPattern, excludes); + return Promise.all(virPattern.map(this._normalizePattern, this)).then((patterns) => { + if (patterns.length === 0) { return []; } patterns = Array.prototype.concat.apply([], patterns); + if (!options.nodir) { for (let i = patterns.length - 1; i >= 0; i--) { const idx = this._virBaseDir.indexOf(patterns[i]); diff --git a/lib/adapters/FileSystem.js b/lib/adapters/FileSystem.js index 60e311cf..8a10537a 100644 --- a/lib/adapters/FileSystem.js +++ b/lib/adapters/FileSystem.js @@ -21,9 +21,10 @@ class FileSystem extends AbstractAdapter { * @param {Object} parameters Parameters * @param {string} parameters.virBasePath Virtual base path * @param {string} parameters.fsBasePath (Physical) File system path + * @param {string[]} [parameters.excludes] List of GLOB patterns to exclude */ - constructor({virBasePath, project, fsBasePath}) { - super({virBasePath, project}); + constructor({virBasePath, project, fsBasePath, excludes}) { + super({virBasePath, project, excludes}); this._fsBasePath = fsBasePath; } diff --git a/lib/adapters/Memory.js b/lib/adapters/Memory.js index a2fe36b9..edd709d7 100644 --- a/lib/adapters/Memory.js +++ b/lib/adapters/Memory.js @@ -17,9 +17,10 @@ class Memory extends AbstractAdapter { * @public * @param {Object} parameters Parameters * @param {string} parameters.virBasePath Virtual base path + * @param {string[]} [parameters.excludes] List of GLOB patterns to exclude */ - constructor({virBasePath, project}) { - super({virBasePath, project}); + constructor({virBasePath, project, excludes}) { + super({virBasePath, project, excludes}); this._virFiles = {}; // map full of files this._virDirs = {}; // map full of directories } diff --git a/lib/resourceFactory.js b/lib/resourceFactory.js index 14c3611a..b535dfc9 100644 --- a/lib/resourceFactory.js +++ b/lib/resourceFactory.js @@ -128,11 +128,11 @@ const resourceFactory = { * @param {string} [parameters.fsBasePath] File system base path * @returns {module:@ui5/fs.adapters.FileSystem|module:@ui5/fs.adapters.Memory} File System- or Virtual Adapter */ - createAdapter({fsBasePath, virBasePath, project}) { + createAdapter({fsBasePath, virBasePath, project, excludes}) { if (fsBasePath) { - return new FsAdapter({fsBasePath, virBasePath, project}); + return new FsAdapter({fsBasePath, virBasePath, project, excludes}); } else { - return new MemAdapter({virBasePath, project}); + return new MemAdapter({virBasePath, project, excludes}); } }, diff --git a/test/lib/glob.js b/test/lib/glob.js index 06b1b344..b2f646fc 100644 --- a/test/lib/glob.js +++ b/test/lib/glob.js @@ -114,7 +114,7 @@ test("glob with multiple patterns", (t) => { test("glob with multiple patterns with exclude", (t) => { t.plan(2); return t.context.readerWriter.filesystem.byGlob([ - "/**/*.yaml", "/test-resources/**/i18n_de.properties", "!/resources/application.b/**"]) + "/**/*.yaml", "/test-resources/**/i18n_de.properties", "!/test-resources/application.b/**"]) .then((resources) => { const expectedResources = [ "/test-resources/application.a/ui5.yaml"