-
Notifications
You must be signed in to change notification settings - Fork 1
/
commandline.js
142 lines (128 loc) · 4.64 KB
/
commandline.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env node
process.argv[1] = "gqlapi" // cause yargs to take the name of the bin rather than the name 'index.js'
const fs = require('fs')
const graphql = require('graphql');
const yargs = require('yargs');
const request = require('request');
const apiPageBuilder = require('./apiPageBuilder');
const file="FILE";
const url="URL";
let command;
let args = yargs
.usage('Usage: $0 <from-file|from-url> <options>')
.strict()
.command('from-file <outputfile> <path> <apiname> [description]', 'Writes API as HTML from a file to the given output file',
(yargs) =>{
yargs.positional('outputfile', {
describe: 'name (and path) of file to which the API HTML will be saved',
default: 'API.html'
})
.positional('path', {
describe: "the path to the graphql schema file (in graphql.js SDL format)",
type: 'string',
normalize: true
})
.positional('apiname', {
describe: "name of the API",
type: 'string'
})
.positional('description', {
describe: "description of the API",
type: 'string'
})
},
(args) => {command = file}
)
.command('from-url <outputfile> <url> <apiname> [description]', 'Writes API as HTML from a live GraphQL server (via introspection) to the given output file',
(yargs) =>{
yargs.positional('outputfile', {
describe: 'name (and path) of file to which the API HTML will be saved',
default: 'API.html'
})
.positional('url', {
describe: "the path to the graphql file or the URL to the GraphQL service",
type: 'string'
})
.positional('apiname', {
describe: "name of the API",
type: 'string'
})
.positional('description', {
describe: "description of the API",
type: 'string'
})
},
(args) => {command = url}
)
.option('h', {
alias: 'head-part',
describe: "Path to a file with text (utf-8) that will be inserted at the end of the `head` tag in the output html. " +
"This can be used to provide your own CSS (`link`/`style` tag), your own javascript code (`script` tag) or " +
"anything else. Since this tag is inserted at the end of the `head` tag, it may override anything that " +
"was generated by the tool. THIS TEXT WILL NOT BE SANITIZED as it is intended for use only by the script host, " +
"so don't receive this as input from external users (or sanitize first).",
global: true
})
.demandCommand()
.help()
.argv; //parse
let headPart = null;
if (args.h){
headPart = fs.readFileSync(args.h, 'utf-8');
}
if (command===file) {
let schemaText = fs.readFileSync(args.path, 'utf-8');
let schema;
try{
schema = graphql.buildSchema(schemaText);
}
catch (e){
console.error("Could not build schema from file: " + e);
process.exit(1);
}
apiPageBuilder.buildAPIPage(schema, args.apiname, args.description, headPart)
.then(html => {
fs.writeFileSync(args.outputfile, html);
process.exit();
})
.catch(reason => {
console.error(reason);
process.exit(2);
});
}
if (command===url){
let query = graphql.introspectionQuery;
// if protocol is not specified, use http
if (!args.url.includes("://"))
args.url="http://"+args.url;
request(args.url, {
body: {
query: query
},
json: true,
method: 'POST'
},
(err, res, body)=>{
if (err) {
console.error("Could not retrieve introspection query: " + err);
process.exit(3);
}
let schema;
try{
schema=graphql.buildClientSchema(body.data);
}
catch(e) {
console.error("Could not build schema from introspection response: " + e);
process.exit(1);
}
apiPageBuilder.buildAPIPage(schema, args.apiname, args.description, headPart)
.then(html => {
fs.writeFileSync(args.outputfile, html);
process.exit();
})
.catch(reason => {
console.error(reason);
process.exit(2);
});
});
}