-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathlink.ts
More file actions
46 lines (42 loc) · 1.21 KB
/
link.ts
File metadata and controls
46 lines (42 loc) · 1.21 KB
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
import {
copyFileSync,
linkSync,
mkdirSync,
readdirSync,
statSync,
symlinkSync,
} from 'fs';
import { dirname, join } from 'path';
/**
* If `node` is started with `--preserve-symlinks`, the module loaded will
* preserve symbolic links instead of resolving them, making it possible to
* symbolically link packages in place instead of fully copying them.
*/
const PRESERVE_SYMLINKS = process.execArgv.includes('--preserve-symlinks');
/**
* Creates directories containing hard links if possible, and falls back on
* copy otherwise.
*
* @param existing is the original file or directory to link.
* @param destination is the new file or directory to create.
*/
export function link(existing: string, destination: string): void {
if (PRESERVE_SYMLINKS) {
mkdirSync(dirname(destination), { recursive: true });
symlinkSync(existing, destination);
return;
}
const stat = statSync(existing);
if (!stat.isDirectory()) {
try {
linkSync(existing, destination);
} catch {
copyFileSync(existing, destination);
}
return;
}
mkdirSync(destination, { recursive: true });
for (const file of readdirSync(existing)) {
link(join(existing, file), join(destination, file));
}
}