-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·86 lines (74 loc) · 2.4 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
const path = require('path');
const lineByLine = require('n-readlines');
const args = require('minimist')(process.argv.slice(2), {
boolean: ['help', 'version'],
string: ['file1', 'file2'],
alias: { h: 'help', v: 'version' },
});
const BASE_PATH = path.resolve(process.env.BASE_PATH || __dirname);
if (args.help) {
printHelp();
} else if (args._.includes('byline')) {
const { file1, file2 } = args;
if (file1 == undefined || file2 == undefined) {
printByLineHelp();
} else {
try {
const file1path = path.join(BASE_PATH || '', file1);
const file2path = path.join(BASE_PATH || '', file2);
const line1 = new lineByLine(file1path);
const line2 = new lineByLine(file2path);
let i = 1;
let l1, l2;
while ((l1 = line1.next()) && (l2 = line2.next())) {
if (l1.toString() != l2.toString()) {
console.log(`${i}: file1: ${l1} | file2: ${l2}`);
}
i++;
}
while ((l1 = line1.next())) {
console.log(`${i}: file1: ${l1} | file2:`);
l1 = line1.next();
i++;
}
while ((l2 = line2.next())) {
console.log('46j');
console.log(`${i}: file1: | file2: ${l2}`);
l2 = line2.next();
i++;
}
} catch (err) {
console.log(err.toString());
}
}
} else if (args.version) {
console.log(require('./package').version);
} else {
handleUnknownCommand();
}
function printHelp() {
console.log('filediffer: A command line tool to compare files\n');
console.log('Usage: filediffer [options]\n');
console.log('Options:\n');
console.log('-v, --version output the version number');
console.log('-h, --help show help');
console.log('<command> -h description of that command');
console.log('byline logs unmatching lines side by side');
}
function handleUnknownCommand() {
console.log('command not found\n');
printHelp();
}
function printByLineHelp() {
console.log('filediffer\n');
console.log('byline help\n');
console.log('description\n');
console.log('logs unmatching lines side by side');
console.log('options:\n');
console.log(
'--file1 Path of first file. Can pass relative path if BASE_PATH is added in the environment'
);
console.log('--file2 Path of second file');
}
// node ./index.js --diff --file1=out.txt --file2=noutput.txt > output.txt