-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (37 loc) · 1.54 KB
/
index.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
var RestFixtures = (function() {
'use strict';
var util = require('util');
var TEMPLATE = 'try { __RESTFIXTURES__.push(%s) } catch(e) { var __RESTFIXTURES__ = []; __RESTFIXTURES__.push(%s) }';
function restFixturesPreprocessor(logger, config) {
config = config || {
basePath: '/',
restPath: '/'
};
var log = logger.create('preprocessor.rest-fixtures');
return function(content, file, done) {
log.debug('Processing \"%s\".', file.originalPath);
var index = file.path.indexOf(config.basePath);
var path = file.path.substr(index + config.basePath.length).replace(/\.json$/i, '').replace(/\./g, '/');
var method = path.indexOf('-') >= 0 ? path.substr(path.indexOf('-') + 1) : 'GET'; // If no method is specified, default to GET.
path = path.indexOf('-') >= 0 ? path.substr(0, path.indexOf('-')) : path;
// Files starting with double underscores are root endpoints, so strip the filename from the path.
var rootIndex = path.indexOf('__');
if (rootIndex >= 0) {
path = path.substr(0, rootIndex - 1);
}
var fixture = JSON.stringify({
method: method.toUpperCase(),
url: config.restPath + path,
response: JSON.parse(content),
});
file.path += '.js';
done(util.format(TEMPLATE, fixture, fixture));
}
}
restFixturesPreprocessor.$inject = ['logger', 'config.restFixturesPreprocessor'];
return restFixturesPreprocessor;
})();
// PUBLISH DI MODULE
module.exports = {
'preprocessor:rest-fixtures': ['factory', RestFixtures],
};