-
Notifications
You must be signed in to change notification settings - Fork 355
/
gruntfile.js
executable file
·157 lines (125 loc) · 5.07 KB
/
gruntfile.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var _ = require('underscore');
var libumd = require('libumd');
var findDuplicates = function (sourceArray, prop) {
var duplicates = [];
var groupedByCount = _.countBy(sourceArray, function (item) {
return item[prop];
});
for (var name in groupedByCount) {
if (groupedByCount[name] > 1) {
var whereClause = [];
whereClause[prop] = name;
_.where(sourceArray, whereClause).map(function (item) {
duplicates.push(item);
});
}
}
return _.uniq(_.pluck(duplicates, prop));
};
var getJSON = function (grunt) {
var content = '';
try {
content = grunt.file.readJSON('data.json');
} catch (e) {
grunt.fail.fatal('data.json is not valid JSON. Error: ' + e);
}
return content;
};
module.exports = function (grunt) {
function validate () {
var content = getJSON(grunt);
// check country names and country shortcodes are unique
var duplicateCountryNames = findDuplicates(content, 'countryName');
if (duplicateCountryNames.length > 0) {
grunt.fail.fatal('The country names are not unique - duplicates: ' + duplicateCountryNames);
}
var duplicateCountryShortCodes = findDuplicates(content, 'countryShortCode');
if (duplicateCountryShortCodes.length > 0) {
grunt.fail.fatal('The country short codes are not unique - duplicates: ' + duplicateCountryShortCodes);
}
// now check region names and short codes are unique for each country
content.forEach(function (countryData) {
var duplicateRegionNames = findDuplicates(countryData.regions, 'name');
if (duplicateRegionNames.length > 0) {
grunt.fail.fatal('The region names for ' + countryData.countryName + ' are not unique - duplicates: ' + duplicateRegionNames);
}
var duplicateRegionShortCodes = findDuplicates(countryData.regions, 'shortCode');
if (duplicateRegionShortCodes.length > 0) {
grunt.fail.fatal('The region names for ' + countryData.countryName + ' are not unique - duplicates: ' + duplicateRegionShortCodes);
}
});
console.log('PASS!');
}
function findIncomplete () {
var content = getJSON(grunt);
var incompleteCountries = [];
content.forEach(function (countryData) {
for (var i = 0; i < countryData.regions.length; i++) {
if (!_.has(countryData.regions[i], 'shortCode')) {
incompleteCountries.push(countryData.countryName);
break;
}
}
});
if (incompleteCountries.length > 0) {
console.log('\nThe following countries are missing region short codes: \n-', incompleteCountries.join('\n- '));
console.log('\n(' + incompleteCountries.length + ' countries)');
} else {
console.log('All regions now have short codes. Nice!');
}
}
function umdify() {
var content = getJSON(grunt);
var output = libumd('return ' + JSON.stringify(content, null, 2) + ';', {
globalAlias: 'countryRegionData',
indent: 2
});
const file = 'dist/data-umd.js';
grunt.file.write(file, output);
console.log(`UMD module created: ${file}`);
}
function es6ify() {
var content = getJSON(grunt);
const countryNames = content.map(({ countryName }) => countryName);
let output = `export const countryNames = ${JSON.stringify(countryNames)};\n`;
const countryShortCodes = content.map(({ countryShortCode }) => countryShortCode);
output += `export const countryShortCodes = ${JSON.stringify(countryShortCodes)};\n`;
content.map(({ countryName, countryShortCode, regions }) => {
output += `export const ${countryShortCode} = [\n\t"${countryName}",\n\t"${countryShortCode}",\n\t[\n${regions.map(({ name, shortCode }) => `\t\t["${name}", "${shortCode}"]`).join(",\n")}\n\t]\n];\n`;
});
output += `export const allCountries = [${countryShortCodes.join(",")}];\n`;
const countryTuples = content.map(({ countryName, countryShortCode }) => [countryName, countryShortCode]);
output += `export const countryTuples = ${JSON.stringify(countryTuples)};\n`;
const file = 'dist/data.js';
grunt.file.write(file, output);
// now generate the corresponding typings file
let typingsOutput = `declare module 'country-region-data' {
export type CountryName = "${countryNames.join('" | "')}";\n`;
typingsOutput += `\texport type CountrySlug = "${countryShortCodes.join('" | "')}";\n`;
typingsOutput += `\texport type RegionName = string;
export type RegionSlug = string;
export const countryNames: CountryName[];
export const countryShortCodes: CountrySlug[];
export const countryTuples: [CountryName, CountrySlug][];
export type Region = [RegionName, RegionSlug];
export type CountryData = [
CountryName,
CountrySlug,
Region[]
];
export const allCountries: CountryData[];
export default allCountries;
`;
typingsOutput += countryShortCodes.map((shortCode) => `\texport const ${shortCode}: CountryData;`).join("\n");
typingsOutput += '\n}\n';
const typingsFile = 'dist/data.d.ts';
grunt.file.write(typingsFile, typingsOutput);
console.log(`ES6 module created: ${file}`);
}
grunt.registerTask('default', ['validate']);
grunt.registerTask('validate', validate);
grunt.registerTask('findIncomplete', findIncomplete);
grunt.registerTask('build', ['umdify', 'es6ify']);
grunt.registerTask('umdify', umdify);
grunt.registerTask('es6ify', es6ify);
};