-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcp.js
More file actions
32 lines (27 loc) · 812 Bytes
/
cp.js
File metadata and controls
32 lines (27 loc) · 812 Bytes
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
const fs = require('fs');
const path = require('path');
const USAGE = "Usage: node cp.js [SOURCE_FILES]... [DESTINATION_DIR]\n";
const args = process.argv.slice(2);
if (args.length < 2) {
console.error(USAGE);
throw new SyntaxError();
}
const src = args.slice(0, -1);
const dest = args[args.length - 1]
if (!fs.existsSync(dest)) {
console.error(`${dest} no such file or directory`)
}
if (!fs.statSync(dest).isDirectory()) {
console.error(`${dest} is not a directory`);
throw new TypeError();
}
for (const f of src) {
if (!fs.existsSync(f)) {
console.error(`${f} no such file or directory`)
}
if (!fs.statSync(f).isFile()) {
console.error(`${f} is not a file`);
throw new TypeError();
}
fs.copyFileSync(f, path.join(dest, path.basename(f)));
}