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);
|
2020-05-15 14:53:23 -04:00
|
|
|
return srcArray.every((current, i) => destArray[i] === current);
|
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 {
|
2020-04-16 01:40:30 -04:00
|
|
|
return fileInfo.isFile
|
2019-04-23 17:42:02 -04:00
|
|
|
? "file"
|
2020-04-16 01:40:30 -04:00
|
|
|
: fileInfo.isDirectory
|
2019-04-23 17:42:02 -04:00
|
|
|
? "dir"
|
2020-04-16 01:40:30 -04:00
|
|
|
: fileInfo.isSymlink
|
2019-04-23 17:42:02 -04:00
|
|
|
? "symlink"
|
|
|
|
: undefined;
|
2019-04-06 21:01:23 -04:00
|
|
|
}
|