forked from thomaspark/glyphsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
124 lines (85 loc) · 3.05 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
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')
// console.log('IN', materialIconsData)
var fs = require('fs')
var cvsParse = require('csv-parse')
var writeStream = fs.createWriteStream('./data/icons-material.json')
var fontCodepointsParser = cvsParse({delimiter: ' ', columns: ['name', 'unicode']}, function(err, fontCodepoints){
// console.log('CODEPOINTS', fontCodepoints)
var fontCodepointsMap = {}
fontCodepoints.forEach(function(codepoint) {
fontCodepointsMap['' + codepoint.name] = codepoint.unicode
fontCodepointsMap['' + codepoint.unicode] = codepoint.name
})
materialIconsData.material.forEach(function(item) {
item.unicode = fontCodepointsMap[item.name]
// item.tags = ''
})
console.log('OUT', materialIconsData)
writeStream.write(JSON.stringify(materialIconsData, null, ' '), 'utf8')
writeStream.end()
done()
})
fs.createReadStream('./bower_components/material-design-icons/iconfont/codepoints').pipe(fontCodepointsParser)
});
};