2019-03-12 01:52:43 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-04-06 21:01:23 -04:00
|
|
|
import { getFileInfoType } from "./utils.ts";
|
2019-03-12 01:52:43 -04:00
|
|
|
/**
|
2019-03-14 10:24:54 -04:00
|
|
|
* Ensures that the directory exists.
|
|
|
|
* If the directory structure does not exist, it is created. Like mkdir -p.
|
2019-03-12 01:52:43 -04:00
|
|
|
*/
|
|
|
|
export async function ensureDir(dir: string): Promise<void> {
|
2019-04-06 21:01:23 -04:00
|
|
|
let pathExists = false;
|
2019-03-12 01:52:43 -04:00
|
|
|
try {
|
|
|
|
// if dir exists
|
2019-04-06 21:01:23 -04:00
|
|
|
const stat = await Deno.stat(dir);
|
|
|
|
pathExists = true;
|
|
|
|
if (!stat.isDirectory()) {
|
|
|
|
throw new Error(
|
|
|
|
`Ensure path exists, expected 'dir', got '${getFileInfoType(stat)}'`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (pathExists) {
|
|
|
|
throw err;
|
|
|
|
}
|
2019-03-12 01:52:43 -04:00
|
|
|
// if dir not exists. then create it.
|
|
|
|
await Deno.mkdir(dir, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-14 10:24:54 -04:00
|
|
|
* Ensures that the directory exists.
|
|
|
|
* If the directory structure does not exist, it is created. Like mkdir -p.
|
2019-03-12 01:52:43 -04:00
|
|
|
*/
|
|
|
|
export function ensureDirSync(dir: string): void {
|
2019-04-06 21:01:23 -04:00
|
|
|
let pathExists = false;
|
2019-03-12 01:52:43 -04:00
|
|
|
try {
|
|
|
|
// if dir exists
|
2019-04-06 21:01:23 -04:00
|
|
|
const stat = Deno.statSync(dir);
|
|
|
|
pathExists = true;
|
|
|
|
if (!stat.isDirectory()) {
|
|
|
|
throw new Error(
|
|
|
|
`Ensure path exists, expected 'dir', got '${getFileInfoType(stat)}'`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (pathExists) {
|
|
|
|
throw err;
|
|
|
|
}
|
2019-03-12 01:52:43 -04:00
|
|
|
// if dir not exists. then create it.
|
|
|
|
Deno.mkdirSync(dir, true);
|
|
|
|
}
|
|
|
|
}
|