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-04-22 15:35:14 +00:00
|
|
|
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
|
2023-12-21 07:39:51 +00:00
|
|
|
import { toPathString } from "./_to_path_string.ts";
|
2019-04-22 15:35:14 +00:00
|
|
|
|
|
|
|
/**
|
2024-03-27 06:28:06 +00:00
|
|
|
* Asynchronously ensures that the hard link exists. If the directory structure
|
|
|
|
* does not exist, it is created.
|
|
|
|
*
|
|
|
|
* @param src The source file path as a string or URL. Directory hard links are
|
|
|
|
* not allowed.
|
|
|
|
* @param dest The destination link path as a string or URL.
|
|
|
|
* @returns A void promise that resolves once the hard link exists.
|
2019-04-22 15:35:14 +00:00
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* @example
|
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { ensureLink } from "@std/fs/ensure-link";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* await ensureLink("./folder/targetFile.dat", "./folder/targetFile.link.dat");
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```
|
2019-04-22 15:35:14 +00:00
|
|
|
*/
|
2022-08-30 03:58:48 +00:00
|
|
|
export async function ensureLink(src: string | URL, dest: string | URL) {
|
|
|
|
dest = toPathString(dest);
|
2023-08-14 07:08:42 +00:00
|
|
|
await ensureDir(dirname(dest));
|
2019-04-22 15:35:14 +00:00
|
|
|
|
2022-08-30 03:58:48 +00:00
|
|
|
await Deno.link(toPathString(src), dest);
|
2019-04-22 15:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-03-27 06:28:06 +00:00
|
|
|
* Synchronously ensures that the hard link exists. If the directory structure
|
|
|
|
* does not exist, it is created.
|
|
|
|
*
|
|
|
|
* @param src The source file path as a string or URL. Directory hard links are
|
|
|
|
* not allowed.
|
|
|
|
* @param dest The destination link path as a string or URL.
|
|
|
|
* @returns A void value that returns once the hard link exists.
|
2019-04-22 15:35:14 +00:00
|
|
|
*
|
2022-11-25 11:40:23 +00:00
|
|
|
* @example
|
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { ensureLinkSync } from "@std/fs/ensure-link";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* ensureLinkSync("./folder/targetFile.dat", "./folder/targetFile.link.dat");
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```
|
2019-04-22 15:35:14 +00:00
|
|
|
*/
|
2022-08-30 03:58:48 +00:00
|
|
|
export function ensureLinkSync(src: string | URL, dest: string | URL) {
|
|
|
|
dest = toPathString(dest);
|
2023-08-14 07:08:42 +00:00
|
|
|
ensureDirSync(dirname(dest));
|
2019-04-22 15:35:14 +00:00
|
|
|
|
2022-08-30 03:58:48 +00:00
|
|
|
Deno.linkSync(toPathString(src), dest);
|
2019-04-22 15:35:14 +00:00
|
|
|
}
|