-
Notifications
You must be signed in to change notification settings - Fork 25
/
fontello.js
214 lines (190 loc) · 5.72 KB
/
fontello.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// TODO: Clean up comments
var fs = require('fs');
var path = require('path');
var async = require('async');
var needle = require('needle');
var unzip = require('unzip');
var mkdirp = require('mkdirp');
var grunt = require('grunt');
/* Verify or build paths */
var processPath = function(options, dir, callback){
fs.exists(dir, function(exists){
if(!exists) {
if(!options.force) {
callback(dir + ' missing! use `force:true` to create');
} else {
// Force create path
mkdirp(dir, function(err){
if (err) { callback(err); }
else {
callback(null, dir + ' created!');
}
});
}
} else {
callback(null, dir + ' verified!');
}
});
};
var setSession = function(options, session, config){
var dest = process.cwd() + '/' + options.config;
if (undefined == config)
config = require(dest);
// Write session to config file. Save session in name field since the
// Fontello api dislikes custom members.
config.name = session;
fs.writeFileSync(dest, JSON.stringify(config, null, '\t'));
}
/*
* Initial Checks
* @callback: options
* */
var init = function(options, callback){
grunt.log.write('Verify paths...');
var tests = [
processPath.bind(null, options, options.fonts),
processPath.bind(null, options, options.styles)
];
async.parallel(options.styles ? tests : [tests[0]], function(err, results){
if(err) {
grunt.log.error(err);
callback(err);
}
else {
grunt.log.ok();
results.forEach(function(result){
grunt.log.debug(result);
});
callback(null, options);
}
});
};
/*
* Create Session
* URL: http://fontello.com
* POST: config.json
* @callback: session id
* */
var createSession = function(options, callback){
// TODO: save session somewhere else?
var data = {
config: {
file: options.config,
content_type: 'application/json'
}
};
var session = null;
var config = require(process.cwd() + '/' + options.config);
if (config.name) {
session = config.name
callback(null, options, session);
}
else {
grunt.log.write('Creating session...');
needle.post( options.host, data, { multipart: true }, function(err, res, body){
if (err) {
grunt.log.error();
callback(err);
}
else {
grunt.log.ok();
grunt.log.debug('sid: ' + body);
callback(null, options, body);
}
}
);
}
};
/*
* Download Archive
* URL: http://fontello.com
* GET: http://fontello.com/SESSIONID/get
* callback: fetch/download result
**/
var fetchStream = function(options, session, callback){
// The Fontello api outputs an error message instead of a session id if the
// config file contains unexpected data. Pass that error on.
if (/Invalid/.test(session))
throw new Error(session);
var tempConfig = process.cwd() + '/config-tmp.json';
var tempZip = process.cwd() + '/fontello-tmp.zip';
setSession(options, session);
grunt.log.write('Fetching archive...');
needle.get(options.host + '/' + session + '/get', function(err, response, body){
if(response.statusCode == 404)
{
setSession(options, '');
createSession(options, fetchStream);
}
else
{
fs.writeFileSync(tempZip, body);
var readStream = fs.createReadStream(tempZip);
/* Extract Files */
if(options.fonts || options.styles) {
return readStream.pipe(unzip.Parse())
// TODO: fix inconsistent return point
.on('entry', function(entry){
var ext = path.extname(entry.path);
var name = path.basename(entry.path);
if(entry.type === 'File') {
if(options.exclude.indexOf(name) !== -1) {
grunt.verbose.writeln('Ignored ', entry.path);
entry.autodrain();
} else {
switch(ext){
// Extract Fonts
case '.woff':case '.svg': case '.ttf': case '.eot': case '.woff2':
var fontPath = path.join(options.fonts, path.basename(entry.path));
return entry.pipe(fs.createWriteStream(fontPath));
// Extract CSS
case '.css':
// SCSS:
if (options.styles) {
var cssPath = (!options.scss) ?
path.join(options.styles, path.basename(entry.path)) :
path.join(options.styles, '_' + path.basename(entry.path).replace(ext, '.scss'));
return entry.pipe(fs.createWriteStream(cssPath));
}
case '.json':
if (options.updateConfig) {
var r = entry.pipe(fs.createWriteStream(tempConfig));
r.on('finish', function() {
var config = require(tempConfig);
setSession(options, session, config);
fs.unlinkSync(tempConfig);
});
}
// Drain everything else
default:
grunt.verbose.writeln('Ignored ', entry.path);
entry.autodrain();
}
}
}
})
.on('close', function(){
fs.unlinkSync(tempZip);
grunt.log.ok();
callback(null, 'extract complete');
});
}
/* Extract full archive */
return readStream.pipe(unzip.Extract({ path: options.zip }))
.on('close', function(){
grunt.log.ok();
fs.unlinkSync(tempZip);
callback(null, 'Fontello extracted to '+options.zip);
});
}
if(err){
grunt.log.err();
callback(err);
}
});
};
module.exports = {
init : init,
post : createSession,
fetch : fetchStream
};