summaryrefslogtreecommitdiff
path: root/fs
diff options
context:
space:
mode:
Diffstat (limited to 'fs')
-rw-r--r--fs/copy.ts28
-rw-r--r--fs/globrex.ts6
-rw-r--r--fs/globrex_test.ts76
-rw-r--r--fs/path/join_test.ts9
-rw-r--r--fs/path/parse_format_test.ts10
-rw-r--r--fs/path/resolve_test.ts6
-rw-r--r--fs/path/utils.ts4
-rw-r--r--fs/path/win32.ts5
-rw-r--r--fs/utils.ts14
-rw-r--r--fs/walk.ts12
-rw-r--r--fs/walk_test.ts2
11 files changed, 97 insertions, 75 deletions
diff --git a/fs/copy.ts b/fs/copy.ts
index d02ca290f..b51106e3a 100644
--- a/fs/copy.ts
+++ b/fs/copy.ts
@@ -22,7 +22,7 @@ async function ensureValidCopy(
options: CopyOptions,
isCopyFolder: boolean = false
): Promise<Deno.FileInfo> {
- let destStat: Deno.FileInfo;
+ let destStat: Deno.FileInfo | null;
destStat = await Deno.lstat(dest).catch(
(): Promise<null> => Promise.resolve(null)
@@ -39,7 +39,7 @@ async function ensureValidCopy(
}
}
- return destStat;
+ return destStat!;
}
function ensureValidCopySync(
@@ -48,7 +48,7 @@ function ensureValidCopySync(
options: CopyOptions,
isCopyFolder: boolean = false
): Deno.FileInfo {
- let destStat: Deno.FileInfo;
+ let destStat: Deno.FileInfo | null;
try {
destStat = Deno.lstatSync(dest);
@@ -56,8 +56,8 @@ function ensureValidCopySync(
// ignore error
}
- if (destStat) {
- if (isCopyFolder && !destStat.isDirectory()) {
+ if (destStat!) {
+ if (isCopyFolder && !destStat!.isDirectory()) {
throw new Error(
`Cannot overwrite non-directory '${dest}' with directory '${src}'.`
);
@@ -67,7 +67,7 @@ function ensureValidCopySync(
}
}
- return destStat;
+ return destStat!;
}
/* copy file to dest */
@@ -80,7 +80,7 @@ async function copyFile(
await Deno.copyFile(src, dest);
if (options.preserveTimestamps) {
const statInfo = await Deno.stat(src);
- await Deno.utime(dest, statInfo.accessed, statInfo.modified);
+ await Deno.utime(dest, statInfo.accessed!, statInfo.modified!);
}
}
/* copy file to dest synchronously */
@@ -89,7 +89,7 @@ function copyFileSync(src: string, dest: string, options: CopyOptions): void {
Deno.copyFileSync(src, dest);
if (options.preserveTimestamps) {
const statInfo = Deno.statSync(src);
- Deno.utimeSync(dest, statInfo.accessed, statInfo.modified);
+ Deno.utimeSync(dest, statInfo.accessed!, statInfo.modified!);
}
}
@@ -105,7 +105,7 @@ async function copySymLink(
await Deno.symlink(originSrcFilePath, dest, type);
if (options.preserveTimestamps) {
const statInfo = await Deno.lstat(src);
- await Deno.utime(dest, statInfo.accessed, statInfo.modified);
+ await Deno.utime(dest, statInfo.accessed!, statInfo.modified!);
}
}
@@ -121,7 +121,7 @@ function copySymlinkSync(
Deno.symlinkSync(originSrcFilePath, dest, type);
if (options.preserveTimestamps) {
const statInfo = Deno.lstatSync(src);
- Deno.utimeSync(dest, statInfo.accessed, statInfo.modified);
+ Deno.utimeSync(dest, statInfo.accessed!, statInfo.modified!);
}
}
@@ -139,13 +139,13 @@ async function copyDir(
if (options.preserveTimestamps) {
const srcStatInfo = await Deno.stat(src);
- await Deno.utime(dest, srcStatInfo.accessed, srcStatInfo.modified);
+ await Deno.utime(dest, srcStatInfo.accessed!, srcStatInfo.modified!);
}
const files = await Deno.readDir(src);
for (const file of files) {
- const srcPath = path.join(src, file.name);
+ const srcPath = path.join(src, file.name!);
const destPath = path.join(dest, path.basename(srcPath as string));
if (file.isDirectory()) {
await copyDir(srcPath, destPath, options);
@@ -167,13 +167,13 @@ function copyDirSync(src: string, dest: string, options: CopyOptions): void {
if (options.preserveTimestamps) {
const srcStatInfo = Deno.statSync(src);
- Deno.utimeSync(dest, srcStatInfo.accessed, srcStatInfo.modified);
+ Deno.utimeSync(dest, srcStatInfo.accessed!, srcStatInfo.modified!);
}
const files = Deno.readDirSync(src);
for (const file of files) {
- const srcPath = path.join(src, file.name);
+ const srcPath = path.join(src, file.name!);
const destPath = path.join(dest, path.basename(srcPath as string));
if (file.isDirectory()) {
copyDirSync(srcPath, destPath, options);
diff --git a/fs/globrex.ts b/fs/globrex.ts
index 439cea348..56def02f8 100644
--- a/fs/globrex.ts
+++ b/fs/globrex.ts
@@ -66,7 +66,7 @@ export function globrex(
// Helper function to build string and segments
function add(
- str,
+ str: string,
options: AddOptions = { split: false, last: false, only: "" }
): void {
const { split, last, only } = options;
@@ -114,13 +114,13 @@ export function globrex(
if (c === ")") {
if (ext.length) {
add(c);
- let type = ext.pop();
+ let type: string | undefined = ext.pop();
if (type === "@") {
add("{1}");
} else if (type === "!") {
add("([^/]*)");
} else {
- add(type);
+ add(type as string);
}
continue;
}
diff --git a/fs/globrex_test.ts b/fs/globrex_test.ts
index 34821b620..c6732b9ea 100644
--- a/fs/globrex_test.ts
+++ b/fs/globrex_test.ts
@@ -5,29 +5,45 @@
import { test } from "../testing/mod.ts";
import { assertEquals } from "../testing/asserts.ts";
import { globrex, GlobrexResult } from "./globrex.ts";
+import { GlobOptions } from "./glob.ts";
const isWin = Deno.build.os === "win";
const t = { equal: assertEquals, is: assertEquals };
-function match(glob, strUnix, strWin?, opts = {}): boolean {
+function match(
+ glob: string,
+ strUnix: string,
+ strWin?: string | object,
+ opts = {}
+): boolean {
if (typeof strWin === "object") {
opts = strWin;
- strWin = false;
+ strWin = "";
}
let res = globrex(glob, opts);
return res.regex.test(isWin && strWin ? strWin : strUnix);
}
-function matchRegex(t, pattern, ifUnix, ifWin, opts): GlobrexResult {
+function matchRegex(
+ pattern: string,
+ ifUnix: string,
+ ifWin: string,
+ opts: GlobOptions
+): GlobrexResult {
const res = globrex(pattern, opts);
- const { regex } = opts.filepath ? res.path : res;
+ const { regex } = opts.filepath ? res.path! : res;
t.is(regex.toString(), isWin ? ifWin : ifUnix, "~> regex matches expectant");
return res;
}
-function matchSegments(t, pattern, ifUnix, ifWin, opts): GlobrexResult {
+function matchSegments(
+ pattern: string,
+ ifUnix: RegExp[],
+ ifWin: RegExp[],
+ opts: GlobOptions
+): GlobrexResult {
const res = globrex(pattern, { filepath: true, ...opts });
- const str = res.path.segments.join(" ");
+ const str = res.path!.segments.join(" ");
const exp = (isWin ? ifWin : ifUnix).join(" ");
t.is(str, exp);
return res;
@@ -191,7 +207,7 @@ test({
t.equal(match("f?o", "fooo", { extended: true }), false);
t.equal(match("f?oo", "foo", { extended: true }), false);
- const tester = (globstar): void => {
+ const tester = (globstar: boolean): void => {
t.equal(
match("f?o", "foo", { extended: true, globstar, flags: "g" }),
true
@@ -235,7 +251,7 @@ test({
t.equal(match("fo[!tz]", "fot", { extended: true }), false);
t.equal(match("fo[!tz]", "fob", { extended: true }), true);
- const tester = (globstar): void => {
+ const tester = (globstar: boolean): void => {
t.equal(
match("fo[oz]", "foo", { extended: true, globstar, flags: "g" }),
true
@@ -321,7 +337,7 @@ test({
t.equal(match("foo{bar,baaz}", "foobuzz", { extended: true }), false);
t.equal(match("foo{bar,b*z}", "foobuzz", { extended: true }), true);
- const tester = (globstar): void => {
+ const tester = (globstar: boolean): void => {
t.equal(
match("foo{bar,baaz}", "foobaaz", {
extended: true,
@@ -405,7 +421,7 @@ test({
false
);
- const tester = (globstar): void => {
+ const tester = (globstar: boolean): void => {
t.equal(
match(
"http://?o[oz].b*z.com/{*.js,*.html}",
@@ -456,7 +472,7 @@ test({
test({
name: "globrex: standard globstar",
fn(): void {
- const tester = (globstar): void => {
+ const tester = (globstar: boolean): void => {
t.equal(
match(
"http://foo.com/**/{*.js,*.html}",
@@ -491,7 +507,7 @@ test({
test({
name: "globrex: remaining chars should match themself",
fn(): void {
- const tester = (globstar): void => {
+ const tester = (globstar: boolean): void => {
const testExtStr = "\\/$^+.()=!|,.*";
t.equal(match(testExtStr, testExtStr, { extended: true }), true);
t.equal(
@@ -801,47 +817,43 @@ test({
res = globrex("", opts);
t.is(res.hasOwnProperty("path"), true);
- t.is(res.path.hasOwnProperty("regex"), true);
- t.is(res.path.hasOwnProperty("segments"), true);
- t.is(Array.isArray(res.path.segments), true);
+ t.is(res.path!.hasOwnProperty("regex"), true);
+ t.is(res.path!.hasOwnProperty("segments"), true);
+ t.is(Array.isArray(res.path!.segments), true);
pattern = "foo/bar/baz.js";
res = matchRegex(
- t,
pattern,
"/^foo\\/bar\\/baz\\.js$/",
"/^foo\\\\+bar\\\\+baz\\.js$/",
opts
);
- t.is(res.path.segments.length, 3);
+ t.is(res.path!.segments.length, 3);
res = matchRegex(
- t,
"../foo/bar.js",
"/^\\.\\.\\/foo\\/bar\\.js$/",
"/^\\.\\.\\\\+foo\\\\+bar\\.js$/",
opts
);
- t.is(res.path.segments.length, 3);
+ t.is(res.path!.segments.length, 3);
res = matchRegex(
- t,
"*/bar.js",
"/^.*\\/bar\\.js$/",
"/^.*\\\\+bar\\.js$/",
opts
);
- t.is(res.path.segments.length, 2);
+ t.is(res.path!.segments.length, 2);
opts.globstar = true;
res = matchRegex(
- t,
"**/bar.js",
"/^((?:[^\\/]*(?:\\/|$))*)bar\\.js$/",
"/^((?:[^\\\\]*(?:\\\\|$))*)bar\\.js$/",
opts
);
- t.is(res.path.segments.length, 2);
+ t.is(res.path!.segments.length, 2);
}
});
@@ -854,42 +866,42 @@ test({
unix = [/^foo$/, /^bar$/, /^([^\/]*)$/, /^baz\.(md|js|txt)$/];
win = [/^foo$/, /^bar$/, /^([^\\]*)$/, /^baz\.(md|js|txt)$/];
- matchSegments(t, "foo/bar/*/baz.{md,js,txt}", unix, win, {
+ matchSegments("foo/bar/*/baz.{md,js,txt}", unix, win, {
...opts,
globstar: true
});
unix = [/^foo$/, /^.*$/, /^baz\.md$/];
win = [/^foo$/, /^.*$/, /^baz\.md$/];
- matchSegments(t, "foo/*/baz.md", unix, win, opts);
+ matchSegments("foo/*/baz.md", unix, win, opts);
unix = [/^foo$/, /^.*$/, /^baz\.md$/];
win = [/^foo$/, /^.*$/, /^baz\.md$/];
- matchSegments(t, "foo/**/baz.md", unix, win, opts);
+ matchSegments("foo/**/baz.md", unix, win, opts);
unix = [/^foo$/, /^((?:[^\/]*(?:\/|$))*)$/, /^baz\.md$/];
win = [/^foo$/, /^((?:[^\\]*(?:\\|$))*)$/, /^baz\.md$/];
- matchSegments(t, "foo/**/baz.md", unix, win, { ...opts, globstar: true });
+ matchSegments("foo/**/baz.md", unix, win, { ...opts, globstar: true });
unix = [/^foo$/, /^.*$/, /^.*\.md$/];
win = [/^foo$/, /^.*$/, /^.*\.md$/];
- matchSegments(t, "foo/**/*.md", unix, win, opts);
+ matchSegments("foo/**/*.md", unix, win, opts);
unix = [/^foo$/, /^((?:[^\/]*(?:\/|$))*)$/, /^([^\/]*)\.md$/];
win = [/^foo$/, /^((?:[^\\]*(?:\\|$))*)$/, /^([^\\]*)\.md$/];
- matchSegments(t, "foo/**/*.md", unix, win, { ...opts, globstar: true });
+ matchSegments("foo/**/*.md", unix, win, { ...opts, globstar: true });
unix = [/^foo$/, /^:$/, /^b:az$/];
win = [/^foo$/, /^:$/, /^b:az$/];
- matchSegments(t, "foo/:/b:az", unix, win, opts);
+ matchSegments("foo/:/b:az", unix, win, opts);
unix = [/^foo$/, /^baz\.md$/];
win = [/^foo$/, /^baz\.md$/];
- matchSegments(t, "foo///baz.md", unix, win, { ...opts, strict: true });
+ matchSegments("foo///baz.md", unix, win, { ...opts, strict: true });
unix = [/^foo$/, /^baz\.md$/];
win = [/^foo$/, /^baz\.md$/];
- matchSegments(t, "foo///baz.md", unix, win, { ...opts, strict: false });
+ matchSegments("foo///baz.md", unix, win, { ...opts, strict: false });
}
});
diff --git a/fs/path/join_test.ts b/fs/path/join_test.ts
index d82a1b471..2c0f35e48 100644
--- a/fs/path/join_test.ts
+++ b/fs/path/join_test.ts
@@ -108,18 +108,21 @@ const windowsJoinTests = [
test(function join() {
joinTests.forEach(function(p) {
- const actual = path.posix.join.apply(null, p[0]);
+ const _p = p[0] as string[];
+ const actual = path.posix.join.apply(null, _p);
assertEquals(actual, p[1]);
});
});
test(function joinWin32() {
joinTests.forEach(function(p) {
- const actual = path.win32.join.apply(null, p[0]).replace(backslashRE, "/");
+ const _p = p[0] as string[];
+ const actual = path.win32.join.apply(null, _p).replace(backslashRE, "/");
assertEquals(actual, p[1]);
});
windowsJoinTests.forEach(function(p) {
- const actual = path.win32.join.apply(null, p[0]);
+ const _p = p[0] as string[];
+ const actual = path.win32.join.apply(null, _p);
assertEquals(actual, p[1]);
});
});
diff --git a/fs/path/parse_format_test.ts b/fs/path/parse_format_test.ts
index 05327a139..8829891da 100644
--- a/fs/path/parse_format_test.ts
+++ b/fs/path/parse_format_test.ts
@@ -79,8 +79,8 @@ const unixSpecialCaseFormatTests = [
[{}, ""]
];
-function checkParseFormat(path, paths): void {
- paths.forEach(function(p) {
+function checkParseFormat(path: any, paths: any): void {
+ paths.forEach(function(p: Record<string, unknown>[]) {
const element = p[0];
const output = path.parse(element);
assertEquals(typeof output.root, "string");
@@ -95,8 +95,8 @@ function checkParseFormat(path, paths): void {
});
}
-function checkSpecialCaseParseFormat(path, testCases): void {
- testCases.forEach(function(testCase) {
+function checkSpecialCaseParseFormat(path: any, testCases: any): void {
+ testCases.forEach(function(testCase: Record<string, unknown>[]) {
const element = testCase[0];
const expect = testCase[1];
const output = path.parse(element);
@@ -106,7 +106,7 @@ function checkSpecialCaseParseFormat(path, testCases): void {
});
}
-function checkFormat(path, testCases): void {
+function checkFormat(path: any, testCases: unknown[][]): void {
testCases.forEach(function(testCase) {
assertEquals(path.format(testCase[0]), testCase[1]);
});
diff --git a/fs/path/resolve_test.ts b/fs/path/resolve_test.ts
index 7ee9513c1..606570aad 100644
--- a/fs/path/resolve_test.ts
+++ b/fs/path/resolve_test.ts
@@ -37,14 +37,16 @@ const posixTests =
test(function resolve() {
posixTests.forEach(function(p) {
- const actual = path.posix.resolve.apply(null, p[0]);
+ const _p = p[0] as string[];
+ const actual = path.posix.resolve.apply(null, _p);
assertEquals(actual, p[1]);
});
});
test(function resolveWin32() {
windowsTests.forEach(function(p) {
- const actual = path.win32.resolve.apply(null, p[0]);
+ const _p = p[0] as string[];
+ const actual = path.win32.resolve.apply(null, _p);
assertEquals(actual, p[1]);
});
});
diff --git a/fs/path/utils.ts b/fs/path/utils.ts
index e50cb6d3c..7a4cd7299 100644
--- a/fs/path/utils.ts
+++ b/fs/path/utils.ts
@@ -49,7 +49,7 @@ export function normalizeString(
let code: number;
for (let i = 0, len = path.length; i <= len; ++i) {
if (i < len) code = path.charCodeAt(i);
- else if (isPathSeparator(code)) break;
+ else if (isPathSeparator(code!)) break;
else code = CHAR_FORWARD_SLASH;
if (isPathSeparator(code)) {
@@ -107,7 +107,7 @@ export function _format(
sep: string,
pathObject: FormatInputPathObject
): string {
- const dir: string | null = pathObject.dir || pathObject.root;
+ const dir: string | undefined = pathObject.dir || pathObject.root;
const base: string =
pathObject.base || (pathObject.name || "") + (pathObject.ext || "");
if (!dir) return base;
diff --git a/fs/path/win32.ts b/fs/path/win32.ts
index 6b906bc7b..d42cc4885 100644
--- a/fs/path/win32.ts
+++ b/fs/path/win32.ts
@@ -171,7 +171,7 @@ export function normalize(path: string): string {
const len = path.length;
if (len === 0) return ".";
let rootEnd = 0;
- let device: string;
+ let device: string | undefined;
let isAbsolute = false;
const code = path.charCodeAt(0);
@@ -301,7 +301,7 @@ export function join(...paths: string[]): string {
const pathsCount = paths.length;
if (pathsCount === 0) return ".";
- let joined: string;
+ let joined: string | undefined;
let firstPart: string;
for (let i = 0; i < pathsCount; ++i) {
let path = paths[i];
@@ -329,6 +329,7 @@ export function join(...paths: string[]): string {
// path.join('//server', 'share') -> '\\\\server\\share\\')
let needsReplace = true;
let slashCount = 0;
+ firstPart = firstPart!;
if (isPathSeparator(firstPart.charCodeAt(0))) {
++slashCount;
const firstLen = firstPart.length;
diff --git a/fs/utils.ts b/fs/utils.ts
index 83390ad70..ab5bf2f0e 100644
--- a/fs/utils.ts
+++ b/fs/utils.ts
@@ -16,10 +16,14 @@ export function isSubdir(
}
const srcArray = src.split(sep);
const destArray = dest.split(sep);
-
- return srcArray.reduce((acc, current, i): boolean => {
- return acc && destArray[i] === current;
- }, true);
+ // 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";
@@ -29,7 +33,7 @@ export type PathType = "file" | "dir" | "symlink";
*
* @param fileInfo A FileInfo describes a file and is returned by `stat`, `lstat`
*/
-export function getFileInfoType(fileInfo: Deno.FileInfo): PathType {
+export function getFileInfoType(fileInfo: Deno.FileInfo): PathType | undefined {
return fileInfo.isFile()
? "file"
: fileInfo.isDirectory()
diff --git a/fs/walk.ts b/fs/walk.ts
index 5cfd7e63d..4520fe174 100644
--- a/fs/walk.ts
+++ b/fs/walk.ts
@@ -71,7 +71,7 @@ export async function* walk(
root: string,
options: WalkOptions = {}
): AsyncIterableIterator<WalkInfo> {
- options.maxDepth -= 1;
+ options.maxDepth! -= 1;
let ls: FileInfo[] = [];
try {
ls = await readDir(root);
@@ -90,14 +90,14 @@ export async function* walk(
}
}
- const filename = join(root, info.name);
+ const filename = join(root, info.name!);
if (info.isFile()) {
if (include(filename, options)) {
yield { filename, info };
}
} else {
- if (!(options.maxDepth < 0)) {
+ if (!(options.maxDepth! < 0)) {
yield* walk(filename, options);
}
}
@@ -109,7 +109,7 @@ export function* walkSync(
root: string = ".",
options: WalkOptions = {}
): IterableIterator<WalkInfo> {
- options.maxDepth -= 1;
+ options.maxDepth! -= 1;
let ls: FileInfo[] = [];
try {
ls = readDirSync(root);
@@ -127,14 +127,14 @@ export function* walkSync(
}
}
- const filename = join(root, info.name);
+ const filename = join(root, info.name!);
if (info.isFile()) {
if (include(filename, options)) {
yield { filename, info };
}
} else {
- if (!(options.maxDepth < 0)) {
+ if (!(options.maxDepth! < 0)) {
yield* walkSync(filename, options);
}
}
diff --git a/fs/walk_test.ts b/fs/walk_test.ts
index 20e105a52..1111f9f00 100644
--- a/fs/walk_test.ts
+++ b/fs/walk_test.ts
@@ -5,7 +5,7 @@ import { test, TestFunction, runIfMain } from "../testing/mod.ts";
import { assertEquals } from "../testing/asserts.ts";
export async function testWalk(
- setup: (string) => void | Promise<void>,
+ setup: (arg0: string) => void | Promise<void>,
t: TestFunction
): Promise<void> {
const name = t.name;