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:
parent
45eebed3bf
commit
0a61800163
3 changed files with 20 additions and 20 deletions
|
@ -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}'`
|
||||
);
|
||||
|
|
24
fs/utils.ts
24
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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue