forked from OpenTermsArchive/versions-cleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeclarationUtils.js
executable file
·87 lines (67 loc) · 2.78 KB
/
DeclarationUtils.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
import fs from 'fs';
import path from 'path';
import DeepDiff from 'deep-diff';
export default class DeclarationUtils {
static genericPageDeclaration = {
location: 'http://service.example',
contentSelectors: 'html',
filters: [document => {
document.querySelectorAll('a').forEach(el => {
const url = new URL(el.getAttribute('href'), document.location);
url.search = '';
el.setAttribute('href', url.toString());
});
}],
};
static pageToJSON = page => ({
fetch: page.location,
select: page.contentSelectors,
remove: page.noiseSelectors,
filter: page.filters ? page.filters.map(filter => filter.name) : undefined,
executeClientScripts: page.executeClientScripts,
});
static declarationToJSON(declaration) {
return ({
name: declaration.service.name,
documents: {
[declaration.type]: declaration.isMultiPage
? { combine: declaration.pages.map(page => DeclarationUtils.pageToJSON(page)) }
: DeclarationUtils.pageToJSON(declaration.pages[0]),
},
});
}
constructor(baseDir, { logger }) {
this.baseDir = baseDir;
this.logger = logger;
}
logDeclaration(declaration) {
this.logger.info(JSON.stringify(DeclarationUtils.declarationToJSON(declaration), null, 2));
}
async updateHistory(serviceId, documentType, documentDeclaration, { previousValidUntil }) {
const historyFullPath = path.join(this.baseDir, `${serviceId}.history.json`);
if (!fs.existsSync(historyFullPath)) {
fs.writeFileSync(historyFullPath, `${JSON.stringify({ [documentType]: [] }, null, 2)}\n`);
}
const currentJSONDeclaration = { ...DeclarationUtils.declarationToJSON(documentDeclaration).documents[documentType] };
const existingHistory = JSON.parse(fs.readFileSync(historyFullPath).toString());
const historyEntries = existingHistory[documentType] || [];
let entryAlreadyExists = false;
existingHistory[documentType] = [...existingHistory[documentType] || []];
historyEntries.map(({ validUntil, ...historyEntry }) => {
const diff = DeepDiff.diff(historyEntry, currentJSONDeclaration);
if (diff) {
return { ...historyEntry, validUntil };
}
entryAlreadyExists = true;
this.logger.info(`History entry is already present, updating validUntil to ${previousValidUntil}`);
return { ...historyEntry, validUntil: previousValidUntil };
});
if (entryAlreadyExists) {
existingHistory[documentType] = historyEntries;
} else {
this.logger.info('History entry does not exist, creating one');
existingHistory[documentType] = [ ...historyEntries, { ...currentJSONDeclaration, validUntil: previousValidUntil }];
}
fs.writeFileSync(historyFullPath, `${JSON.stringify(existingHistory, null, 2)}\n`);
}
}