2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { dirname } from "@std/path/dirname";
|
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
|
|
|
/**
|
2024-06-07 03:54:50 +00:00
|
|
|
* Asynchronously ensures that the file exists.
|
2024-03-27 06:28:06 +00:00
|
|
|
*
|
2024-06-07 03:54:50 +00:00
|
|
|
* If the file already exists, this function does nothing. If the parent
|
|
|
|
* directories for the file do not exist, they are created.
|
|
|
|
*
|
|
|
|
* Requires `--allow-read` and `--allow-write` permissions.
|
|
|
|
*
|
|
|
|
* @see {@link https://docs.deno.com/runtime/manual/basics/permissions#file-system-access}
|
|
|
|
* for more information on Deno's permissions system.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* @param filePath The path of the file to ensure, as a string or URL.
|
2024-06-07 03:54:50 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* @returns A void promise that resolves once the file exists.
|
|
|
|
*
|
2024-06-07 03:54:50 +00:00
|
|
|
* @example Usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { ensureFile } from "@std/fs/ensure-file";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* await ensureFile("./folder/targetFile.dat");
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```
|
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(
|
2024-08-26 04:31:34 +00:00
|
|
|
`Failed to ensure file 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-06-07 03:54:50 +00:00
|
|
|
* Synchronously ensures that the file exists.
|
2024-03-27 06:28:06 +00:00
|
|
|
*
|
2024-06-07 03:54:50 +00:00
|
|
|
* If the file already exists, this function does nothing. If the parent
|
|
|
|
* directories for the file do not exist, they are created.
|
|
|
|
*
|
|
|
|
* Requires `--allow-read` and `--allow-write` permissions.
|
|
|
|
*
|
|
|
|
* @see {@link https://docs.deno.com/runtime/manual/basics/permissions#file-system-access}
|
|
|
|
* for more information on Deno's permissions system.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* @param filePath The path of the file to ensure, as a string or URL.
|
2024-06-07 03:54:50 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* @returns A void value that returns once the file exists.
|
|
|
|
*
|
2024-06-07 03:54:50 +00:00
|
|
|
* @example Usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { ensureFileSync } from "@std/fs/ensure-file";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* ensureFileSync("./folder/targetFile.dat");
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```
|
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(
|
2024-08-26 04:31:34 +00:00
|
|
|
`Failed to ensure file 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
|
|
|
}
|
|
|
|
}
|