summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/deno.ts2
-rw-r--r--js/mkdir.ts37
-rw-r--r--js/mkdir_test.ts28
-rw-r--r--js/os.ts19
-rw-r--r--js/os_test.ts19
-rw-r--r--js/unit_tests.ts1
6 files changed, 67 insertions, 39 deletions
diff --git a/js/deno.ts b/js/deno.ts
index ce01c5dad..fab8ef4e2 100644
--- a/js/deno.ts
+++ b/js/deno.ts
@@ -6,12 +6,12 @@ export {
exit,
FileInfo,
makeTempDirSync,
- mkdirSync,
renameSync,
statSync,
lstatSync,
writeFileSync
} from "./os";
+export { mkdirSync, mkdir } from "./mkdir";
export { readFileSync, readFile } from "./read_file";
export { ErrorKind, DenoError } from "./errors";
export { libdeno } from "./libdeno";
diff --git a/js/mkdir.ts b/js/mkdir.ts
new file mode 100644
index 000000000..8e76fc17e
--- /dev/null
+++ b/js/mkdir.ts
@@ -0,0 +1,37 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+import * as fbs from "gen/msg_generated";
+import { flatbuffers } from "flatbuffers";
+import * as dispatch from "./dispatch";
+
+/**
+ * Creates a new directory with the specified path and permission synchronously.
+ *
+ * import { mkdirSync } from "deno";
+ * mkdirSync("new_dir");
+ */
+export function mkdirSync(path: string, mode = 0o777): void {
+ dispatch.sendSync(...req(path, mode));
+}
+
+/**
+ * Creates a new directory with the specified path and permission.
+ *
+ * import { mkdir } from "deno";
+ * await mkdir("new_dir");
+ */
+export async function mkdir(path: string, mode = 0o777): Promise<void> {
+ await dispatch.sendAsync(...req(path, mode));
+}
+
+function req(
+ path: string,
+ mode: number
+): [flatbuffers.Builder, fbs.Any, flatbuffers.Offset] {
+ const builder = new flatbuffers.Builder();
+ const path_ = builder.createString(path);
+ fbs.Mkdir.startMkdir(builder);
+ fbs.Mkdir.addPath(builder, path_);
+ fbs.Mkdir.addMode(builder, mode);
+ const msg = fbs.Mkdir.endMkdir(builder);
+ return [builder, fbs.Any.Mkdir, msg];
+}
diff --git a/js/mkdir_test.ts b/js/mkdir_test.ts
new file mode 100644
index 000000000..ed275bd36
--- /dev/null
+++ b/js/mkdir_test.ts
@@ -0,0 +1,28 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+import { test, testPerm, assert, assertEqual } from "./test_util.ts";
+import * as deno from "deno";
+
+testPerm({ write: true }, function mkdirSyncSuccess() {
+ const path = deno.makeTempDirSync() + "/dir/subdir";
+ deno.mkdirSync(path);
+ const pathInfo = deno.statSync(path);
+ assert(pathInfo.isDirectory());
+});
+
+testPerm({ write: false }, function mkdirSyncPerm() {
+ let err;
+ try {
+ deno.mkdirSync("/baddir");
+ } catch (e) {
+ err = e;
+ }
+ assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
+ assertEqual(err.name, "PermissionDenied");
+});
+
+testPerm({ write: true }, async function mkdirSuccess() {
+ const path = deno.makeTempDirSync() + "/dir/subdir";
+ await deno.mkdir(path);
+ const pathInfo = deno.statSync(path);
+ assert(pathInfo.isDirectory());
+});
diff --git a/js/os.ts b/js/os.ts
index d29d0d74a..4849ac19f 100644
--- a/js/os.ts
+++ b/js/os.ts
@@ -107,25 +107,6 @@ export function makeTempDirSync({
return path!;
}
-// mkdir creates a new directory with the specified name
-// and permission bits (before umask).
-export function mkdirSync(path: string, mode = 0o777): void {
- /* Ideally we could write:
- const res = sendSync({
- command: fbs.Command.MKDIR_SYNC,
- mkdirSyncPath: path,
- mkdirSyncMode: mode,
- });
- */
- const builder = new flatbuffers.Builder();
- const path_ = builder.createString(path);
- fbs.MkdirSync.startMkdirSync(builder);
- fbs.MkdirSync.addPath(builder, path_);
- fbs.MkdirSync.addMode(builder, mode);
- const msg = fbs.MkdirSync.endMkdirSync(builder);
- sendSync(builder, fbs.Any.MkdirSync, msg);
-}
-
function createEnv(_msg: fbs.EnvironRes): { [index: string]: string } {
const env: { [index: string]: string } = {};
diff --git a/js/os_test.ts b/js/os_test.ts
index 544d7d49f..df88b5085 100644
--- a/js/os_test.ts
+++ b/js/os_test.ts
@@ -152,25 +152,6 @@ test(function makeTempDirSyncPerm() {
assertEqual(err.name, "PermissionDenied");
});
-testPerm({ write: true }, function mkdirSync() {
- const path = deno.makeTempDirSync() + "/dir/subdir";
- deno.mkdirSync(path);
- const pathInfo = deno.statSync(path);
- assert(pathInfo.isDirectory());
-});
-
-testPerm({ write: false }, function mkdDirSyncPerm() {
- let err;
- try {
- const path = "/baddir";
- deno.mkdirSync(path);
- } catch (err_) {
- err = err_;
- }
- assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
- assertEqual(err.name, "PermissionDenied");
-});
-
testPerm({ write: true }, function renameSync() {
const testDir = deno.makeTempDirSync() + "/test-rename";
const oldpath = testDir + "/oldpath";
diff --git a/js/unit_tests.ts b/js/unit_tests.ts
index efbc7383c..1c5a9227e 100644
--- a/js/unit_tests.ts
+++ b/js/unit_tests.ts
@@ -6,3 +6,4 @@ import "./console_test.ts";
import "./fetch_test.ts";
import "./os_test.ts";
import "./read_file_test.ts";
+import "./mkdir_test.ts";