-
Notifications
You must be signed in to change notification settings - Fork 6
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking, should this rather be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
/*
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; | ||
|
There was a problem hiding this comment.
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