From f184332c09c851faac50f598d29ebe4426e05464 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Sat, 9 May 2020 13:34:47 +0100 Subject: BREAKING(std): reorganization (#5087) * Prepend underscores to private modules * Remove collectUint8Arrays() It would be a misuse of Deno.iter()'s result. * Move std/_util/async.ts to std/async * Move std/util/sha*.ts to std/hash --- std/fs/_util.ts | 45 ++++++++++++++++++++++++++++++++++ std/fs/_util_test.ts | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ std/fs/copy.ts | 2 +- std/fs/ensure_dir.ts | 2 +- std/fs/ensure_file.ts | 2 +- std/fs/ensure_link.ts | 2 +- std/fs/ensure_symlink.ts | 2 +- std/fs/expand_glob.ts | 3 ++- std/fs/move.ts | 2 +- std/fs/utils.ts | 45 ---------------------------------- std/fs/utils_test.ts | 63 ------------------------------------------------ 11 files changed, 116 insertions(+), 115 deletions(-) create mode 100644 std/fs/_util.ts create mode 100644 std/fs/_util_test.ts delete mode 100644 std/fs/utils.ts delete mode 100644 std/fs/utils_test.ts (limited to 'std/fs') diff --git a/std/fs/_util.ts b/std/fs/_util.ts new file mode 100644 index 000000000..48a98a0b1 --- /dev/null +++ b/std/fs/_util.ts @@ -0,0 +1,45 @@ +import * as path from "../path/mod.ts"; + +/** + * 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, + sep: string = path.sep +): boolean { + if (src === dest) { + return false; + } + const srcArray = src.split(sep); + const destArray = dest.split(sep); + // see: https://github.com/Microsoft/TypeScript/issues/30821 + return srcArray.reduce( + // @ts-ignore + (acc: true, current: string, i: number): boolean => { + return acc && destArray[i] === current; + }, + true + ); +} + +export type PathType = "file" | "dir" | "symlink"; + +/** + * 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 | undefined { + return fileInfo.isFile + ? "file" + : fileInfo.isDirectory + ? "dir" + : fileInfo.isSymlink + ? "symlink" + : undefined; +} diff --git a/std/fs/_util_test.ts b/std/fs/_util_test.ts new file mode 100644 index 000000000..48fc33ecd --- /dev/null +++ b/std/fs/_util_test.ts @@ -0,0 +1,63 @@ +// Copyright the Browserify authors. MIT License. + +import { assertEquals } from "../testing/asserts.ts"; +import * as path from "../path/mod.ts"; +import { isSubdir, getFileInfoType, PathType } from "./_util.ts"; +import { ensureFileSync } from "./ensure_file.ts"; +import { ensureDirSync } from "./ensure_dir.ts"; + +const testdataDir = path.resolve("fs", "testdata"); + +Deno.test("_isSubdir", function (): void { + const pairs = [ + ["", "", false, path.posix.sep], + ["/first/second", "/first", false, path.posix.sep], + ["/first", "/first", false, path.posix.sep], + ["/first", "/first/second", true, path.posix.sep], + ["first", "first/second", true, path.posix.sep], + ["../first", "../first/second", true, path.posix.sep], + ["c:\\first", "c:\\first", false, path.win32.sep], + ["c:\\first", "c:\\first\\second", true, path.win32.sep], + ]; + + pairs.forEach(function (p): void { + const src = p[0] as string; + const dest = p[1] as string; + const expected = p[2] as boolean; + const sep = p[3] as string; + assertEquals( + isSubdir(src, dest, sep), + expected, + `'${src}' should ${expected ? "" : "not"} be parent dir of '${dest}'` + ); + }); +}); + +Deno.test("_getFileInfoType", function (): void { + const pairs = [ + [path.join(testdataDir, "file_type_1"), "file"], + [path.join(testdataDir, "file_type_dir_1"), "dir"], + ]; + + pairs.forEach(function (p): void { + const filePath = p[0] as string; + const type = p[1] as PathType; + switch (type) { + case "file": + ensureFileSync(filePath); + break; + case "dir": + ensureDirSync(filePath); + break; + case "symlink": + // TODO(axetroy): test symlink + break; + } + + const stat = Deno.statSync(filePath); + + Deno.removeSync(filePath, { recursive: true }); + + assertEquals(getFileInfoType(stat), type); + }); +}); diff --git a/std/fs/copy.ts b/std/fs/copy.ts index 609a8437b..85558d905 100644 --- a/std/fs/copy.ts +++ b/std/fs/copy.ts @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as path from "../path/mod.ts"; import { ensureDir, ensureDirSync } from "./ensure_dir.ts"; -import { isSubdir, getFileInfoType } from "./utils.ts"; +import { isSubdir, getFileInfoType } from "./_util.ts"; import { assert } from "../testing/asserts.ts"; export interface CopyOptions { diff --git a/std/fs/ensure_dir.ts b/std/fs/ensure_dir.ts index ecc7356ef..34053b157 100644 --- a/std/fs/ensure_dir.ts +++ b/std/fs/ensure_dir.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { getFileInfoType } from "./utils.ts"; +import { getFileInfoType } from "./_util.ts"; const { lstat, lstatSync, mkdir, mkdirSync } = Deno; /** diff --git a/std/fs/ensure_file.ts b/std/fs/ensure_file.ts index de6cab500..2539c71ac 100644 --- a/std/fs/ensure_file.ts +++ b/std/fs/ensure_file.ts @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as path from "../path/mod.ts"; import { ensureDir, ensureDirSync } from "./ensure_dir.ts"; -import { getFileInfoType } from "./utils.ts"; +import { getFileInfoType } from "./_util.ts"; const { lstat, lstatSync, writeFile, writeFileSync } = Deno; /** diff --git a/std/fs/ensure_link.ts b/std/fs/ensure_link.ts index e43325a25..f446df781 100644 --- a/std/fs/ensure_link.ts +++ b/std/fs/ensure_link.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 { getFileInfoType } from "./utils.ts"; +import { getFileInfoType } from "./_util.ts"; /** * Ensures that the hard link exists. diff --git a/std/fs/ensure_symlink.ts b/std/fs/ensure_symlink.ts index 03c355b5d..2a184bb4f 100644 --- a/std/fs/ensure_symlink.ts +++ b/std/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 { getFileInfoType } from "./utils.ts"; +import { getFileInfoType } from "./_util.ts"; /** * Ensures that the link exists. diff --git a/std/fs/expand_glob.ts b/std/fs/expand_glob.ts index 803c67cdf..e5abdabcf 100644 --- a/std/fs/expand_glob.ts +++ b/std/fs/expand_glob.ts @@ -4,7 +4,6 @@ import { globToRegExp, isAbsolute, isGlob, - isWindows, joinGlobs, normalize, } from "../path/mod.ts"; @@ -19,6 +18,8 @@ import { assert } from "../testing/asserts.ts"; const { cwd } = Deno; type FileInfo = Deno.FileInfo; +const isWindows = Deno.build.os == "windows"; + export interface ExpandGlobOptions extends GlobOptions { root?: string; exclude?: string[]; diff --git a/std/fs/move.ts b/std/fs/move.ts index cf1e49193..4a6395365 100644 --- a/std/fs/move.ts +++ b/std/fs/move.ts @@ -1,6 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { exists, existsSync } from "./exists.ts"; -import { isSubdir } from "./utils.ts"; +import { isSubdir } from "./_util.ts"; interface MoveOptions { overwrite?: boolean; diff --git a/std/fs/utils.ts b/std/fs/utils.ts deleted file mode 100644 index 48a98a0b1..000000000 --- a/std/fs/utils.ts +++ /dev/null @@ -1,45 +0,0 @@ -import * as path from "../path/mod.ts"; - -/** - * 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, - sep: string = path.sep -): boolean { - if (src === dest) { - return false; - } - const srcArray = src.split(sep); - const destArray = dest.split(sep); - // see: https://github.com/Microsoft/TypeScript/issues/30821 - return srcArray.reduce( - // @ts-ignore - (acc: true, current: string, i: number): boolean => { - return acc && destArray[i] === current; - }, - true - ); -} - -export type PathType = "file" | "dir" | "symlink"; - -/** - * 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 | undefined { - return fileInfo.isFile - ? "file" - : fileInfo.isDirectory - ? "dir" - : fileInfo.isSymlink - ? "symlink" - : undefined; -} diff --git a/std/fs/utils_test.ts b/std/fs/utils_test.ts deleted file mode 100644 index e104e3e7d..000000000 --- a/std/fs/utils_test.ts +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright the Browserify authors. MIT License. - -import { assertEquals } from "../testing/asserts.ts"; -import * as path from "../path/mod.ts"; -import { isSubdir, getFileInfoType, PathType } from "./utils.ts"; -import { ensureFileSync } from "./ensure_file.ts"; -import { ensureDirSync } from "./ensure_dir.ts"; - -const testdataDir = path.resolve("fs", "testdata"); - -Deno.test("_isSubdir", function (): void { - const pairs = [ - ["", "", false, path.posix.sep], - ["/first/second", "/first", false, path.posix.sep], - ["/first", "/first", false, path.posix.sep], - ["/first", "/first/second", true, path.posix.sep], - ["first", "first/second", true, path.posix.sep], - ["../first", "../first/second", true, path.posix.sep], - ["c:\\first", "c:\\first", false, path.win32.sep], - ["c:\\first", "c:\\first\\second", true, path.win32.sep], - ]; - - pairs.forEach(function (p): void { - const src = p[0] as string; - const dest = p[1] as string; - const expected = p[2] as boolean; - const sep = p[3] as string; - assertEquals( - isSubdir(src, dest, sep), - expected, - `'${src}' should ${expected ? "" : "not"} be parent dir of '${dest}'` - ); - }); -}); - -Deno.test("_getFileInfoType", function (): void { - const pairs = [ - [path.join(testdataDir, "file_type_1"), "file"], - [path.join(testdataDir, "file_type_dir_1"), "dir"], - ]; - - pairs.forEach(function (p): void { - const filePath = p[0] as string; - const type = p[1] as PathType; - switch (type) { - case "file": - ensureFileSync(filePath); - break; - case "dir": - ensureDirSync(filePath); - break; - case "symlink": - // TODO(axetroy): test symlink - break; - } - - const stat = Deno.statSync(filePath); - - Deno.removeSync(filePath, { recursive: true }); - - assertEquals(getFileInfoType(stat), type); - }); -}); -- cgit v1.2.3