2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-10-16 14:39:33 -04:00
|
|
|
import * as path from "../path/mod.ts";
|
2019-03-12 01:52:43 -04:00
|
|
|
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
|
2020-05-09 08:34:47 -04:00
|
|
|
import { getFileInfoType } from "./_util.ts";
|
2019-03-14 10:24:54 -04:00
|
|
|
|
2019-03-12 01:52:43 -04:00
|
|
|
/**
|
|
|
|
* Ensures that the file exists.
|
2019-06-19 00:22:01 -04:00
|
|
|
* If the file that is requested to be created is in directories that do not
|
|
|
|
* exist.
|
|
|
|
* these directories are created. If the file already exists,
|
|
|
|
* it is NOTMODIFIED.
|
2020-05-16 14:36:13 -04:00
|
|
|
* Requires the `--allow-read` and `--allow-write` flag.
|
2019-03-12 01:52:43 -04:00
|
|
|
*/
|
|
|
|
export async function ensureFile(filePath: string): Promise<void> {
|
|
|
|
try {
|
|
|
|
// if file exists
|
2020-06-12 15:23:38 -04:00
|
|
|
const stat = await Deno.lstat(filePath);
|
2020-04-16 01:40:30 -04:00
|
|
|
if (!stat.isFile) {
|
2019-04-06 21:01:23 -04:00
|
|
|
throw new Error(
|
2020-07-14 15:24:17 -04:00
|
|
|
`Ensure path exists, expected 'file', got '${getFileInfoType(stat)}'`,
|
2019-04-06 21:01:23 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2019-03-12 01:52:43 -04:00
|
|
|
// if file not exists
|
2020-02-24 15:48:35 -05:00
|
|
|
if (err instanceof Deno.errors.NotFound) {
|
2019-12-18 09:45:19 -05:00
|
|
|
// ensure dir exists
|
|
|
|
await ensureDir(path.dirname(filePath));
|
|
|
|
// create file
|
2020-06-12 15:23:38 -04:00
|
|
|
await Deno.writeFile(filePath, new Uint8Array());
|
2019-12-18 09:45:19 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
2019-03-12 01:52:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures that the file exists.
|
2019-06-19 00:22:01 -04:00
|
|
|
* If the file that is requested to be created is in directories that do not
|
|
|
|
* exist,
|
|
|
|
* these directories are created. If the file already exists,
|
|
|
|
* it is NOT MODIFIED.
|
2020-05-16 14:36:13 -04:00
|
|
|
* Requires the `--allow-read` and `--allow-write` flag.
|
2019-03-12 01:52:43 -04:00
|
|
|
*/
|
|
|
|
export function ensureFileSync(filePath: string): void {
|
|
|
|
try {
|
|
|
|
// if file exists
|
2020-06-12 15:23:38 -04:00
|
|
|
const stat = Deno.lstatSync(filePath);
|
2020-04-16 01:40:30 -04:00
|
|
|
if (!stat.isFile) {
|
2019-04-06 21:01:23 -04:00
|
|
|
throw new Error(
|
2020-07-14 15:24:17 -04:00
|
|
|
`Ensure path exists, expected 'file', got '${getFileInfoType(stat)}'`,
|
2019-04-06 21:01:23 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2019-03-12 01:52:43 -04:00
|
|
|
// if file not exists
|
2020-02-24 15:48:35 -05:00
|
|
|
if (err instanceof Deno.errors.NotFound) {
|
2019-12-18 09:45:19 -05:00
|
|
|
// ensure dir exists
|
|
|
|
ensureDirSync(path.dirname(filePath));
|
|
|
|
// create file
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.writeFileSync(filePath, new Uint8Array());
|
2019-12-18 09:45:19 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw err;
|
2019-03-12 01:52:43 -04:00
|
|
|
}
|
|
|
|
}
|