forked from cncf/landscapeapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckWrongCharactersInFilenames.js
79 lines (73 loc) · 2.97 KB
/
checkWrongCharactersInFilenames.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
const _ = require('lodash');
const path = require('path');
const { projectPath } = require('./settings');
const { dump } = require('./yaml');
const { hasFatalErrors, setFatalError, reportFatalErrors } = require('./fatalErrors');
function hasNonAscii(str) {
return ! /^[\x00-\x7F]*$/.test(str);
}
async function main() {
const source = require('js-yaml').load(require('fs').readFileSync(path.resolve(projectPath, 'landscape.yml')));
const processedSource = require('js-yaml').load(require('fs').readFileSync(path.resolve(projectPath, 'processed_landscape.yml')));
// fix landscape itself with logos
_.each(source.landscape, function(category) {
_.each(category.subcategories, function(subcategory) {
_.each(subcategory.items, function(item) {
if (!item.logo) {
const error = `FATAL: entry ${item.name} is missing a logo`;
console.info(error);
setFatalError(error);
}
if (item.logo && item.logo.indexOf('/') === -1) {
const logo = item.logo;
const processedLogo = _.deburr(logo);
if (hasNonAscii(processedLogo)) {
const error = `FATAL: entry ${item.name} has non ascii characters in a logo ${logo}`;
console.info(error);
setFatalError(error);
}
else if (logo !== processedLogo) {
item.logo = processedLogo;
const oldFile = path.resolve(projectPath, 'hosted_logos', logo);
const newFile = path.resolve(projectPath, 'hosted_logos', processedLogo);
require('fs').renameSync(oldFile, newFile);
console.info(`RENAMED: ${logo} => ${processedLogo}`);
}
}
});
});
});
require('fs').writeFileSync(path.resolve(projectPath, 'landscape.yml'), dump(source));
// fix processed landscape
_.each(processedSource.landscape, function(category) {
_.each(category.subcategories, function(subcategory) {
_.each(subcategory.items, function(item) {
if (item.image_data) {
const logo = item.image_data.fileName;
const processedLogo = _.deburr(logo);
if (hasNonAscii(processedLogo)) {
const error = `FATAL: entry ${item.name} has non ascii characters in a logo ${logo}`;
console.info(error);
setFatalError(error);
}
else if (logo !== processedLogo) {
item.image_data.fileName = processedLogo;
const oldFile = path.resolve(projectPath, 'cached_logos', logo);
const newFile = path.resolve(projectPath, 'cached_logos', processedLogo);
require('fs').renameSync(oldFile, newFile);
console.info(`RENAMED: ${logo} => ${processedLogo}`);
}
}
});
});
});
require('fs').writeFileSync(path.resolve(projectPath, 'processed_landscape.yml'), dump(processedSource));
if (hasFatalErrors()) {
await reportFatalErrors();
process.exit(1);
}
}
main().catch(function(ex) {
console.info(ex);
process.exit(1);
});