-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathbuild-community-groups.js
More file actions
executable file
·68 lines (61 loc) · 1.83 KB
/
Copy pathbuild-community-groups.js
File metadata and controls
executable file
·68 lines (61 loc) · 1.83 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
63
64
65
66
67
68
#!/usr/bin/env node
const Fs = require('fs')
const cmdwatcher = require('./util/cmdwatcher')
const mkdirp = require('mkdirp')
function groupByValue(list, grouper, groupName) {
var grouped = {}
groupName = groupName || 'items'
for (var i = 0; i < list.length; i++) {
var key = list[i][grouper]
if (typeof grouped[key] !== "object" || typeof grouped[key].push !== "function") {
grouped[key] = []
}
grouped[key].push(list[i])
}
return Object.keys(grouped).map(function (group) {
var formatted = {}
formatted[grouper] = group
formatted.count = grouped[group].length
formatted[groupName] = grouped[group]
return formatted
});
}
function sortByKey(list, key) {
return list.sort(function (a, b) {
return a[key] > b[key] ? 1 : 0
})
}
function sortedGroupByValue(list, grouper, groupName) {
return sortByKey(groupByValue(list, grouper, groupName), grouper);
}
var communityGroups = [];
cmdwatcher('build-community-groups'
, './community-groups/!(list).json', function (files) {
files.forEach(function (f) {
Fs.readFile(f, function (err, buf) {
if (err) {
return console.error(err);
}
var data = buf.toString();
try {
var communityGroup = JSON.parse(data);
} catch (e) {
console.error("JSON parse error: " + f, e);
}
communityGroups.push(communityGroup);
if (communityGroups.length === files.length) {
writeCommunityGroups(communityGroups);
}
});
});
});
function writeCommunityGroups(communityGroups) {
data = {
total: communityGroups.length,
regions: sortedGroupByValue(communityGroups, 'name', 'communityGroups')
};
mkdirp.sync('.build/communityGroups')
Fs.writeFile('.build/communityGroups/list.json', JSON.stringify(data, null, 2), function (err) {
if (err) console.error(err);
});
}