1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 01:29:14 -05:00

fs utils getFileInfoType() return undefined when not found (#341)

This commit is contained in:
Axetroy 2019-04-24 05:42:02 +08:00 committed by Ryan Dahl
parent 45eebed3bf
commit 0a61800163
3 changed files with 20 additions and 20 deletions

View file

@ -2,7 +2,7 @@
import * as path from "./path/mod.ts";
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
import { exists, existsSync } from "./exists.ts";
import { PathType, getFileInfoType } from "./utils.ts";
import { getFileInfoType } from "./utils.ts";
const isWindows = Deno.platform.os === "win";
@ -20,7 +20,7 @@ export async function ensureSymlink(src: string, dest: string): Promise<void> {
if (await exists(dest)) {
const destStatInfo = await Deno.lstat(dest);
const destFilePathType = getFileInfoType(destStatInfo);
if (destFilePathType !== PathType.symlink) {
if (destFilePathType !== "symlink") {
throw new Error(
`Ensure path exists, expected 'symlink', got '${destFilePathType}'`
);
@ -52,7 +52,7 @@ export function ensureSymlinkSync(src: string, dest: string): void {
if (existsSync(dest)) {
const destStatInfo = Deno.lstatSync(dest);
const destFilePathType = getFileInfoType(destStatInfo);
if (destFilePathType !== PathType.symlink) {
if (destFilePathType !== "symlink") {
throw new Error(
`Ensure path exists, expected 'symlink', got '${destFilePathType}'`
);

View file

@ -17,24 +17,24 @@ export function isSubdir(
const srcArray = src.split(sep);
const destArray = dest.split(sep);
return srcArray.reduce((acc, current, i) => {
return srcArray.reduce((acc: boolean, current, i) => {
return acc && destArray[i] === current;
}, true);
}
export enum PathType {
file = "file",
dir = "dir",
symlink = "symlink"
}
export type PathType = "file" | "dir" | "symlink";
/* Get a human readable file type string */
export function getFileInfoType(fileInfo: Deno.FileInfo): PathType | null {
/**
* 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 {
return fileInfo.isFile()
? PathType.file
? "file"
: fileInfo.isDirectory()
? PathType.dir
? "dir"
: fileInfo.isSymlink()
? PathType.symlink
: null;
? "symlink"
: undefined;
}

View file

@ -36,21 +36,21 @@ test(function _isSubdir() {
test(function _getFileInfoType() {
const pairs = [
[path.join(testdataDir, "file_type_1"), PathType.file],
[path.join(testdataDir, "file_type_dir_1"), PathType.dir]
[path.join(testdataDir, "file_type_1"), "file"],
[path.join(testdataDir, "file_type_dir_1"), "dir"]
];
pairs.forEach(function(p) {
const filePath = p[0] as string;
const type = p[1] as PathType;
switch (type) {
case PathType.file:
case "file":
ensureFileSync(filePath);
break;
case PathType.dir:
case "dir":
ensureDirSync(filePath);
break;
case PathType.symlink:
case "symlink":
// TODO(axetroy): test symlink
break;
}