forked from davidflanagan/jstdg7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyfile.js
46 lines (42 loc) · 1.94 KB
/
copyfile.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
const fs = require("fs");
// A streaming file copy function, using "flowing mode".
// Copies the contents of the named source file to the named destination file.
// On success, invokes the callback with a null argument. On error,
// invokes the callback with an Error object.
function copyFile(sourceFilename, destinationFilename, callback) {
let input = fs.createReadStream(sourceFilename);
let output = fs.createWriteStream(destinationFilename);
input.on("data", (chunk) => { // When we get new data,
let hasRoom = output.write(chunk); // write it to the output stream.
if (!hasRoom) { // If the output stream is full
input.pause(); // then pause the input stream.
}
});
input.on("end", () => { // When we reach the end of input,
output.end(); // tell the output stream to end.
});
input.on("error", err => { // If we get an error on the input,
callback(err); // call the callback with the error
process.exit(); // and quit.
});
output.on("drain", () => { // When the output is no longer full,
input.resume(); // resume data events on the input
});
output.on("error", err => { // If we get an error on the output,
callback(err); // call the callback with the error
process.exit(); // and quit.
});
output.on("finish", () => { // When output is fully written
callback(null); // call the callback with no error.
});
}
// Here's a simple command-line utility to copy files
let from = process.argv[2], to = process.argv[3];
console.log(`Copying file ${from} to ${to}...`);
copyFile(from, to, err => {
if (err) {
console.error(err);
} else {
console.log("done.");
}
});