2019-10-16 14:39:33 -04:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-03-17 12:34:55 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test whether or not `dest` is a sub-directory of `src`
|
|
|
|
* @param src src file path
|
|
|
|
* @param dest dest file path
|
|
|
|
* @param sep path separator
|
|
|
|
*/
|
|
|
|
export function isSubdir(
|
|
|
|
src: string,
|
|
|
|
dest: string,
|
|
|
|
sep: string = path.sep
|
|
|
|
): boolean {
|
|
|
|
if (src === dest) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const srcArray = src.split(sep);
|
|
|
|
const destArray = dest.split(sep);
|
2019-05-30 08:59:30 -04:00
|
|
|
// see: https://github.com/Microsoft/TypeScript/issues/30821
|
|
|
|
return srcArray.reduce(
|
|
|
|
// @ts-ignore
|
|
|
|
(acc: true, current: string, i: number): boolean => {
|
|
|
|
return acc && destArray[i] === current;
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
2019-03-17 12:34:55 -04:00
|
|
|
}
|
2019-04-06 21:01:23 -04:00
|
|
|
|
2019-04-23 17:42:02 -04:00
|
|
|
export type PathType = "file" | "dir" | "symlink";
|
2019-04-06 21:01:23 -04:00
|
|
|
|
2019-04-23 17:42:02 -04:00
|
|
|
/**
|
|
|
|
* Get a human readable file type string.
|
|
|
|
*
|
2019-06-19 00:22:01 -04:00
|
|
|
* @param fileInfo A FileInfo describes a file and is returned by `stat`,
|
|
|
|
* `lstat`
|
2019-04-23 17:42:02 -04:00
|
|
|
*/
|
2019-05-30 08:59:30 -04:00
|
|
|
export function getFileInfoType(fileInfo: Deno.FileInfo): PathType | undefined {
|
2019-04-06 21:01:23 -04:00
|
|
|
return fileInfo.isFile()
|
2019-04-23 17:42:02 -04:00
|
|
|
? "file"
|
2019-04-06 21:01:23 -04:00
|
|
|
: fileInfo.isDirectory()
|
2019-04-23 17:42:02 -04:00
|
|
|
? "dir"
|
2019-04-06 21:01:23 -04:00
|
|
|
: fileInfo.isSymlink()
|
2019-04-23 17:42:02 -04:00
|
|
|
? "symlink"
|
|
|
|
: undefined;
|
2019-04-06 21:01:23 -04:00
|
|
|
}
|