forked from OpenTermsArchive/versions-cleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCleaner.js
98 lines (81 loc) · 2.92 KB
/
Cleaner.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
import fs from 'fs';
import path from 'path';
const DEFAULT_CONTENT = {
documents: {},
documentTypes: { },
};
export default class Cleaner {
constructor(baseDir, filename = 'index.json') {
this.baseDir = baseDir;
this.filename = filename;
this.filePath = path.join(baseDir, filename);
fs.mkdir(baseDir, { recursive: true }, () => {
if (!fs.existsSync(this.filePath)) {
fs.writeFileSync(this.filePath, `${JSON.stringify(DEFAULT_CONTENT, null, 2)}\n`);
}
});
}
getRules() {
if (!this.rules) {
this.rules = JSON.parse(fs.readFileSync(this.filePath).toString());
}
return this.rules;
}
update(newRules) {
this.rules = newRules;
fs.writeFileSync(this.filePath, `${JSON.stringify(newRules, null, 2)}\n`);
}
updateDocument(serviceId, documentType, field, value) {
const cleaningRules = this.getRules();
const service = cleaningRules.documents[serviceId] || {};
const document = service[documentType] || {};
let updatedValue;
if (field == 'skipContent') {
updatedValue = { ...document[field] || {}, ...value };
} else if ([ 'skipCommit', 'skipSelector', 'skipMissingSelector' ].includes(field)) {
updatedValue = [ ...document[field] || [], value ];
} else if (field == 'done') {
updatedValue = value;
}
cleaningRules.documents = {
...cleaningRules.documents,
[serviceId]: {
...service,
[documentType]: {
...document,
[field]: updatedValue,
},
},
};
// logger.debug(`${value} appended to ${field}`);
this.update(cleaningRules);
}
getSnapshotIdsToSkip(serviceId, documentType) {
const snapshotsIds = Object.entries(this.getRules().documents)
.filter(([cleaningServiceId]) => [ '*', serviceId ].includes(cleaningServiceId))
.map(([ , cleaningDocumentTypes ]) => Object.entries(cleaningDocumentTypes)
.filter(([cleaningDocumentType]) => [ '*', documentType ].includes(cleaningDocumentType))
.map(([ , { skipCommit }]) => skipCommit)
.filter(skippedCommit => !!skippedCommit)
.flat()).flat();
return snapshotsIds;
}
getDocumentRules(serviceId, documentType) {
const documentRules = (this.getRules().documents[serviceId] || {})[documentType];
const contentsToSkip = (documentRules && documentRules.skipContent) || {};
const selectorsToSkip = (documentRules && documentRules.skipSelector) || [];
const missingRequiredSelectors = (documentRules && documentRules.skipIfMissingSelector) || [];
return {
contentsToSkip,
selectorsToSkip,
missingRequiredSelectors,
};
}
getDocumentTypesRules() {
return this.getRules().documentTypes || {};
}
isDocumentDone(serviceId, documentType) {
const rules = this.getRules();
return rules && rules.documents[serviceId] && rules.documents[serviceId][documentType] && rules.documents[serviceId][documentType].done;
}
}