-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathgruntfile.js
146 lines (124 loc) · 5.16 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
var _ = require("underscore");
module.exports = function (grunt) {
"use strict";
var packageFile = grunt.file.readJSON("package.json");
var countriesJSON = grunt.file.readJSON("node_modules/country-region-data/data.json");
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-template");
// used for the customBuild target. This generates a custom build of CRS with the list of countries specified
// by the user
function getCountryList() {
var countries = grunt.option("countries");
if (!countries) {
grunt.fail.fatal("Country list not passed. Example usage: `grunt customBuild --countries=Canada,United States`");
}
// countries may contain commas in their names. Bit ugly, but simple. This swiches out those escaped commas
// and replaces them later
var commaReplaced = countries.replace(/\\,/g, '{COMMA}');
var targetCountries = _.map(commaReplaced.split(","), function (country) {
return country.replace(/\{COMMA\}/, ',').trim();
});
var countryData = [];
var foundCountryNames = [];
var formattedData = minifyJSON(countriesJSON);
_.each(formattedData, function (countryInfo) {
var countryName = countryInfo[0];
if (_.contains(targetCountries, countryName)) {
countryData.push(countryInfo);
foundCountryNames.push(countryName);
}
});
// if one or more of the countries wasn't found, they probably made a typo: throw a warning but continue
if (targetCountries.length !== countryData.length) {
grunt.log.error("The following countries weren't found (check the source/data.json file to ensure you entered the exact country string):");
var missing = _.difference(targetCountries, foundCountryNames);
_.each(missing, function (countryName) {
grunt.log.error("--", countryName);
});
// all good! Let the user know what bundle is being created, just to remove any ambiguity
} else {
grunt.log.writeln("");
grunt.log.writeln("Creating bundle with following countries:");
_.each(targetCountries, function (country) {
grunt.log.writeln("* " + country);
});
}
config.template.customBuild.options.data.__DATA__ = "\nvar _data = " + JSON.stringify(countryData);
}
// converts the data.json content from the country-region-data
function minifyJSON(json) {
var js = [];
json.forEach(function (countryData) {
var pairs = [];
countryData.regions.forEach(function (info) {
if (_.has(info, 'shortCode')) {
pairs.push(info.name + '~' + info.shortCode);
} else {
pairs.push(info.name);
}
});
var regionListStr = pairs.join('|');
js.push([
countryData.countryName,
countryData.countryShortCode,
regionListStr
]);
});
return js;
}
var config = {
template: {
generate: {
options: {
data: {
__VERSION__: packageFile.version,
__DATA__: "\nvar _data = " + JSON.stringify(minifyJSON(countriesJSON))
}
},
files: {
"dist/crs.js": ["source/source-crs.js"],
"dist/jquery.crs.js": ["source/source-jquery.crs.js"]
}
},
customBuild: {
options: {
data: {
__VERSION__: packageFile.version,
__DATA__: "" // populated dynamically
}
},
files: {
"dist/crs.js": ["source/source-crs.js"],
"dist/jquery.crs.js": ["source/source-jquery.crs.js"]
}
}
},
uglify: {
standalone: {
files: {
"dist/crs.min.js": "dist/crs.js",
"dist/jquery.crs.min.js": "dist/jquery.crs.js"
},
options: {
report: "min",
compress: {},
mangleProperties: true,
banner: "/*!\n" +
"* country-region-selector\n" +
"* ------------------------\n" +
"* " + packageFile.version + "\n" +
"* @author Ben Keen\n" +
"* @repo https://github.com/benkeen/country-region-selector\n" +
"* @licence MIT\n" +
"*/\n"
}
}
},
build: {}
};
grunt.initConfig(config);
grunt.registerTask("default", ["template:generate", "uglify"]);
grunt.registerTask("generate", ["template:generate", "uglify"]);
grunt.registerTask("customBuild", ["build", "template:customBuild", "uglify"]);
grunt.registerTask("build", getCountryList);
};