2019-03-17 12:34:55 -04:00
|
|
|
import * as path from "./path/mod.ts";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-04-24 07:41:23 -04:00
|
|
|
return srcArray.reduce((acc, current, i): boolean => {
|
2019-03-17 12:34:55 -04:00
|
|
|
return acc && destArray[i] === current;
|
|
|
|
}, true);
|
|
|
|
}
|
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.
|
|
|
|
*
|
|
|
|
* @param fileInfo A FileInfo describes a file and is returned by `stat`, `lstat`
|
|
|
|
*/
|
|
|
|
export function getFileInfoType(fileInfo: Deno.FileInfo): PathType {
|
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
|
|
|
}
|