Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix broken path in chat example in windows #4648

Closed
wants to merge 12 commits into from
34 changes: 34 additions & 0 deletions std/node/_fs/_fs_exists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

type ExitsCallback = (exists: boolean) => void;

/* Deprecated in node api */

export function exists(path: string, callback: ExitsCallback): void {
new Promise(async (resolve, reject) => {
try {
await Deno.lstat(path);
resolve();
} catch (err) {
reject(err);
}
})
.then(() => {
callback(true);
})
.catch(() => {
callback(false);
});
}

export function existsSync(path: string): boolean {
try {
Deno.lstatSync(path);
return true;
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false;
}
throw err;
}
}
28 changes: 28 additions & 0 deletions std/node/_fs/_fs_exists_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

import { assertEquals } from "../../testing/asserts.ts";
import { exists, existsSync } from "./_fs_exists.ts";
import * as path from "../../path/mod.ts";

const { test } = Deno;

const testAvailableData = path.resolve(path.join("testdata", "hello.txt"));
const testNotAvailableData = path.resolve(
path.join("testdata", "notAvailable.txt")
);

test(async function existsFile() {
const availableFile = await new Promise((resolve, reject) => {
exists(testAvailableData, (exists: boolean) => resolve(exists));
});
const notAvailableFile = await new Promise((resolve, reject) => {
exists(testNotAvailableData, (exists: boolean) => resolve(exists));
});
assertEquals(availableFile, true);
assertEquals(notAvailableFile, false);
});

test(function existsSyncFile() {
assertEquals(existsSync(testAvailableData), true);
assertEquals(existsSync(testNotAvailableData), false);
});
73 changes: 73 additions & 0 deletions std/node/_fs/_fs_mkdir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { CallbackWithError } from "./_fs_common.ts";

type Path = string; // TODO path can also be a Buffer or URL.
type MkdirOptions =
| { recursive?: boolean; mode?: number | undefined }
| number
| boolean;

export function mkdir(
path: Path,
options?: MkdirOptions | CallbackWithError,
callback?: CallbackWithError
): void {
let mode: number = 0o777;
let recursive: boolean = false;

if (typeof options == "function") {
callback == options;
} else if (typeof options === "number") {
mode = options;
} else if (typeof options === "boolean") {
recursive = options;
} else if (options) {
if (options.recursive !== undefined) recursive = options.recursive;
if (options.mode !== undefined) mode = options.mode;
}
if (typeof recursive !== "boolean")
throw new Deno.errors.InvalidData(
"invalid recursive option , must be a boolean"
);
new Promise(async (resolve, reject) => {
try {
await Deno.mkdir(path, recursive, mode);
resolve();
} catch (err) {
reject(err);
}
})
.then(() => {
if (callback && typeof callback == "function") {
callback();
}
})
.catch((err) => {
if (callback && typeof callback == "function") {
callback(err);
}
});
}

export function mkdirSync(path: Path, options?: MkdirOptions): void {
let mode: number = 0o777;
let recursive: boolean = false;

if (typeof options === "number") {
mode = options;
} else if (typeof options === "boolean") {
recursive = options;
} else if (options) {
if (options.recursive !== undefined) recursive = options.recursive;
if (options.mode !== undefined) mode = options.mode;
}
if (typeof recursive !== "boolean")
throw new Deno.errors.InvalidData(
"invalid recursive option , must be a boolean"
);
try {
Deno.mkdirSync(path, recursive, mode);
} catch (err) {
throw err;
}
}
6 changes: 6 additions & 0 deletions std/node/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { close, closeSync } from "./_fs/_fs_close.ts";
import * as constants from "./_fs/_fs_constants.ts";
import { readFile, readFileSync } from "./_fs/_fs_readFile.ts";
import { readlink, readlinkSync } from "./_fs/_fs_readlink.ts";
import { exists, existsSync } from "./_fs/_fs_exists.ts";
import { mkdir, mkdirSync } from "./_fs/_fs_mkdir.ts";

export {
access,
Expand All @@ -21,8 +23,12 @@ export {
close,
closeSync,
constants,
exists,
existsSync,
readFile,
readFileSync,
readlink,
readlinkSync,
mkdir,
mkdirSync,
};