-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
executable file
·197 lines (167 loc) · 6.64 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
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
/*
* gult-stubby-server
* https://github.com/felixzapata/gulp-stubby-server
*
* Copyright (c) 2014 Félix Zapata
* Licensed under the MIT license.
*/
'use strict';
var _ = require('lodash'),
fs = require('fs'),
glob = require('glob-all'),
gutil = require('gulp-util'),
Stubby = require('stubby').Stubby,
path = require('path'),
YAML = require('js-yaml'),
PLUGIN_NAME = 'gulp-stubby-server';
// defines the absolute path for external static request/response
// files that will be processed internally by Stubby
function setPathStaticFiles(array, filepath) {
filepath = path.dirname(filepath);
function setAbsoluteFilePath(file) {
return filepath + '/' + file;
}
array = array.map(function(object) {
if (_.isObject(object.request) && object.request.file) {
if (!isPathAbsolute(object.request.file)) {
object.request.file = setAbsoluteFilePath(object.request.file);
}
}
if (_.isObject(object.response)) {
// support collections for responses
if (_.isArray(object.response)) {
object.response = object.response.map(function(response) {
if (response.file && !isPathAbsolute(response.file)) {
response.file = setAbsoluteFilePath(response.file);
}
return response;
});
} else {
if (object.response.file && !isPathAbsolute(object.response.file)) {
object.response.file = setAbsoluteFilePath(object.response.file);
}
}
}
return object;
});
return array;
}
function isPathAbsolute() {
var filepath = path.join.apply(path, arguments);
return path.resolve(filepath) === filepath.replace(/[\/\\]+$/, '');
}
function readYAML(filepath, options) {
var src = fs.readFileSync(filepath, options),
result;
if (!options.quiet) {
gutil.log(gutil.colors.yellow('Parsing ' + filepath + '...'));
}
try {
result = YAML.load(src);
return result;
} catch (e) {
throw new gutil.PluginError(PLUGIN_NAME, 'Unable to parse "' + filepath + '" file (' + e.problem + ').', e);
}
}
function readJSON(filepath, options) {
var src = fs.readFileSync(filepath, options),
result;
if (!options.quiet) {
gutil.log(gutil.colors.yellow('Parsing ' + filepath + '...'));
}
try {
result = JSON.parse(src);
return result;
} catch (e) {
throw new gutil.PluginError(PLUGIN_NAME, 'Unable to parse "' + filepath + '" file (' + e.message + ').', e);
}
}
function getAbsolutePath(filepath) {
if (!isPathAbsolute(filepath)) {
filepath = process.cwd() + '/' + filepath;
}
return filepath;
}
function stubbyPlugin(customOptions, cb) {
// Merge task-specific and/or target-specific options with these defaults.
var defaultOptions = {
callback: null, // takes one parameter: the error message (if there is one), undefined otherwise
stubs: 8882, // port number to run the stubs portal
admin: 8889, // port number to run the admin portal
tls: 7443, // port number to run the stubs portal over https
data: null, // JavaScript Object/Array containing endpoint data
location: 'localhost', // address/hostname at which to run stubby
key: null, // keyfile contents (in PEM format)
cert: null, // certificate file contents (in PEM format)
pfx: null, // pfx file contents (mutually exclusive with key/cert options)
watch: null, // filename to monitor and load as stubby's data when changes occur
quiet: true, // defaults to true. Pass in false to have console output (if available)
relativeFilesPath: false, // if enabled, obtains the data mock file path relatively to the config file directory
persistent: false // Run the task in a persistent server mode. Other tasks not will run until the Stubby server stops
},
options = customOptions ? _.assign(defaultOptions, customOptions) : defaultOptions,
stubbyServer = new Stubby(),
data, files;
if (options.files) {
files = glob.sync(options.files);
// Iterate over all specified file groups.
data = _.union.apply(_, files.filter(function(filepath) {
// Warn on and remove invalid source files (if nonull was set).
if (!fs.existsSync(filepath)) {
gutil.log(gutil.colors.red('Source file "' + filepath + '" not found.'));
return false;
}
return true;
}).map(function(filepath) {
var data;
filepath = getAbsolutePath(filepath);
// Read file source.
if (/\.ya?ml$/g.test(filepath)) {
data = readYAML(filepath, options);
} else if (/\.js$/g.test(filepath)) {
try {
data = require(filepath);
} catch (e) {
gutil.log(gutil.colors.red('Error while parsing JS file "' + filepath + '"', 1));
}
} else {
data = readJSON(filepath, options);
}
if (!_.isArray(data)) {
data = [data];
}
return options.relativeFilesPath ? setPathStaticFiles(data, filepath) : data;
}));
if (_.isObject(options.data)) {
if (_.isArray(options.data)) {
options.data = _.union(options.data, data);
} else {
options.data = data.push(options.data);
}
} else {
options.data = data;
}
stubbyServer.start(_.omit(options, 'callback', 'relativeFilesPath', 'persistent'), function(error) {
if (error) {
gutil.log(gutil.colors.red('Stubby error: "' + error));
return cb();
}
if (_.isFunction(options.callback)) {
options.callback(stubbyServer, options);
}
gutil.log(gutil.colors.green('Stubby HTTP server listening on port ' + options.stubs));
gutil.log(gutil.colors.green('Stubby HTTPS server listening on port ' + options.tls));
gutil.log(gutil.colors.green('Admin server listening on port ' + options.admin));
if (!options.persistent) {
cb();
}
});
}
return {
stop: function() {
stubbyServer.stop();
gutil.log(gutil.colors.green('Stubby server stopped'));
}
};
}
module.exports = stubbyPlugin;