forked from Mermade/check_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_api.js
72 lines (65 loc) · 2.13 KB
/
check_api.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
#!/usr/bin/env node
var fs = require('fs');
var https = require('https');
var url = require('url');
var util = require('util');
var fetch = require('node-fetch');
var check_api = require('./index.js');
var red = process.env.NODE_DISABLE_COLORS ? '' : '\x1b[31m';
var green = process.env.NODE_DISABLE_COLORS ? '' : '\x1b[32m';
var normal = process.env.NODE_DISABLE_COLORS ? '' : '\x1b[0m';
var options = {};
options.source = process.argv[2];
options.convert = process.argv.length>3;
options.fetchOptions = {};
function result(err,options) {
if (options.convert && options.converted) {
fs.writeFileSync(process.argv[3],JSON.stringify(options.converted,null,2),'utf8');
}
if (options && options.format && options.mode) {
console.log(normal+'Mode: %s, format: %s',options.mode,options.format);
}
if (err) {
if (err.message) {
console.error(red+err.message);
if (err.stack && err.name !== 'AssertionError') {
console.log(err.stack);
}
}
else {
console.error(red+util.inspect(err,{depth:4}));
}
}
else process.stdout.write(green);
if (options && options.output) console.log(util.inspect(options.output));
if (options && options.message) console.log(options.message);
if (options && options.context) {
let context = options.context.pop();
console.log(context);
}
if (!err) process.exitCode = 0;
console.log('Exiting with result code: %s',process.exitCode);
}
process.exitCode = 1;
if (process.argv.length<3) {
console.log('Usage: node check_api {url-or-filename} [{convert-filename}]');
process.exit();
}
var u = url.parse(process.argv[2]);
if (u.protocol) {
if (u.protocol.startsWith('https')) {
options.fetchOptions.agent = new https.Agent({rejectUnauthorized: false});
}
fetch(process.argv[2],options.fetchOptions)
.then(res => {
return res.text();
})
.then(data => {
check_api.check_api(data,options,result);
});
}
else {
fs.readFile(process.argv[2],'utf8',function(err,data){
check_api.check_api(data,options,result);
});
}