-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs-write-worker-copy.js
67 lines (61 loc) · 1.71 KB
/
fs-write-worker-copy.js
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
59
60
61
62
63
64
65
66
67
// @flow
import { ncp } from "graceful-ncp";
import progress from "progress-stream";
import getSize from "get-folder-size";
import fs from "mz/fs";
import * as c from "../fs-write-constants";
import * as t from "../fs-write-actiontypes";
import type { WriteStream, ReadStream } from "fs";
import type { SubTask, ProgressReport } from "../fs-write-types";
export default function copy(
subTask: SubTask,
handleProgress: (progress: ProgressReport) => void
): Promise<*> {
var fullSize = 1;
var progressPerFileStack = [];
var calcProgress = () => {
// Combine each progress to a full value
var fullProgress = 0;
progressPerFileStack.forEach(bytes => {
fullProgress = fullProgress + bytes;
});
handleProgress({
destination: subTask.destination,
percentage: Math.round(fullProgress / fullSize * 100)
});
};
var ncpOptions = {
stopOnErr: true,
clobber: subTask.clobber,
limit: 16,
transform: (read: ReadStream, write: WriteStream) => {
// Insert Progress report in ncp stream
// For every file in ncp
var id = progressPerFileStack.length;
progressPerFileStack[id] = 0;
var str = progress({
time: 100
}).on("progress", progress => {
progressPerFileStack[id] = progress.transferred;
calcProgress();
});
read.pipe(str).pipe(write);
}
};
return new Promise((resolve, reject) => {
getSize(subTask.source, (err, size) => {
if (err) {
reject(err);
return;
}
fullSize = size;
ncp(subTask.source, subTask.destination, ncpOptions, errList => {
if (errList) {
reject(errList[0]);
return;
}
resolve();
});
});
});
}