This repository was archived by the owner on Oct 15, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck_api.js
More file actions
executable file
·76 lines (67 loc) · 2.27 KB
/
Copy pathcheck_api.js
File metadata and controls
executable file
·76 lines (67 loc) · 2.27 KB
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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const https = require('https');
const url = require('url');
const util = require('util');
const fetch = require('node-fetch');
const check_api = require('./index.js');
const red = process.env.NODE_DISABLE_COLORS ? '' : '\x1b[31m';
const green = process.env.NODE_DISABLE_COLORS ? '' : '\x1b[32m';
const normal = process.env.NODE_DISABLE_COLORS ? '' : '\x1b[0m';
const options = {};
options.source = process.argv[2];
options.convert = process.argv.length>3;
options.fetchOptions = {};
options.fetchOptions.headers = { "Accept-Language": "en-GB,en;q=0.5" };
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();
}
let u = url.parse(process.argv[2]);
if (u.protocol && u.protocol.startsWith('http')) {
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);
});
}