-
Notifications
You must be signed in to change notification settings - Fork 0
/
step3.js
54 lines (47 loc) · 1.04 KB
/
step3.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
const fs = require('fs');
const process = require('process');
const axios = require('axios');
function handleOutput(text, out) {
if (out) {
fs.writeFile(out, text, 'utf8', function(err) {
if (err) {
console.error(`Couldn't write ${out}: ${err}`);
process.exit(1);
}
});
} else {
console.log(text);
}
}
function cat(path, out) {
fs.readFile(path, 'utf8', function(err, data) {
if (err) {
console.error(`Error reading ${path}: ${err}`);
process.exit(1);
} else {
handleOutput(data, out);
}
});
}
async function webCat(url, out) {
try {
let resp = await axios.get(url);
handleOutput(resp.data, out);
} catch (err) {
console.error(`Error fetching ${url}: ${err}`);
process.exit(1);
}
}
let path;
let out;
if (process.argv[2] === '--out') {
out = process.argv[3];
path = process.argv[4];
} else {
path = process.argv[2];
}
if (path.slice(0, 4) === 'http') {
webCat(path, out);
} else {
cat(path, out);
}