From 7a3c70e74b46885eaee4dea6529daf1f2c2f84a0 Mon Sep 17 00:00:00 2001 From: chiefbiiko Date: Tue, 1 Jan 2019 23:45:41 +0000 Subject: [PATCH] Add mkdirp (#59) --- .travis.yml | 2 +- README.md | 1 + azure-pipelines.yml | 4 ++-- mkdirp/mkdirp.ts | 24 ++++++++++++++++++++++++ mkdirp/readme.md | 17 +++++++++++++++++ mkdirp/test.ts | 30 ++++++++++++++++++++++++++++++ net/file_server.ts | 2 +- test.ts | 5 ++++- 8 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 mkdirp/mkdirp.ts create mode 100644 mkdirp/readme.md create mode 100644 mkdirp/test.ts diff --git a/.travis.yml b/.travis.yml index ba2580074..113dda997 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,4 +5,4 @@ install: - export PATH="$HOME/.deno/bin:$PATH" script: -- deno test.ts --allow-run --allow-net +- deno test.ts --allow-run --allow-net --allow-write diff --git a/README.md b/README.md index 74043c3e2..6032730e2 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ for Deno. | [path](./path/) | File path manipulation. | | [flags](./flags/) | Command line arguments parser. | | [logging](./logging/) | Command line logging | +| [mkdirp](./mkdirp/) | Make directory branches. | --- diff --git a/azure-pipelines.yml b/azure-pipelines.yml index d0c063140..98a45284f 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -9,7 +9,7 @@ jobs: steps: - script: curl -L https://deno.land/x/install/install.py | python - $(DENO_VERSION) - script: echo '##vso[task.prependpath]$(HOME)/.deno/bin/' - - script: deno test.ts --allow-run --allow-net + - script: deno test.ts --allow-run --allow-net --allow-write - job: 'Mac' pool: @@ -17,7 +17,7 @@ jobs: steps: - script: curl -L https://deno.land/x/install/install.py | python - $(DENO_VERSION) - script: echo '##vso[task.prependpath]$(HOME)/.deno/bin/' - - script: deno test.ts --allow-run --allow-net + - script: deno test.ts --allow-run --allow-net --allow-write # TODO Windows is broken on a bug: https://github.com/denoland/deno/issues/1384 #- job: 'Windows' diff --git a/mkdirp/mkdirp.ts b/mkdirp/mkdirp.ts new file mode 100644 index 000000000..9d27c751a --- /dev/null +++ b/mkdirp/mkdirp.ts @@ -0,0 +1,24 @@ +import { ErrorKind, FileInfo, lstat, mkdir, platform } from "deno"; + +const PATH_SEPARATOR: string = platform.os === "win" ? "\\" : "/"; + +export async function mkdirp(path: string, mode?: number): Promise { + for ( + let parts: string[] = path.split(/\/|\\/), + parts_len: number = parts.length, + level: string, + info: FileInfo, + i: number = 0; + i < parts_len; + i++ + ) { + level = parts.slice(0, i + 1).join(PATH_SEPARATOR); + try { + info = await lstat(level); + if (!info.isDirectory()) throw Error(`${level} is not a directory`); + } catch (err) { + if (err.kind !== ErrorKind.NotFound) throw err; + await mkdir(level, mode); + } + } +} diff --git a/mkdirp/readme.md b/mkdirp/readme.md new file mode 100644 index 000000000..48269aa28 --- /dev/null +++ b/mkdirp/readme.md @@ -0,0 +1,17 @@ +# deno-mkdirp + +`mkdir -p` 4 `deno`. + +## Import + +```ts +import { mkdirp } from "https://deno.land/x/std/mkdirp/mkdirp.ts"; +``` + +## API + +Same as [`deno.mkdir`](https://deno.land/typedoc/index.html#mkdir). + +### `mkdirp(path: string, mode?: number) : Promise` + +Creates directories if they do not already exist and makes parent directories as needed. diff --git a/mkdirp/test.ts b/mkdirp/test.ts new file mode 100644 index 000000000..9f5d7fb92 --- /dev/null +++ b/mkdirp/test.ts @@ -0,0 +1,30 @@ +import { cwd, lstat, makeTempDirSync, removeAll, FileInfo } from "deno"; +import { test, assert } from "https://deno.land/x/testing/testing.ts"; +import { mkdirp } from "./mkdirp.ts"; + +let root: string = `${cwd()}/${Date.now()}`; //makeTempDirSync(); + +test(async function createsNestedDirs(): Promise { + const leaf: string = `${root}/levelx/levely`; + await mkdirp(leaf); + const info: FileInfo = await lstat(leaf); + assert(info.isDirectory()); + await removeAll(root); +}); + +test(async function handlesAnyPathSeparator(): Promise { + const leaf: string = `${root}\\levelx\\levely`; + await mkdirp(leaf); + const info: FileInfo = await lstat(leaf.replace(/\\/g, "/")); + assert(info.isDirectory()); + await removeAll(root); +}); + +test(async function failsNonDir(): Promise { + try { + await mkdirp("./test.ts/fest.fs"); + } catch (err) { + // TODO: assert caught DenoError of kind NOT_A_DIRECTORY or similar + assert(err); + } +}); diff --git a/net/file_server.ts b/net/file_server.ts index aaaec64d5..91afa2d57 100755 --- a/net/file_server.ts +++ b/net/file_server.ts @@ -177,7 +177,7 @@ function guessContentType(filename: string): string { return contentType; } - return extensionsMap['']; + return extensionsMap[""]; } async function serveFile(req: ServerRequest, filename: string) { diff --git a/test.ts b/test.ts index e2a76f38f..25178ecc6 100755 --- a/test.ts +++ b/test.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env deno --allow-run --allow-net +#!/usr/bin/env deno --allow-run --allow-net --allow-write import { run } from "deno"; // colors tests @@ -32,6 +32,9 @@ import "path/relative_test.ts"; import "path/resolve_test.ts"; import "path/zero_length_strings_test.ts"; +// mkdirp tests +import "mkdirp/test.ts"; + // I am also too lazy to do this properly LOL runTests(new Promise(res => setTimeout(res, 5000))); (async () => {