forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess-gql.js
44 lines (35 loc) · 1.27 KB
/
preprocess-gql.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
const fs = require('fs');
const minimist = require('minimist');
const gqlLoader = require('graphql-tag/loader');
function printUsage(consoleFn, exitCode) {
consoleFn(`Usage: bin/preprocess-gql --in [filename] --out [filename]
Preprocess a file containing GraphQL queries into a *.js module consistently
with the way that Webpack will transpile it at build time.
Options:
--help, -h Display this message.
--in, -i [filename] Read GraphQL queries from the file at [filename].
--out, -o [filename] Emit JavaScript source to the file at [filename].
`);
process.exit(exitCode);
}
const argv = minimist(process.argv.slice(2), {
string: ['in', 'out'],
boolean: ['help'],
alias: { h: 'help', i: 'in', o: 'out' },
unknown: param => {
console.error(`Unrecognized command-line argument: ${param}\n`);
printUsage(console.error, 1);
},
});
if (argv.help) {
printUsage(console.log, 0);
}
const inFilename = argv.in;
const outFilename = argv.out;
if (!inFilename || !outFilename) {
console.error('Both --in and --out parameters are required.\n');
printUsage(console.error, 1);
}
const querySource = fs.readFileSync(inFilename, { encoding: 'utf8' });
const jsSource = gqlLoader.call({ cacheable() {} }, querySource);
fs.writeFileSync(outFilename, jsSource, { encoding: 'utf8' });