From 0a6180016383b9d527bb96556ae0bcdabe5161eb Mon Sep 17 00:00:00 2001 From: Axetroy Date: Wed, 24 Apr 2019 05:42:02 +0800 Subject: [PATCH] fs utils getFileInfoType() return undefined when not found (#341) --- fs/ensure_symlink.ts | 6 +++--- fs/utils.ts | 24 ++++++++++++------------ fs/utils_test.ts | 10 +++++----- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/fs/ensure_symlink.ts b/fs/ensure_symlink.ts index fbb89948e4..09452a8cea 100644 --- a/fs/ensure_symlink.ts +++ b/fs/ensure_symlink.ts @@ -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 { 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}'` ); diff --git a/fs/utils.ts b/fs/utils.ts index 410e459094..06b4c295c2 100644 --- a/fs/utils.ts +++ b/fs/utils.ts @@ -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; } diff --git a/fs/utils_test.ts b/fs/utils_test.ts index 9f8d10cb01..d833aa559d 100644 --- a/fs/utils_test.ts +++ b/fs/utils_test.ts @@ -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; }