-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcopyFile.js
35 lines (25 loc) · 834 Bytes
/
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
const { copyFile } = require('fs/promises');
const { join } = require('path');
async function copyAFile(from, to) {
try {
await copyFile(from, to);
console.log(`Copied ${from} to ${to}`);
} catch (err) {
console.error(`Got an error trying to copy the file: ${err.message}`);
}
}
async function copyAll(fromDir, toDir, filePaths) {
return Promise.all(filePaths.map(filePath => {
return copyAFile(join(fromDir, filePath), join(toDir, filePath));
}));
}
async function copyFiles(fromDir, toDir, filePaths) {
try {
await copyAll(fromDir, toDir, filePaths);
console.log('Copied all files');
} catch(err) {
console.error(`Got an error trying to copy the files: ${err.message}`);
}
}
// copyFile('friends.txt', 'friends-copy.txt');
copyFiles('from', 'to', ['copyA.txt', 'copyB.txt']);