-
Notifications
You must be signed in to change notification settings - Fork 18
/
start.js
53 lines (49 loc) · 1.29 KB
/
start.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
'use strict';
const out = require('simple-output');
const globby = require('globby');
const methods = require('methods');
const hashLoader = require('stubborn-server-hash-loader');
function startCmd(opts) {
const mockFolderName = opts.mockFolderName || '__mocks__';
const port = opts.port || 8059;
try {
opts.stubborn.start({
logMode: opts.verbose ? 'all' : 'warn',
namespace: '',
pathToMocks: mockFolderName,
servePort: port,
fallbacks: [],
plugins: [
{
loader: hashLoader({
algorithm: opts.hashAlgorithm,
headers: opts.hashHeaders || [],
cookies: opts.hashCookies || []
})
}
]
});
if (!opts.silent) {
out.success('Successfully launched snapstub server on: ' +
'http://localhost:' + port);
printRoutes(mockFolderName, port);
}
} catch (e) {
out.error('Failed to launch snapstub server');
}
}
function printRoutes(srcPath, port) {
const patterns = methods.map(m => `**/${m}.+(json|js)`);
globby(patterns, {cwd: srcPath})
.then(paths => {
const routes = paths.map(p => {
// Remove filename from path
const directoryPath = p.substring(0, p.lastIndexOf('/'));
return `http://localhost:${port}/${directoryPath}`;
});
for (let route of new Set(routes)) {
out.info(route);
}
});
}
module.exports = startCmd;