Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a method to find the root for a file path #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fs-merger",
"version": "3.0.2",
"version": "3.0.3",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new features are usually minor version bumps so likely: 3.1.0

"description": "Reads files from a real location",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
22 changes: 20 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const WHITELISTEDOPERATION = new Set([
'readdir',
'readFileMeta',
'entries',
'at'
'at',
'getRoot'
]);

function getRootAndPrefix(node: any): FSMerger.FSMergerObject {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -265,6 +266,23 @@ class FSMerger {
result.sort((entryA, entryB) => (entryA.relativePath > entryB.relativePath) ? 1 : -1);
return result;
}

getRoot(filePath: string): string | undefined {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking, should this rather be merger.realpathSync(filePath); ? I believe modeling this after realpathSync would be ideal, as it is a real and valid fs operation, and also unambiguously will point to the concrete file on disk, regardless of funky links or mergers.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

realPathSync makes more sense. Yes, it will more unambiguous as well.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, I know its some extra work, but I think since this a public API, it's very well worth it.

Copy link
Collaborator Author

@SparshithNR SparshithNR Jan 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

realPathSync implementation wasn't the intention here.
The issue I am trying to solve here is, provided relative path or absolute path, find the corresponding root which is part of the dirList.
For Example:

/*
test
 | 
 -- realtest.js


/
 |
 -- user
      |
      -- unit
            |
            -- test.js
*/
let fsMerger = new FSMerger(['fixture/test', '/user/testing/unit']);
let root = fsMerger.getRoot('realtest.js'); // returns `fixture/test`
let fullPath = fsMerger.getRoot('/user/testing/unit/test.js'); // returns `/user/testing/unit`
let relativePath = fsMerger.getRoot('test.js'); // returns `/user/testing/unit`

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;
Expand Down
27 changes: 27 additions & 0 deletions tests/unit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
});
});