From 844c5fc15294676dbcc1a3e0d08d51ebee956e9d Mon Sep 17 00:00:00 2001 From: sparshithNR Date: Thu, 9 Jan 2020 09:33:45 -0800 Subject: [PATCH] Provide a method to find the root for a file path 1. Ensures that we will have a method to find path for the file --- package.json | 2 +- src/index.ts | 22 ++++++++++++++++++++-- tests/unit-test.js | 27 +++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 2a31d11..09bf98b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fs-merger", - "version": "3.0.2", + "version": "3.0.3", "description": "Reads files from a real location", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index ac33340..1fa222b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,7 +28,8 @@ const WHITELISTEDOPERATION = new Set([ 'readdir', 'readFileMeta', 'entries', - 'at' + 'at', + 'getRoot' ]); function getRootAndPrefix(node: any): FSMerger.FSMergerObject { @@ -70,7 +71,7 @@ function handleOperation(this: FSMerger & {[key: string]: any}, { target, proper let fullPath = relativePath // at is a spcfical property exist in FSMerge which takes number as input do not perform path operation on it. - if (propertyName == 'at' || !path.isAbsolute(relativePath)) { + if (propertyName == 'at' || propertyName == 'getRoot' || !path.isAbsolute(relativePath)) { // if property is present in the FSMerge do not hijack it with fs operations if (this[propertyName]) { return this[propertyName](relativePath, ...fsArguments); @@ -265,6 +266,23 @@ class FSMerger { result.sort((entryA, entryB) => (entryA.relativePath > entryB.relativePath) ? 1 : -1); return result; } + + getRoot(filePath: string): string | undefined { + if (!this.MAP) { + this._generateMap(); + } + for (let i = this._dirList.length - 1; i >= 0; i--) { + let { root } = this.PREFIXINDEXMAP[i]; + filePath = path.normalize(filePath); + let regEx = new RegExp(`${path.normalize(root)}(\/|\s?$)`); + let startsWith = regEx.test(filePath); + if (startsWith) { + return root; + } else if(!path.isAbsolute(filePath) && fs.existsSync(path.resolve(root, filePath))){ + return root; + } + } + } } export = FSMerger; diff --git a/tests/unit-test.js b/tests/unit-test.js index 9addcc6..ddefe71 100644 --- a/tests/unit-test.js +++ b/tests/unit-test.js @@ -333,4 +333,31 @@ describe('fs-reader', function () { expect(content).to.be.equal('hello'); }); }); + describe('getRoot', function () { + let fsMerger = new FSMerge(['fixtures/test-1', 'fixtures/test-2', path.resolve('fixtures/test-3')]); + it('returns valid root', function() { + let content = fsMerger.getRoot('fixtures/test-1/tester'); + expect(content).to.be.equal('fixtures/test-1'); + }); + it('matches if relative path is not found directly', function() { + let content = fsMerger.getRoot('test-sub-1'); + expect(content).to.be.equal(path.resolve('fixtures/test-3')); + }); + it('undefined if no match', function() { + let content = fsMerger.getRoot('test-sub-10'); + expect(content).to.be.undefined; + }); + it('partial match shouldnt return root', function() { + let content = fsMerger.getRoot('fixtures/test-12/tester'); + expect(content).to.be.undefined; + content = fsMerger.getRoot('fixtures/test/12/tester'); + expect(content).to.be.undefined; + content = fsMerger.getRoot('fixtures/test-12'); + expect(content).to.be.undefined; + }); + it('matches absolute path', function() { + let content = fsMerger.getRoot(path.resolve('fixtures/test-3/test-sub-1')); + expect(content).to.be.equal(path.resolve('fixtures/test-3')); + }); + }); }); \ No newline at end of file