-
Notifications
You must be signed in to change notification settings - Fork 102
/
Gruntfile.js
332 lines (286 loc) · 8.5 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* Grunt tasks to support running linter, unit tests, and generating
* documentation.
*
* @module GruntFile
*
* @copyright 2014, Digium, Inc.
* @license Apache License, Version 2.0
* @author Samuel Fortier-Galarneau <[email protected]>
*/
'use strict';
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
gendocs: {
options: {
baseUrl: 'http://ari.js:8088',
username: 'user',
password: 'secret'
}
},
genfixtures: {
options: {
baseUrl: 'http://ari.js:8088',
username: 'user',
password: 'secret'
}
},
jsdoc : {
dist : {
src: [
'lib/*.js',
'examples/*.js',
'test/*.js',
'Gruntfile.js',
'README.md'
],
options: {
destination: 'doc'
}
}
}
});
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-jsdoc');
// Default task.
grunt.registerTask('default', ['gendocs']);
grunt.registerTask(
'gendocs',
'Generate operations and events documentation.',
function () {
var done = this.async();
var options = this.options({});
var swagger = require('swagger-client');
var url = require('url');
var util = require('util');
var fs = require('fs');
var mustache = require('mustache');
var _ = require('lodash');
var resourcesLib = require('./lib/resources.js');
var operations = '';
var events = '';
var operationTemplate = fs.readFileSync(
'./dev/operation.mustache',
'utf-8'
);
var eventTemplate = fs.readFileSync(
'./dev/event.mustache',
'utf-8'
);
var parsedUrl = url.parse(options.baseUrl);
swagger.authorizations.add(
'basic-auth',
new swagger.PasswordAuthorization(
parsedUrl.hostname,
options.username,
options.password
)
);
// Connect to API using swagger and attach resources on Client instance
var resourcesUrl = util.format(
'%s//%s/ari/api-docs/resources.json',
parsedUrl.protocol,
parsedUrl.host
);
var swaggerClient = new swagger.SwaggerApi({
url: resourcesUrl,
success: swaggerLoaded,
failure: swaggerFailed
});
// Swagger success callback
function swaggerLoaded () {
if(swaggerClient.ready === true) {
grunt.log.writeln('generating operations documentation');
var apis = _.sortBy(_.keys(swaggerClient.apis));
_.each(apis, generateOperations);
grunt.log.writeln('generating events documentation');
var models = _.sortBy(_.keys(swaggerClient.apis.events.models));
_.each(models, generateEvents);
var template = fs.readFileSync('./dev/README.mustache', 'utf-8');
var output = mustache.render(template, {
operations: operations,
events: events
});
fs.writeFileSync('./README.md', output, 'utf-8');
done();
}
}
// Swagger failure callback
function swaggerFailed (err) {
grunt.log.error(err);
done(false);
}
// Generate all operations
function generateOperations (resource) {
if (resource !== 'events') {
operations += util.format('#### %s\n\n', resource);
var api = swaggerClient.apis[resource];
var ops = _.sortBy(_.keys(api.operations));
_.each(ops, function (name) {
var operation = api.operations[name];
var results = '';
if (operation.type !== null) {
var returnType = operation.type;
var regexArr =
resourcesLib.swaggerListTypeRegex.exec(returnType);
if (regexArr !== null) {
returnType = util.format('%ss', regexArr[1]);
}
returnType = returnType.toLowerCase();
results += util.format(', %s', returnType);
}
var params = '';
var paramsPromises = '';
var requiredParams = [];
var availableParams = [];
var parameters = _.sortBy(operation.parameters, 'name');
_.each(parameters, function (param) {
if (param.required) {
requiredParams.push(
util.format('%s: val', param.name)
);
}
availableParams.push(
util.format(
'- %s (%s) - %s',
param.name,
param.type,
param.description
)
);
});
if (requiredParams.length > 0) {
params = util.format(
'{%s}', requiredParams.join(', ')
);
params += ',\n ';
paramsPromises = util.format(
'{\n %s\n}', requiredParams.join(',\n ')
);
}
operations += mustache.render(operationTemplate, {
name: name,
desc: operation.summary,
resource: operation.resourceName,
params: params,
paramsPromises: paramsPromises,
results: results,
resultsPromises: results.substring(2)
});
if (availableParams.length > 0) {
operations += util.format(
'###### Available Parameters\n%s\n\n',
availableParams.join('\n')
);
}
});
}
}
// Generate all events
function generateEvents (name) {
if (name !== 'Event' && name !== 'Message') {
var event = swaggerClient.apis.events.models[name];
var results = '';
var props = _.sortBy(event.properties, 'name');
var availableProps = [];
var promoted = [];
var instances = [];
_.each(props, function (prop) {
var propType = prop.dataType;
var regexArr =
resourcesLib.swaggerListTypeRegex.exec(propType);
if (regexArr !== null) {
propType = util.format('%s', regexArr[1]);
}
if (_.includes(resourcesLib.knownTypes, propType)) {
promoted.push(prop.name);
if (!_.includes(instances, propType)) {
instances.push(propType);
}
}
availableProps.push(
util.format(
'- %s (%s) - %s',
prop.name,
prop.dataType,
prop.descr
)
);
});
if (promoted.length > 1) {
results += util.format(', {%s: val}', promoted.join(': val, '));
} else if (promoted.length === 1) {
results += util.format(', %s', promoted[0]);
}
events += mustache.render(eventTemplate, {
name: name,
desc: swaggerClient.apis.events.rawModels[name].description,
results: results
});
if (availableProps.length > 0) {
events += util.format(
'###### Available Event Properties\n%s\n\n',
availableProps.join('\n')
);
}
if (instances.length > 0) {
events += util.format(
'###### Resource Specific Emitters\n%s\n\n',
instances.join('\n')
);
}
}
}
});
grunt.registerTask(
'genfixtures',
'Generate fixtures from ARI for unit tests.',
function () {
var taskDone = this.async();
var options = this.options({});
var request = require('request');
var fs = require('fs');
var util = require('util');
var async = require('async');
var fixtures = [
'resources',
'sounds',
'recordings',
'playbacks',
'mailboxes',
'events',
'endpoints',
'deviceStates',
'channels',
'bridges',
'asterisk',
'applications',
];
// generate all fixtures in parallel
async.each(fixtures, loadFixtureJson, taskDone);
// loads a given fixture from an ARI definition json file
function loadFixtureJson (fixtureName, done) {
grunt.log.writeln(
util.format('generating fixture for %s', fixtureName)
);
var url = util.format(
'%s/ari/api-docs/%s.json?api_key=%s:%s',
options.baseUrl,
fixtureName,
options.username,
options.password
);
request(url, function (err, resp, body) {
var filename = util.format(
'%s/test/fixtures/%s.json',
__dirname,
fixtureName
);
var content = body.replace(/ari\.js/g, 'localhost');
fs.writeFileSync(filename, content);
done();
});
}
});
};