2020-09-21 21:26:41 +09:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-10-16 19:39:33 +01:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-03-18 00:34:55 +08: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,
|
2020-07-14 15:24:17 -04:00
|
|
|
sep: string = path.sep,
|
2019-03-18 00:34:55 +08:00
|
|
|
): boolean {
|
|
|
|
if (src === dest) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const srcArray = src.split(sep);
|
|
|
|
const destArray = dest.split(sep);
|
2020-05-15 19:53:23 +01:00
|
|
|
return srcArray.every((current, i) => destArray[i] === current);
|
2019-03-18 00:34:55 +08:00
|
|
|
}
|
2019-04-07 09:01:23 +08:00
|
|
|
|
2019-04-24 05:42:02 +08:00
|
|
|
export type PathType = "file" | "dir" | "symlink";
|
2019-04-07 09:01:23 +08:00
|
|
|
|
2019-04-24 05:42:02 +08:00
|
|
|
/**
|
|
|
|
* Get a human readable file type string.
|
|
|
|
*
|
2019-06-19 12:22:01 +08:00
|
|
|
* @param fileInfo A FileInfo describes a file and is returned by `stat`,
|
|
|
|
* `lstat`
|
2019-04-24 05:42:02 +08:00
|
|
|
*/
|
2019-05-30 14:59:30 +02:00
|
|
|
export function getFileInfoType(fileInfo: Deno.FileInfo): PathType | undefined {
|
2020-04-16 06:40:30 +01:00
|
|
|
return fileInfo.isFile
|
2019-04-24 05:42:02 +08:00
|
|
|
? "file"
|
2020-04-16 06:40:30 +01:00
|
|
|
: fileInfo.isDirectory
|
2019-04-24 05:42:02 +08:00
|
|
|
? "dir"
|
2020-04-16 06:40:30 +01:00
|
|
|
: fileInfo.isSymlink
|
2019-04-24 05:42:02 +08:00
|
|
|
? "symlink"
|
|
|
|
: undefined;
|
2019-04-07 09:01:23 +08:00
|
|
|
}
|