-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcat.js
More file actions
53 lines (42 loc) · 1.18 KB
/
cat.js
File metadata and controls
53 lines (42 loc) · 1.18 KB
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
const fs = require('fs')
const readline = require('readline');
const os = require("os");
var args = [...process.argv]
if (args.length >= 3 && args[2].indexOf('.') > -1) {
args.shift()
args.shift();
const isCopy = args.includes('-c')
const isLine = args.includes('-l')
let lineNumber = 1
let lines = ""
const path = args[0].replace(/\\/g, "\\\\");
const rl = readline.createInterface({
input: fs.createReadStream(path, "utf8"),
output: process.stdout,
terminal: false
});
rl.on('line', (line) => {
if (isLine) {
console.log(`${lineNumber}: ${line}`)
lineNumber++;
} else {
console.log(line);
}
if (isCopy) {
const temp = line + os.EOL
lines += temp;
}
});
rl.on('close', () => {
if (isCopy) {
require('child_process').spawn('clip').stdin.end(lines);
console.log("copied...");
}
})
} else {
console.error("no path found .... ")
console.log("try cat <filename>")
console.log("-c for copy file conent")
console.log("-l to show line number")
process.exit(1)
}