This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyNationalTeamsLogos.js
71 lines (64 loc) · 1.9 KB
/
copyNationalTeamsLogos.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
var path = require('path');
var fs = require('fs');
var glob = require('glob');
var pattern = path.join(__dirname, 'dist', 'data', 'teams', '*_1.json');
var async = require('async');
var map = {};
glob(pattern, {}, function (err, files) {
async.eachLimit(files, 10, function (file, fileCb) {
fs.readFile(file, 'utf8', function (err, content) {
if (err) {
console.error('unable to read', file);
return fileCb();
}
var json;
try {
json = JSON.parse(content);
} catch (e){
console.error('unable to parse', file);
return fileCb();
}
if(json.type !== 'CENAT'){
return fileCb();
}
map[json.id] = json.country;
fileCb();
});
}, function () {
async.eachOf(map, function( country,teamId, teamCb){
var flag = path.join(__dirname, 'dist/data/flags/flags_un/48', country + '.png');
var logo = path.join(__dirname, 'dist/data/teams/logos', teamId + '.png');
copyFile(flag, logo, function(err){
if (err){
console.error('ERR', country, teamId);
} else {
console.info('OK', country, teamId);
}
teamCb();
});
}, function(){
console.info('done');
})
});
});
function copyFile(source, target, cb) {
var cbCalled = false;
var rd = fs.createReadStream(source);
rd.on("error", function(err) {
done(err);
});
var wr = fs.createWriteStream(target);
wr.on("error", function(err) {
done(err);
});
wr.on("close", function(ex) {
done();
});
rd.pipe(wr);
function done(err) {
if (!cbCalled) {
cb(err);
cbCalled = true;
}
}
}