-
Notifications
You must be signed in to change notification settings - Fork 19
/
generate-changelog.js
260 lines (219 loc) · 8.74 KB
/
generate-changelog.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the W3C Software Notice and
* Document License (2015-05-13) which is available at
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document.
*
* SPDX-License-Identifier: EPL-2.0 OR W3C-20150513
*/
/** ========================================================================
* Includes and Globals
*======================================================================== **/
// JSON to CSV and vice versa libraries
const Json2CsvParser = require("json2csv").Parser;
const csvjson = require("csvjson");
const { readFileSync, writeFileSync, existsSync, fstat } = require("fs");
const path = require("path");
const CHANGE_TYPES = ["added", "removed", "renamed", "line-change", "description"];
let oldCsvTable;
let newCsvTable;
let oldCsvPath;
let newCsvPath;
let outputPath;
class Change {
constructor(assertionID, changeType, additionalParam) {
this.assertionID = assertionID;
this.changeType = changeType;
this.additionalParam = additionalParam;
}
toString() {
switch (this.changeType) {
case "added":
return `- \`${this.assertionID}\` was added`;
case "removed":
return `- \`${this.assertionID}\` was removed`;
case "renamed":
return `- \`${this.additionalParam}\` was renamed to \`${this.assertionID}\``;
case "line-change":
return (
`- \`${this.assertionID}\` was moved from Line ` +
`${this.additionalParam.oldline + 1} to ${this.additionalParam.newline + 1}`
);
case "description":
return `- \`${this.assertionID}\` -> \`"${this.additionalParam}"\``;
}
}
}
class Changelog {
constructor() {
this.log = {};
this.logString = "";
this.numberOfChanges = 0;
}
addLog(assertionID, changeType, additionalParam) {
if (!this.log[changeType]) this.log[changeType] = [];
this.log[changeType].push(new Change(assertionID, changeType, additionalParam));
this.numberOfChanges++;
}
getLogMarkDownString() {
this.logString = `
# CSV Changelog - ${new Date().toLocaleDateString("en-GB")}
[Old CSV Path](${oldCsvPath})
[New CSV Path](${newCsvPath})
`;
if (this.numberOfChanges === 0) {
this.logString += `\nThere are no changes between both files\n`;
return this.logString;
}
for (const changeType of CHANGE_TYPES) {
if (this.log[changeType]) {
this.logString += `
## ${changeType.toUpperCase()}
`;
for (const change of this.log[changeType]) {
this.logString += `${change.toString()}\n`;
}
}
}
return this.logString;
}
printLogToConsole() {
console.log(this.getLogMarkDownString());
}
containsCriticalChange() {
return false;
}
}
const changeLogs = new Changelog();
/** ========================================================================
* Command line Interface
*========================================================================**/
const helpMessage = `
Usage: node generate-changelog.js <old CSV path> <new CSV path> [output path]
Output path is optional. If not specified, the Markdown will be printed to the terminal instead.
node generate-changelog.js [-h|--help] displays this message.
`;
const myArgs = process.argv.slice(2);
if (myArgs.length <= 1 || myArgs.length > 3) {
console.log(helpMessage);
return -1;
} else {
oldCsvPath = myArgs[0].trim();
newCsvPath = myArgs[1].trim();
if (myArgs[2]) outputPath = myArgs[2].trim();
/* Normalizing paths */
oldCsvPath = oldCsvPath.split(path.win32.sep);
if (oldCsvPath.length === 1) {
oldCsvPath = oldCsvPath[0].split(path.posix.sep);
}
oldCsvPath = path.join(...oldCsvPath);
newCsvPath = newCsvPath.split(path.win32.sep);
if (newCsvPath.length === 1) {
newCsvPath = newCsvPath[0].split(path.posix.sep);
}
newCsvPath = path.join(...newCsvPath);
if (outputPath) {
outputPath = outputPath.split(path.win32.sep);
if (outputPath.length === 1) {
outputPath = outputPath[0].split(path.posix.sep);
}
outputPath = path.join(...outputPath);
}
/* Check paths exist*/
if (!existsSync(oldCsvPath)) throw new Error("Given path for 'oldCsvPath' does not exist");
if (!existsSync(newCsvPath)) throw new Error("Given path for 'newCsvPath' does not exist");
/* Check paths are csvs*/
if (path.extname(oldCsvPath) !== ".csv")
throw new Error(
"The path 'oldCsvPath' does not point to a csv file. Make sure that the file has the extension name '.csv'"
);
if (path.extname(newCsvPath) !== ".csv")
throw new Error(
"The path 'newCsvPath' does not point to a csv file. Make sure that the file has the extension name '.csv'"
);
if (outputPath && path.extname(outputPath) !== ".md") outputPath += ".md";
/** ========================================================================
* Read and Parse CSV
*========================================================================**/
const oldCsv = readFileSync(oldCsvPath, {
encoding: "utf-8",
});
const newCsv = readFileSync(newCsvPath, {
encoding: "utf-8",
});
const csvParserOptions = {
delimiter: ",", // optional
quote: '"', // optional
};
oldCsvTable = csvjson.toObject(oldCsv, csvParserOptions);
newCsvTable = csvjson.toObject(newCsv, csvParserOptions);
for (const newAssertion of newCsvTable) {
inspectNewAssertion(newAssertion);
}
for (const oldAssertion of oldCsvTable) {
inspectOldAssertion(oldAssertion);
}
if (outputPath) {
writeFileSync(outputPath, changeLogs.getLogMarkDownString());
} else {
changeLogs.printLogToConsole();
}
}
function inspectOldAssertion(oldAssertion) {
const newAssertionIndex = newCsvTable.findIndex((assertion) => {
return oldAssertion.ID === assertion.ID;
});
const notFound = newAssertionIndex === -1;
if (notFound) changeLogs.addLog(oldAssertion.ID, "removed");
}
function inspectNewAssertion(newAssertion) {
const oldAssertionIndex = oldCsvTable.findIndex((assertion) => {
return newAssertion.ID === assertion.ID;
});
const notFound = oldAssertionIndex === -1;
if (notFound) {
const sameDescriptionIndex = oldCsvTable.findIndex((assertion) => {
return newAssertion.Assertion === assertion.Assertion;
});
const sameDescriptionFound = sameDescriptionIndex !== -1;
// ! Checking on same description is not reliable for now because of different assertions with same descriptions.
// ! Code can be used once that issue is solved
if (false) {
const sameDescriptionAssertion = oldCsvTable[sameDescriptionIndex];
changeLogs.addLog(newAssertion.ID, "renamed", sameDescriptionAssertion);
} else {
changeLogs.addLog(newAssertion.ID, "added");
}
} else {
// Check if assertion was moved to a new line
const newAssertionIndex = newCsvTable.findIndex((assertion) => {
return newAssertion.ID === assertion.ID;
});
if (newAssertionIndex !== oldAssertionIndex)
changeLogs.addLog(newAssertion.ID, "line-change", {
oldline: oldAssertionIndex,
newline: newAssertionIndex,
});
// Check description changes
if (newAssertion.Assertion !== oldCsvTable[oldAssertionIndex].Assertion) {
// Assertion Descriptions are not strictly equal, but maybe the change is only in punctuation
// Remove punctuation then check again
const newDes = newAssertion.Assertion.replace(/[^\w\s\']|_/g, "")
.replace(/\s+/g, " ")
.trim();
const oldDesc = newAssertion.Assertion.replace(/[^\w\s\']|_/g, "")
.replace(/\s+/g, " ")
.trim();
// todo Should we hint at the punctuation changes? Should we fix oldCVS punctuation?
if (newDes !== oldDesc) {
changeLogs.addLog(newAssertion.ID, "description", newAssertion.Assertion);
}
}
}
}