1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 10:09:20 -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 * as path from "./path/mod.ts";
import { ensureDir, ensureDirSync } from "./ensure_dir.ts"; import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
import { exists, existsSync } from "./exists.ts"; import { exists, existsSync } from "./exists.ts";
import { PathType, getFileInfoType } from "./utils.ts"; import { getFileInfoType } from "./utils.ts";
const isWindows = Deno.platform.os === "win"; const isWindows = Deno.platform.os === "win";
@ -20,7 +20,7 @@ export async function ensureSymlink(src: string, dest: string): Promise<void> {
if (await exists(dest)) { if (await exists(dest)) {
const destStatInfo = await Deno.lstat(dest); const destStatInfo = await Deno.lstat(dest);
const destFilePathType = getFileInfoType(destStatInfo); const destFilePathType = getFileInfoType(destStatInfo);
if (destFilePathType !== PathType.symlink) { if (destFilePathType !== "symlink") {
throw new Error( throw new Error(
`Ensure path exists, expected 'symlink', got '${destFilePathType}'` `Ensure path exists, expected 'symlink', got '${destFilePathType}'`
); );
@ -52,7 +52,7 @@ export function ensureSymlinkSync(src: string, dest: string): void {
if (existsSync(dest)) { if (existsSync(dest)) {
const destStatInfo = Deno.lstatSync(dest); const destStatInfo = Deno.lstatSync(dest);
const destFilePathType = getFileInfoType(destStatInfo); const destFilePathType = getFileInfoType(destStatInfo);
if (destFilePathType !== PathType.symlink) { if (destFilePathType !== "symlink") {
throw new Error( throw new Error(
`Ensure path exists, expected 'symlink', got '${destFilePathType}'` `Ensure path exists, expected 'symlink', got '${destFilePathType}'`
); );

View file

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

View file

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