-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex.js
executable file
·95 lines (85 loc) · 2.51 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
#!/usr/bin/env node
'use strict';
const isUrl = require('is-url');
const program = require('commander');
// file I/O helpers
const fileIO = require('./util/fileIO');
// Interface Utils
const interfaceUtils = require('./util/interface');
// Module Utils
const moduleUtils = require('./util/module');
// Fetch utils
const fetchUtils = require('./util/fetcher');
program
.version('0.4.4')
.usage('[options] <schema.json>')
.option('-e --export', 'use export rather than declare to define types')
.option(
'-o --output-file [outputFile]',
'name for ouput file, defaults to graphql-export.flow.js',
'graphql-export.flow.js'
)
.option('--null-keys [nullKeys]', 'makes all keys nullable', false)
.option('--null-values [nullValues]', 'makes all values nullable', false)
.option(
'-m --module-name [moduleName]',
'name for the export module. Types are not wrapped in a module if this is not set',
''
)
.option(
'-t --typeMap <typeSpec>',
'Define custom scalar types where typeSpec is <graphql type>:<flow type>',
(val, typeMap) => {
const [graphqlType, flowType] = val.split(':');
if (!graphqlType || !flowType) {
throw new Error(
'-t argument format should be <graphql type>:<flow type>'
);
}
typeMap[graphqlType] = flowType;
return typeMap;
},
{}
)
.option(
'-i --ignored-types <ignoredTypes>',
'names of types to ignore (comma delimited)',
v => v.split(','),
[]
)
.option(
'-w --whitelist <whitelist>',
'names of types to whitelist (comma delimited)',
v => v.split(','),
[]
)
.action((path, options) => {
const getSchema = new Promise((resolve, reject) => {
if (isUrl(path)) {
fetchUtils.fetchWithIntrospection(path, (err, schema) => {
if (err) {
reject(err);
} else if (!schema.data) {
reject(
new Error('Server replied with an invalid introspection schema')
);
} else {
resolve(schema);
}
});
} else {
resolve(fileIO.readFile(path));
}
});
getSchema
.then(schema => {
let interfaces = interfaceUtils.generateTypes(schema, options);
let module = moduleUtils.generateModule(options.moduleName, interfaces);
moduleUtils.writeModuleToFile(options.outputFile, module);
})
.catch(err => console.error(err.message));
})
.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp();
}