2023-01-03 10:47:44 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2023-08-14 07:08:42 +00:00
|
|
|
import { dirname } from "../path/dirname.ts";
|
2019-03-12 05:52:43 +00:00
|
|
|
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
|
2023-12-21 07:39:51 +00:00
|
|
|
import { getFileInfoType } from "./_get_file_info_type.ts";
|
|
|
|
import { toPathString } from "./_to_path_string.ts";
|
2019-03-14 14:24:54 +00:00
|
|
|
|
2019-03-12 05:52:43 +00:00
|
|
|
/**
|
|
|
|
* Ensures that the file exists.
|
2019-06-19 04:22:01 +00: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 18:36:13 +00:00
|
|
|
* Requires the `--allow-read` and `--allow-write` flag.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* import { ensureFile } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
|
|
|
*
|
|
|
|
* ensureFile("./folder/targetFile.dat"); // returns promise
|
|
|
|
* ```
|
2019-03-12 05:52:43 +00:00
|
|
|
*/
|
2023-12-05 08:52:56 +00:00
|
|
|
export async function ensureFile(filePath: string | URL): Promise<void> {
|
2019-03-12 05:52:43 +00:00
|
|
|
try {
|
|
|
|
// if file exists
|
2020-06-12 19:23:38 +00:00
|
|
|
const stat = await Deno.lstat(filePath);
|
2020-04-16 05:40:30 +00:00
|
|
|
if (!stat.isFile) {
|
2019-04-07 01:01:23 +00:00
|
|
|
throw new Error(
|
2020-07-14 19:24:17 +00:00
|
|
|
`Ensure path exists, expected 'file', got '${getFileInfoType(stat)}'`,
|
2019-04-07 01:01:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2019-03-12 05:52:43 +00:00
|
|
|
// if file not exists
|
2020-02-24 20:48:35 +00:00
|
|
|
if (err instanceof Deno.errors.NotFound) {
|
2019-12-18 14:45:19 +00:00
|
|
|
// ensure dir exists
|
2023-08-14 07:08:42 +00:00
|
|
|
await ensureDir(dirname(toPathString(filePath)));
|
2019-12-18 14:45:19 +00:00
|
|
|
// create file
|
2020-06-12 19:23:38 +00:00
|
|
|
await Deno.writeFile(filePath, new Uint8Array());
|
2019-12-18 14:45:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
2019-03-12 05:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures that the file exists.
|
2019-06-19 04:22:01 +00: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 18:36:13 +00:00
|
|
|
* Requires the `--allow-read` and `--allow-write` flag.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* import { ensureFileSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
|
|
|
*
|
|
|
|
* ensureFileSync("./folder/targetFile.dat"); // void
|
|
|
|
* ```
|
2019-03-12 05:52:43 +00:00
|
|
|
*/
|
2023-12-05 08:52:56 +00:00
|
|
|
export function ensureFileSync(filePath: string | URL): void {
|
2019-03-12 05:52:43 +00:00
|
|
|
try {
|
|
|
|
// if file exists
|
2020-06-12 19:23:38 +00:00
|
|
|
const stat = Deno.lstatSync(filePath);
|
2020-04-16 05:40:30 +00:00
|
|
|
if (!stat.isFile) {
|
2019-04-07 01:01:23 +00:00
|
|
|
throw new Error(
|
2020-07-14 19:24:17 +00:00
|
|
|
`Ensure path exists, expected 'file', got '${getFileInfoType(stat)}'`,
|
2019-04-07 01:01:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2019-03-12 05:52:43 +00:00
|
|
|
// if file not exists
|
2020-02-24 20:48:35 +00:00
|
|
|
if (err instanceof Deno.errors.NotFound) {
|
2019-12-18 14:45:19 +00:00
|
|
|
// ensure dir exists
|
2023-08-14 07:08:42 +00:00
|
|
|
ensureDirSync(dirname(toPathString(filePath)));
|
2019-12-18 14:45:19 +00:00
|
|
|
// create file
|
2020-06-12 19:23:38 +00:00
|
|
|
Deno.writeFileSync(filePath, new Uint8Array());
|
2019-12-18 14:45:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw err;
|
2019-03-12 05:52:43 +00:00
|
|
|
}
|
|
|
|
}
|