mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
Add mkdirp (#59)
This commit is contained in:
parent
d3193a09de
commit
7a3c70e74b
@ -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
|
||||
|
@ -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. |
|
||||
|
||||
---
|
||||
|
||||
|
@ -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'
|
||||
|
24
mkdirp/mkdirp.ts
Normal file
24
mkdirp/mkdirp.ts
Normal file
@ -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<void> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
17
mkdirp/readme.md
Normal file
17
mkdirp/readme.md
Normal file
@ -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<void>`
|
||||
|
||||
Creates directories if they do not already exist and makes parent directories as needed.
|
30
mkdirp/test.ts
Normal file
30
mkdirp/test.ts
Normal file
@ -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<void> {
|
||||
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<void> {
|
||||
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<void> {
|
||||
try {
|
||||
await mkdirp("./test.ts/fest.fs");
|
||||
} catch (err) {
|
||||
// TODO: assert caught DenoError of kind NOT_A_DIRECTORY or similar
|
||||
assert(err);
|
||||
}
|
||||
});
|
@ -177,7 +177,7 @@ function guessContentType(filename: string): string {
|
||||
return contentType;
|
||||
}
|
||||
|
||||
return extensionsMap[''];
|
||||
return extensionsMap[""];
|
||||
}
|
||||
|
||||
async function serveFile(req: ServerRequest, filename: string) {
|
||||
|
5
test.ts
5
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 () => {
|
||||
|
Loading…
Reference in New Issue
Block a user