-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
executable file
·109 lines (103 loc) · 3.05 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
#!/usr/bin/env node
var fs = require('fs');
var logger = require('./lib/log');
var color = require('./lib/util/colors').color;
var path = require('path');
function cli(args) {
if (args[2] === "--help" || args[2] === "-h") {
console.log(fs.readFileSync(__dirname + "/man/stubmatic.1", 'utf-8'));
} else if (args[2] === "--version") {
console.log(require(__dirname + "/package.json").version);
} else if (args[2] === "init") {
require('./init').init(args[3] || "stub-repo");
} else if (args[2] === "validate") {
validateSyntax(args[3]);
} else {
var options = buildServerOptions(args);
var StubmaticServer = require('./lib/server');
const server = new StubmaticServer(options);
server.start();
}
}
function validateSyntax(fileName) {
try {
if (fileName.endsWith(".json")) {
require('jsonlint').parse(fs.readFileSync(fileName, {
encoding: 'utf-8'
}));
console.log("Validated successfully");
} else if (fileName.endsWith(".yaml") || fileName.endsWith(".yml")) {
require('yamljs').parseFile(fileName);
console.log("Validated successfully");
}else if(fileName.endsWith(".xml")){
var result = require('fast-xml-parser').validate(fs.readFileSync(fileName, {
encoding: 'utf-8'
}));
if(result === true) {
console.log("Validated successfully");
}else{
console.log("Validation failed");
}
}else {
console.log("Unsupported file");
}
} catch (e) {
console.log("Validation failed");
console.log(color(e, 'red'));
//if(e.line) console.log("line number: " + e.line + ":" + e.column);
}
}
function buildServerOptions(args) {
var options = {};
for (var i = 2; i < args.length; i++) {
if (args[i].indexOf("-") === 0) {
var key = args[i];
if (key === '-d') {
var dirpath = args[++i];
if (isExist(dirpath)) {
global.basePath = dirpath;
} else {
global.basePath = path.join(process.cwd(), dirpath);
}
options[key] = global.basePath;
} else if (key === '--from') { // target URL
options[key] = args[++i];
} else if (key === '--record') { //only valid in case of target URL
options[key] = args[++i];
} else if (key === '-v' || key === '--verbose') {
logger.setVerbose(true);
} else if (key === '-l' || key === '--logs') {
logger.writeLogs(true);
} else if (key === '--debug') {
logger.debugLogs(true);
} else {
if (key === '--port' || key === '-p') {
key = '-p';
} else if (key === '--config' || key === '-c') {
key = '-c';
} else if (key === '--host' || key === '-P' || key === '--mutualSSL') {
//valid keys
} else {
console.log("Invalid options");
console.log("Try 'stubmatic --help' for more information.");
throw new Error("Invalid options");
}
options[key] = args[++i];
}
}
}
if( ( options["--record"] && !options["--from"] )){
console.log("Invalid options");
throw new Error("'--record' option is valid only with '--from' option");
}
return options;
}
var isExist = function (filepath) {
try {
fs.accessSync(filepath, fs.F_OK);
return true;
} catch (e) {
return false;
}
};
cli(process.argv);