-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathcopy.ts
58 lines (45 loc) · 1.73 KB
/
copy.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
51
52
53
54
55
56
57
58
import * as fs from 'fs';
import * as path from 'path';
import { IgnoreStrategy } from './ignore';
import { CopyOptions, SymlinkFollowMode } from './options';
import { shouldFollow } from './utils';
export function copyDirectory(srcDir: string, destDir: string, options: CopyOptions = { }, rootDir?: string) {
const follow = options.follow ?? SymlinkFollowMode.EXTERNAL;
rootDir = rootDir || srcDir;
const ignoreStrategy = IgnoreStrategy.fromCopyOptions(options, rootDir);
if (!fs.statSync(srcDir).isDirectory()) {
throw new Error(`${srcDir} is not a directory`);
}
const files = fs.readdirSync(srcDir);
for (const file of files) {
const sourceFilePath = path.join(srcDir, file);
if (ignoreStrategy.ignores(sourceFilePath)) {
continue;
}
const destFilePath = path.join(destDir, file);
let stat: fs.Stats | undefined = follow === SymlinkFollowMode.ALWAYS
? fs.statSync(sourceFilePath)
: fs.lstatSync(sourceFilePath);
if (stat && stat.isSymbolicLink()) {
const target = fs.readlinkSync(sourceFilePath);
// determine if this is an external link (i.e. the target's absolute path
// is outside of the root directory).
const targetPath = path.normalize(path.resolve(srcDir, target));
if (shouldFollow(follow, rootDir, targetPath)) {
stat = fs.statSync(sourceFilePath);
} else {
fs.symlinkSync(target, destFilePath);
stat = undefined;
}
}
if (stat && stat.isDirectory()) {
fs.mkdirSync(destFilePath);
copyDirectory(sourceFilePath, destFilePath, options, rootDir);
stat = undefined;
}
if (stat && stat.isFile()) {
fs.copyFileSync(sourceFilePath, destFilePath);
stat = undefined;
}
}
}