forked from ramoona/banks-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.js
76 lines (64 loc) · 2.44 KB
/
lint.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
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const JSV = require('JSV').JSV;
const linter = JSV.createEnvironment();
const jsonfile = require('jsonfile');
function showError(filePath, error) {
error = error || '';
console.error(chalk.red('FAIL ' + filePath + error));
}
fs.readdir(path.join(__dirname, 'banks'), (err, files) => {
if (err) throw err;
const countries = files.filter(file =>
fs.lstatSync(path.join(__dirname, 'banks/' + file)).isDirectory());
countries.forEach(country => {
const banks = fs.readdirSync(
path.join(__dirname, 'banks/' + country)).filter(file => /\.json$/.test(file)
);
jsonfile.readFile(path.join(__dirname, 'schema.json'), (err1, schema) => {
if (err1) throw err1;
banks.forEach(name => {
const bankPath = 'banks/' + country + '/' + name;
jsonfile.readFile(path.join(__dirname, bankPath), (err2, bank) => {
if (err2) throw err2;
const report = linter.validate(bank, schema);
name = name.replace(/\.json$/, '');
if (report.errors.length > 0) {
showError(bankPath);
report.errors.forEach(i => console.error(i));
process.exit(1);
} else if (bank.country !== country) {
showError(bankPath, ':\ncountry folder doesn\'t match with bank country');
process.exit(1);
} else if (bank.name !== name) {
showError(bankPath, ':\nJSON filename doesn\'t match with bank name');
process.exit(1);
} else if (!/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/.test(bank.color)) {
showError(bankPath, ': \ninvalid color format (use HEX)');
process.exit(1);
} else {
bank.prefixes.sort();
if (/[A-F]\w*/.test(bank.color)) {
bank.color = bank.color.toLowerCase();
}
jsonfile.writeFile(path.join(__dirname, bankPath), bank, { spaces: 2 }, err3 => {
if (err3) {
throw err3;
} else {
console.log(chalk.green('OK ') + chalk.white('banks/' + country + '/' + name));
}
});
}
});
});
});
});
});
fs.readdir(path.join(__dirname, 'banks'), (err4, items) => {
if (err4) throw err4;
if (/\.json/.test(items.join())) {
showError('banks/', ': JSON must not be placed straght in banks folder');
process.exit(1);
}
});