-
Notifications
You must be signed in to change notification settings - Fork 584
/
ensure_link.ts
46 lines (42 loc) · 1.43 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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import * as path from "../path/mod.ts";
import { ensureDir, ensureDirSync } from "./ensure_dir.ts";
import { toPathString } from "./_util.ts";
/**
* Ensures that the hard link exists.
* If the directory structure does not exist, it is created.
*
* @example
* ```ts
* import { ensureSymlink } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
*
* ensureSymlink("./folder/targetFile.dat", "./folder/targetFile.link.dat"); // returns promise
* ```
*
* @param src the source file path. Directory hard links are not allowed.
* @param dest the destination link path
*/
export async function ensureLink(src: string | URL, dest: string | URL) {
dest = toPathString(dest);
await ensureDir(path.dirname(dest));
await Deno.link(toPathString(src), dest);
}
/**
* Ensures that the hard link exists.
* If the directory structure does not exist, it is created.
*
* @example
* ```ts
* import { ensureSymlinkSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
*
* ensureSymlinkSync("./folder/targetFile.dat", "./folder/targetFile.link.dat"); // void
* ```
*
* @param src the source file path. Directory hard links are not allowed.
* @param dest the destination link path
*/
export function ensureLinkSync(src: string | URL, dest: string | URL) {
dest = toPathString(dest);
ensureDirSync(path.dirname(dest));
Deno.linkSync(toPathString(src), dest);
}