Improve nested path detection across app

This commit is contained in:
Evan Hahn 2020-08-27 13:08:37 -05:00 committed by Josh Perez
parent 57d206a344
commit f8fc23a7a3
5 changed files with 169 additions and 12 deletions

16
ts/util/isPathInside.ts Normal file
View file

@ -0,0 +1,16 @@
// This is inspired by the `is-path-inside` module on npm.
import * as path from 'path';
export function isPathInside(childPath: string, parentPath: string): boolean {
const childPathResolved = path.resolve(childPath);
let parentPathResolved = path.resolve(parentPath);
if (!parentPathResolved.endsWith(path.sep)) {
parentPathResolved += path.sep;
}
return (
childPathResolved !== parentPathResolved &&
childPathResolved.startsWith(parentPathResolved)
);
}