-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
136 lines (107 loc) · 3.1 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env node
/*!
* Copyright (c) 2020 Daniel Duarte <[email protected]>
* Licensed under MIT License. See LICENSE file for details.
*/
const fs = require('fs');
const path = require('path');
const parser = require('@tandil/diffparse');
const toIndexFilename = (filename, index) => {
const fileParts = path.parse(filename);
return path.join(fileParts.dir, `${fileParts.name}_${index.toString().padStart(4, '0')}${fileParts.ext}`);
};
const getFreeFilename = filename => {
if (!fs.existsSync(filename)) {
return filename;
}
const fileParts = path.parse(filename);
let index = 1;
let newFilename;
do {
newFilename = path.join(fileParts.dir, `${fileParts.name}-${index++}${fileParts.ext}`);
} while (fs.existsSync(newFilename));
return newFilename;
};
const generateOutput = (diff, diffFile, { output }) => {
// If there is only one file (or zero), nothing is generated
if (diff.files.length <= 1) {
return [`Diff have ${diff.files.length === 1 ? '1 file' : '0 files'}, so no split was done`];
}
for (let i = 0; i < diff.files.length; i++) {
const f = diff.files[i];
let lines = [];
const write = line => {
lines.push(line);
};
f.header && write(f.header);
f.fileMode && write(f.fileMode);
f.oldMode && write(f.oldMode);
f.newMode && write(f.newMode);
f.index && write(f.index);
f.oldFile && write(f.oldFile);
f.newFile && write(f.newFile);
for (const ch of f.chunks) {
write(ch.header);
for (const l of ch.content) {
write(l);
}
}
const filename = getFreeFilename(toIndexFilename(diffFile, i + 1));
const content = lines.join('\n') + '\n';
fs.writeFileSync(filename, content);
}
return [`Split in ${diff.files.length} files`];
};
const split = opts => {
opts = Object.assign({
files: [],
output: 'write',
}, opts);
opts.files.forEach(f => {
console.log(`Processing file: "${f}"`);
if (!fs.existsSync(f)) {
console.log(' Errors:');
console.log(' File does not exist');
console.log();
return;
}
const diff = parser.parseDiffFileSync(f);
const errors = diff.errors;
if (errors.length === 0) {
const result = generateOutput(diff, f, { output: opts.output });
console.log(' OK' + (result.length === 0 ? '' : ':'));
console.log(' ' + result.join('\n '));
} else {
console.log(` Errors:`);
console.log(' ' + errors.join('\n '));
}
console.log();
});
};
const getArgs = () => {
const cliArgs = process.argv.slice(2);
const args = {
files: [],
};
const paramRE = /^--([a-zA-Z0-9_-]+)=(.*)$/;
const flagRE = /^--([a-zA-Z0-9_-]+)$/;
for (const arg of cliArgs) {
if (arg.startsWith('--')) {
let matches = paramRE.exec(arg);
if (matches !== null) {
args[matches[1]] = matches[2];
} else {
matches = flagRE.exec(arg);
if (matches !== null) {
args[matches[1]] = true;
}
}
} else {
args.files.push(arg);
}
}
return args;
};
const args = getArgs();
split(args);
module.exports = { split };