-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathuniq.index.ts
70 lines (61 loc) · 1.49 KB
/
uniq.index.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import fs from 'fs';
import { uniq } from './uniq';
async function main() {
let inFile: string | undefined = undefined;
let outFile: string | undefined = undefined;
const inStream = process.stdin;
const outStream = process.stdout;
let count = false;
let repeated = false;
let unique = false;
for (let i = 2; i < process.argv.length; i++) {
const arg = process.argv[i];
// Check for -c, -d
if (arg === '-c' || arg === '--count') {
count = true;
continue;
} else if (arg === '-d' || arg === '--repeated') {
repeated = true;
continue;
} else if (arg === '-u') {
unique = true;
continue;
}
// If no input file is provided
if (arg === '-') {
inFile = undefined;
// Check for output file
if (i < process.argv.length - 1) {
outFile = process.argv[i + 1];
}
break;
}
// Case when input file is provided
inFile = arg;
// Check if output file is provided
if (i < process.argv.length - 1) {
outFile = process.argv[i + 1];
}
break;
}
if (inFile !== undefined) {
if (!fs.existsSync(inFile)) {
console.error('File does not exists');
process.exit(1);
}
}
const output = await uniq({
path: inFile,
inStream: inStream,
count: count,
repeated: repeated,
unique: unique
});
if (outFile !== undefined) {
fs.writeFileSync(outFile, output);
process.exit(0);
}
outStream.write(output);
process.exit(0);
}
main();