-
Notifications
You must be signed in to change notification settings - Fork 73
/
Gruntfile.js
125 lines (89 loc) · 3.01 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
module.exports = function(grunt) {
grunt.initConfig({
'merge-json': {
icons: {
src: [ "data/icons-*.json" ],
dest: "data/icons.json"
}
},
'compile-handlebars': {
remote: {
template: 'templates/batch.handlebars',
templateData: 'data/icons.json',
output: 'data/batch.json'
}
}
});
grunt.loadNpmTasks('grunt-merge-json');
grunt.loadNpmTasks('grunt-compile-handlebars');
grunt.registerTask('default', function(){
grunt.task.run('build');
});
grunt.registerTask('build', ['merge-json', 'compile-handlebars']);
grunt.registerTask('index', 'Push batch.json to Algolia\'s server', function() {
// --- Step 0: required Algolia api key
var personalApiKey = grunt.option('apikey');
if (!personalApiKey) {
grunt.log.write("Checking API key...").error();
grunt.log.write("Please provide a valid API key with option --apikey");
return;
}
// this task is async
var done = this.async();
// --- Step 1: init Algolia API client
var init = grunt.log.write("Initialize Algolia's client...");
var Algolia = require('algolia-search');
var client = new Algolia('9JQV0RIHU0', personalApiKey);
init.ok();
// --- Step 2: prepare Algolia index
var prepare = grunt.log.write("Clearing index...");
client.deleteIndex('icons');
var index = client.initIndex('icons');
index.setSettings({
'attributesToIndex': ["name", "tags", "unicode"],
'customRanking': ["asc(name)"],
'queryType': 'prefixAll'
});
prepare.ok();
// --- Step 3: push data to Algolia
var push = grunt.log.write("Push batch.json...");
index.addObjects(require('./data/batch.json'), function(error, content) {
if (error) {
push.error();
done(false);
} else {
push.ok();
done();
}
});
});
grunt.registerTask('map-unicodes-for-material-icons', function() {
var done = this.async();
var materialIconsData = require('./data/icons-material.json')
var materialIconsString = JSON.stringify(materialIconsData);
var fontCodepoints = grunt.file.readJSON('./bower_components/material-design-icons-iconfont/dist/fonts/MaterialIcons-Regular.json');
var fontCodepointsMap = {}
Object.keys(fontCodepoints).forEach((name) => {
let unicode = fontCodepoints[name].toLowerCase()
fontCodepointsMap['' + name] = unicode;
if (materialIconsString.indexOf(name) < 0) {
materialIconsData.material.push({'name': name, 'tags': ''});
}
});
materialIconsData.material.forEach(function(item) {
item.unicode = fontCodepointsMap[item.name];
});
materialIconsData.material.sort((a, b) => {
if ( a.name < b.name ){
return -1;
}
if ( a.name > b.name ){
return 1;
}
return 0;
});
// console.log('OUT', materialIconsData);
grunt.file.write('./data/icons-material.json', JSON.stringify(materialIconsData, null, 2));
done();
});
};