/
ensure_link.ts
50 lines (46 loc) · 1.66 KB
/
ensure_link.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { dirname } from "../path/dirname.ts";
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
import { toPathString } from "./_to_path_string.ts";
/**
* 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.
*
* @example
* ```ts
* import { ensureLink } from "https://deno.land/std@$STD_VERSION/fs/ensure_link.ts";
*
* await ensureLink("./folder/targetFile.dat", "./folder/targetFile.link.dat");
* ```
*/
export async function ensureLink(src: string | URL, dest: string | URL) {
dest = toPathString(dest);
await ensureDir(dirname(dest));
await Deno.link(toPathString(src), dest);
}
/**
* 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.
*
* @example
* ```ts
* import { ensureLinkSync } from "https://deno.land/std@$STD_VERSION/fs/ensure_link.ts";
*
* ensureLinkSync("./folder/targetFile.dat", "./folder/targetFile.link.dat");
* ```
*/
export function ensureLinkSync(src: string | URL, dest: string | URL) {
dest = toPathString(dest);
ensureDirSync(dirname(dest));
Deno.linkSync(toPathString(src), dest);
}