-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsyncRulesFormatter.js
More file actions
62 lines (46 loc) · 1.29 KB
/
rsyncRulesFormatter.js
File metadata and controls
62 lines (46 loc) · 1.29 KB
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
// Description: Formats rsync rules to be in the correct order
// Version: 1.0.0
// Author URI:
function parseRule(rule) {
const exclude = ! rule.trim().startsWith('!');
const isDir = rule.trim().endsWith('/');
rule = rule.trim().replace('!', '');
const specificity = getRuleSpecificity(rule);
if (isDir && ! exclude ) {
rule = rule + '***';
}
rule = (exclude ? '- ' : '+ ') + rule;
return {
specificity,
rule,
};
}
function getRuleSpecificity(rule) {
let score = 0;
const parts = rule.split('/');
parts.forEach(part => {
if (part.trim() === '') {
return;
}
if (part.includes('*')) {
part = part.replace(/\*/g, ''); // Remove all asterisks
score += part.trim() === '' ? 5 : 10; // If part was only asterisks, add 5, if there were more chars, add 10
} else {
score += 20; // No asterisks
}
});
return score;
}
function run(input) {
const rulesArray = input.trim().split('\n').filter(rule => rule.trim() !== '');
const rsyncRules = [];
rulesArray.forEach(rule => {
rsyncRules.push(parseRule(rule));
});
rsyncRules.sort((a, b) => (a.specificity < b.specificity ? 1 : -1));
const rsyncRulesFormatted = rsyncRules.map(rule => rule.rule);
return rsyncRulesFormatted.join('\n');
}
module.exports = {
run,
};