-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
385 lines (338 loc) · 12.8 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/**
* (C) Copyright 2017 o2r-project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const checker = require('./lib/checker');
const debug = require('debug')('index:requestHandling');
const debugERROR = require('debug')('index:ERROR');
require('colors');
const sizeof = require('object-sizeof');
const fs = require('fs');
const os = require('os');
const path = require('path');
const globWithXignore = require('./lib/glob-with-X_ignore').globWithXignore;
function Metadata(dateStart, comparisonSet, error) {
this.checkSuccessful = false;
this.start = dateStart;
this.end = Date.now();
this.comparisonSet = comparisonSet;
this.images = [];
this.display = {};
this.errors = new Array(0);
if (error != null && error != undefined) {
this.errors.push(error.toString());
this.checkSuccessful = false;
}
}
/**
* Config Object
*
* var checkConfig = {
* directoryMode: Boolean, // read papers from directory and subdirectories automatically? (false: paths for both papers MUST be specified
* pathToMainDirectory: String,
* pathToOriginalHTML: String,
* pathToReproducedHTML: String,
* saveFilesOutputPath: String, // necessary if diff-HTML or check metadata should be saved
* saveDiffHTML: Boolean,
* outFileName: String // default: "diffHTML.html"
* saveMetadataJSON: Boolean,
* createParentDirectories: Boolean, // If outputPath does not yet exist, this flag MUST be set true; otherwise, the check fails
* comparisonSetBaseDir: String
* checkFileTypes: Array // case insensitive list of file endings to be included in Check File List
* quiet: Boolean
* };
*/
/**
* node module: function ercChecker returns Promise
*/
function ercChecker(config) {
let directoryMode = config.directoryMode,
pathToMainDirectory = config.pathToMainDirectory,
outputPath = config.saveFilesOutputPath,
saveDiffHTML = config.saveDiffHTML,
saveMetadataJSON = config.saveMetadataJSON,
outFileName = config.outFileName || "diffHTML.html",
createParentDirectories = config.createParentDirectories,
checkFileTypes = config.checkFileTypes || ['html', 'htm'],
comparisonSetBaseDir = config.comparisonSetBaseDir || ".",
quiet = config.quiet;
if (quiet) {
debug.enabled = debugERROR.enabled = false;
}
return new Promise(function (resolve, reject) {
debug("Starting check, config: %o", config);
var checkStart = Date.now(),
pathOriginalHTML, pathReproducedHTML;
if (!comparisonSetBaseDir) {
reject(new Metadata(checkStart, [], new Error("comparisonSetBaseDir must be configured")));
}
globWithXignore({
checkFileTypes: checkFileTypes,
comparisonSetBaseDir: comparisonSetBaseDir,
ignoreFile: '.ercignore',
findRoot: false
}).then(comparisonSet => {
debug("Comparison set: %o", comparisonSet);
if ((saveDiffHTML || saveMetadataJSON) && (outputPath == null || outputPath == undefined)) {
debugERROR("Cannot save files without output path.".red);
reject(new Metadata(checkStart, comparisonSet, new Error("Requested saving check files without specifying an output path.")));
}
// if output data requested, try define the output path, and if requested, create parent directories
if (saveMetadataJSON || saveDiffHTML) {
var fileOutputPath;
try {
fileOutputPath = fs.realpathSync(outputPath);
}
catch (e) {
if (createParentDirectories) {
let delimiter = (os.platform() == 'win32') ? "\\" : "/";
if (!path.isAbsolute(outputPath)) {
outputPath = path.join(process.cwd(), outputPath);
}
var dirs = outputPath.replace(/\\/g, '/').split('/');
let pathString = delimiter;
try {
dirs.forEach(function (current, index) {
pathString = path.join(pathString, current);
let err;
try {
fs.accessSync(pathString);
}
catch (e) {
err = e;
}
if (err) fs.mkdirSync(pathString);
});
}
catch (e) {
debugERROR(e);
reject(new Metadata(checkStart, comparisonSet, "Error creating output directory: " + e))
}
fileOutputPath = fs.realpathSync(pathString);
}
else {
debugERROR("Absolute output path could not be determined".red, e);
reject(new Metadata(checkStart, comparisonSet, "Absolute output path could not be determined. " + e))
}
}
}
/*
* DIRECTORY MODE
*/
if (directoryMode) {
if (pathToMainDirectory == null || pathToMainDirectory == undefined) {
prematureRejection(new Metadata(checkStart, comparisonSet, "ERROR: directory mode chosen, but no directory specified."), reject);
}
try {
fs.accessSync(pathToMainDirectory);
}
catch (e) {
debugERROR("Error reading input directory: " + e);
prematureRejection(new Metadata(checkStart, comparisonSet, e), reject);
}
let absoluteMainDirPath = fs.realpathSync(pathToMainDirectory);
let absoluteOriginalDirPath = path.join(absoluteMainDirPath, "original");
let absoluteReproducedDirPath = path.join(absoluteMainDirPath, "reproduced");
let originalDirContent, reproducedDirContent;
try {
originalDirContent = fs.readdirSync(absoluteOriginalDirPath);
reproducedDirContent = fs.readdirSync(absoluteReproducedDirPath);
if (originalDirContent.length == 0) {
debugERROR("Error: no paper found in directory " + absoluteOriginalDirPath);
prematureRejection(new Metadata(checkStart, comparisonSet, "Error: no paper found in directory " + absoluteOriginalDirPath), reject);
}
if (reproducedDirContent.length == 0) {
debugERROR("Error: no paper found in directory " + absoluteReproducedDirPath);
prematureRejection(new Metadata(checkStart, comparisonSet, "Error: no paper found in directory " + absoluteReproducedDirPath), reject);
}
}
catch (e) {
debugERROR("Could not read directory: %s", e)
prematureRejection(new Promise(checkStart, e), reject);
}
for (let file in originalDirContent) {
if (originalDirContent[file].includes(".html")) {
pathOriginalHTML = path.join(absoluteOriginalDirPath, originalDirContent[file]);
break;
}
if (file == originalDirContent.length - 1) {
prematureRejection(new Metadata(checkStart, comparisonSet, "Error: provided original paper directory does not contain an .html file."), reject);
}
}
for (let file in reproducedDirContent) {
if (reproducedDirContent[file].includes(".html")) {
pathReproducedHTML = path.join(absoluteReproducedDirPath, reproducedDirContent[file]);
break;
}
if (file == reproducedDirContent.length - 1) {
prematureRejection(new Metadata(checkStart, comparisonSet, "Error: provided reproduced paper directory does not contain an .html file."), reject);
}
}
}
/*
* '2-FILE-PATH' MODE
*/
else {
pathOriginalHTML = config.pathToOriginalHTML,
pathReproducedHTML = config.pathToReproducedHTML;
try {
if (!path.isAbsolute(pathOriginalHTML)) {
pathOriginalHTML = path.join(process.cwd(), config.comparisonSetBaseDir, pathOriginalHTML);
}
}
catch (e) {
debugERROR("The path to your Original HTML file is invalid. Please check if the file exists.".red, e.message);
prematureRejection(new Metadata(checkStart, comparisonSet, e), reject);
}
try {
if (!path.isAbsolute(pathReproducedHTML)) {
pathReproducedHTML = path.join(process.cwd(), config.comparisonSetBaseDir, pathReproducedHTML);
}
}
catch (e) {
debugERROR("The path to your Reproduced HTML file is invalid. Please check if the file exists.".red, e.message);
prematureRejection(new Metadata(checkStart, comparisonSet, e), reject);
}
}
function prematureRejection(rejectionMetadata, rejectFunction) {
if (saveMetadataJSON) {
writeOutputFiles(rejectionMetadata, outputPath, null, saveMetadataJSON)
}
rejectFunction(rejectionMetadata);
}
try {
var originalHTMLBuffer = fs.readFileSync(pathOriginalHTML);
var reproducedHTMLBuffer = fs.readFileSync(pathReproducedHTML);
}
catch (e) {
debugERROR("Failed to read HTML file.".red);
debugERROR(e);
prematureRejection(new Metadata(checkStart, comparisonSet, "wrong path here: " + e), reject);
}
if (originalHTMLBuffer.equals(reproducedHTMLBuffer)) {
debug('The compared files, ' + pathOriginalHTML + ' and ' + pathReproducedHTML + ' do not differ.'.green + '\n' + 'Congrats!'.green);
let resolveMetadataEqualPapers = new Metadata(checkStart, comparisonSet);
resolveMetadataEqualPapers.checkSuccessful = true;
resolve(resolveMetadataEqualPapers);
}
else {
debug("Differences were found; Calling compareHTML to create a HTML file highlighting these differences.");
checker
.compareHTML(pathOriginalHTML, pathReproducedHTML, quiet, checkStart)
.then(
function (result) {
debug('Check done'.green);
let resultMetadata = result;
resultMetadata.end = Date.now();
resultMetadata.comparisonSet = comparisonSet;
if (!process.env.DEV && resultMetadata.images.length != 0) {
try {
let tmpDirForDiffImages = resultMetadata.tmpPath;
deleteFolderRecursive(tmpDirForDiffImages);
delete resultMetadata.tmpPath;
} catch (e) {
debugERROR(e)
}
}
try {
if (saveMetadataJSON) {
outputFile = path.join(fileOutputPath, 'metadata.json')
fs.writeFileSync(outputFile, JSON.stringify(resultMetadata, null, 4));
debug("Metadata JSON file written successfully to ".green, outputFile);
}
if (saveDiffHTML) {
outputFile = path.join(fileOutputPath, outFileName)
fs.writeFileSync(outputFile, resultMetadata.display.diff);
debug("Diff-HTML file written successfully to %s".green, outputFile);
}
}
catch (e) {
debugERROR("Failed to write output file.".red);
resultMetadata.errors.push(e);
debugERROR(e);
reject(resultMetadata);
}
debug("Finished! Returning object of size %s", sizeof(resultMetadata));
resolve(resultMetadata);
},
function (rejectData) {
debug('Check failed'.red);
let reason = rejectData[0];
let tmpPath = rejectData[1];
debugERROR(reason);
let rejectMetadata = new Metadata(checkStart, comparisonSet, reason);
if (!process.env.DEV && tmpPath) {
try {
deleteFolderRecursive(tmpPath);
} catch (e) {
debugERROR("Failed to delete temp path: " + e);
rejectMetadata.errors.push(e);
}
} else {
rejectMetadata.tmpPath = tmpPath;
}
if (saveMetadataJSON) {
try {
fs.writeFileSync(path.join(fileOutputPath, 'metadata.json'), JSON.stringify(rejectMetadata, null, 4));
}
catch (e) {
rejectMetadata.errors.push(e);
debugERROR("Failure writing metadata.json file:", e);
}
}
debug("Finished! Returning object of size %s", sizeof(rejectMetadata));
reject(rejectMetadata);
}
);
}
}).catch(reason => {
debugERROR(reason);
reject(new Metadata(checkStart, comparisonSet, new Error("Failed to get Glob with .ercignore: ", reason)));
});
});
}
var writeOutputFiles = function (data, outputPath, saveDiffHTML, saveMetadataJSON) {
if (saveMetadataJSON) {
fs.writeFileSync(path.join(outputPath, 'metadata.json'), JSON.stringify(data, null, 4));
}
if (saveDiffHTML) {
fs.writeFileSync(path.join(outputPath, "diffHTML.html"), data.display.diff);
debugGeneral("Output Diff-HTML file written successfully".green);
}
};
var deleteFolderRecursive = function (pathParam) {
let tmpDirPath = pathParam;
try {
if (fs.existsSync(tmpDirPath)) {
fs.readdirSync(tmpDirPath).forEach(function (file, index) {
let curPath = path.join(tmpDirPath, file);
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(tmpDirPath);
}
} catch (e) {
debugERROR(e);
}
};
module.exports = {
// ercChecker returns a Promise
// in case of any Error, the Promise is REJECTED, and contains a Metadata Object with Timestamps and Error Description
// otherwise, the Promise is RESOLVED and contains a Metadata Object. In case of Differences, it also contains a Diff-HTML String.
ercChecker: ercChecker
};