forked from country-regions/react-country-region-selector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
executable file
·111 lines (96 loc) · 3.22 KB
/
gulpfile.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
const gulp = require('gulp'),
gulpUtil = require('gulp-util'),
rename = require('gulp-rename'),
_ = require('underscore'),
template = require('gulp-template'),
runSequence = require('run-sequence'),
initGulpTasks = require('react-component-gulp-tasks'),
argv = require('yargs').argv;
// the country + region source data is from the country-region-data
const countriesJSON = JSON.parse(require('fs').readFileSync('./node_modules/country-region-data/data.json'));
// e.g. `gulp --countries="one,two,three"`
gulp.task('customBuild', function () {
function filterCountries (js) {
if (!_.has(argv, 'countries')) {
return js;
}
let targetCountries = argv.countries.split(",");
const filteredCountries = [];
const found = [];
_.each(js, (countryData) => {
var countryShortCode = countryData[1];
if (_.contains(targetCountries, countryShortCode)) {
filteredCountries.push(countryData);
found.push(countryShortCode);
}
});
// if one or more of the countries wasn't found, they probably made a typo. Throw a warning and halt the process.
if (targetCountries.length !== filteredCountries.length) {
let msg = "The following countries weren't found. Check the data.json file in the source repo (https://github.com/benkeen/country-region-data) to ensure you entered the exact country short code:\n";
_.each(_.difference(targetCountries, found), (shortCode) => { msg += "--" + shortCode + '\n'; } );
throw new gulpUtil.PluginError({ plugin: 'react-country-region-selector', message: msg });
}
// all good! Let the user know what bundle is being created, just to remove any ambiguity
console.log("Creating bundle with following countries: " + targetCountries.join(', '));
return filteredCountries;
}
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;
}
gulp.src('src/templates/source-data.template')
.pipe(template({ __DATA__: JSON.stringify(filterCountries(minifyJSON(countriesJSON))) }))
.pipe(rename('source-data.js'))
.pipe(gulp.dest('src'))
});
const taskConfig = {
component: {
name: 'rcrs',
src: 'src',
dist: 'dist',
dependencies: [
'react',
'react-dom',
'underscore'
],
pkgName: 'rcrs'
},
example: {
src: 'examples/src/',
dist: 'examples/dist',
standalone: true,
dependencies: [
'react',
'react-dom'
],
files: [
'index.html',
'css/github-light.css',
'css/normalize.css',
'css/stylesheet.css',
'libs/highlight/highlight.pack.js',
'libs/highlight/railscasts.css'
],
scripts: [
'examples.js'
]
}
};
gulp.task('default', () => { runSequence(['customBuild', 'build'], () => {}); });
initGulpTasks(gulp, taskConfig);