-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
105 lines (96 loc) · 2.38 KB
/
utils.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
const fs = require('fs');
/* eslint-disable no-param-reassign */
const parseCLIOptions = (options, config) => {
if (options.length === 0) {
return;
}
const arg = options[0];
if (options.length === 1) {
config.path = arg;
return;
}
if (arg.includes('--')) {
config[arg.substr(2)] = options[1]; /* eslint-disable-line prefer-destructuring */
options.splice(0, 2);
} else {
options.splice(0, 1);
}
parseCLIOptions(options);
};
/* eslint-enable no-param-reassign */
exports.parseCLIOptions = parseCLIOptions;
const readFile = (path, retries) => {
const response = fs.readFileSync(path, { encoding: 'utf-8' });
if ((!response || response.length === 0) && retries !== 0) {
return readFile(path, retries - 1);
}
return response;
};
const loadMockApi = (path) => {
try {
fs.statSync(path);
const apiData = readFile(path, 10);
const api = JSON.parse(apiData);
return api;
} catch (e) {
if (e.name === 'SyntaxError') {
console.log(`⚠️ Could not parse ${path}`);
return null;
}
if (e.message.includes('ENOENT')) {
console.log(`⚠️ No api config found @ ${path}`);
return null;
}
console.error(e);
process.exit(1);
return null;
}
};
exports.loadMockApi = loadMockApi;
/* eslint-disable no-param-reassign */
const parseKeys = (roots, parts, key = '') => {
if (roots.length === 0) {
// if lengths match
if (parts.length === 0) {
return key;
}
return '';
}
// if we have an exact match
if (roots[0] === parts[0]) {
key += `/${roots[0]}`;
return parseKeys(roots.slice(1), parts.slice(1), key);
}
// if we have a `:name` wildcard
if (roots[0].includes(':')) {
key += `/${roots[0]}`;
return parseKeys(roots.slice(1), parts.slice(1), key);
}
// if we have no match
return '';
};
/* eslint-enable no-param-reassign */
/* eslint-disable no-restricted-syntax */
const findMockKey = (roots, parts) => {
let key = '';
for (const rootPath of roots) {
const tempKey = parseKeys(
rootPath
.split(' ')[1]
.split('/')
.slice(1),
parts,
);
if (tempKey.length && !tempKey.includes(':') && key && key.includes(':')) {
key = tempKey;
break;
}
if (key.length) {
break;
}
key = tempKey;
}
return key;
};
/* eslint-enable no-restricted-syntax */
exports.findMockKey = findMockKey;