/
main.js
executable file
·158 lines (145 loc) · 4.37 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import yargs from 'yargs';
import '@babel/polyfill';
import path from 'path';
import fs from 'fs';
import {CodegenRunner, ConsoleReporter, DotGraphQLParser} from 'graphql-compiler';
import RelayJSModuleParser from 'relay-compiler/lib/RelayJSModuleParser';
import {getFilepathsFromGlob, getRelayFileWriter, getSchema} from './ripped';
import {clean} from './utils';
import {graphqlJSCompiler} from 'relay-compiler-plus/graphqlJSCompiler'; //eslint-disable-line
import {queryMap, persistQuery} from './persistQuery';
/*
* Most of the code in this run method are ripped from:
* relay-compiler/bin/RelayCompilerBin.js
*/
const run = async (options: { schema: string, src: string, webpackConfig: string, extensions: Array<string> }) => {
const srcDir = path.resolve(process.cwd(), options.src);
console.log(`src: ${srcDir}`);
let schemaPath;
let customWebpackConfig;
if (options.webpackConfig) {
customWebpackConfig = path.resolve(process.cwd(), options.webpackConfig);
} else {
schemaPath = path.resolve(process.cwd(), options.schema);
}
if ((schemaPath && path.extname(schemaPath) === '.js') || customWebpackConfig) {
console.log(`schemaPath: ${schemaPath}, customWebpackConfig: ${customWebpackConfig}`);
schemaPath = await graphqlJSCompiler(schemaPath, srcDir, customWebpackConfig);
}
console.log(`schemaPath: ${schemaPath}`);
clean(srcDir);
const reporter = new ConsoleReporter({
verbose: options.verbose,
quiet: options.quiet,
});
const schema = getSchema(schemaPath);
const parserConfigs = {
js: {
baseDir: srcDir,
getFileFilter: RelayJSModuleParser.getFileFilter,
getParser: RelayJSModuleParser.getParser,
getSchema: () => schema,
filepaths: getFilepathsFromGlob(srcDir, {
extensions: options.extensions,
include: options.include,
exclude: options.exclude,
}),
},
graphql: {
baseDir: srcDir,
getParser: DotGraphQLParser.getParser,
getSchema: () => schema,
filepaths: getFilepathsFromGlob(srcDir, {
extensions: ['graphql'],
include: options.include,
exclude: options.exclude,
}),
},
};
const writerConfigs = {
js: {
getWriter: getRelayFileWriter(srcDir, persistQuery),
isGeneratedFile: (filePath: string) => filePath.endsWith('.js') && filePath.includes('__generated__'),
parser: 'js',
baseParsers: ['graphql'],
},
};
const codegenRunner = new CodegenRunner({
reporter,
parserConfigs,
writerConfigs,
onlyValidate: false,
sourceControl: null,
});
let result = '';
try {
// the real work is done here
result = await codegenRunner.compileAll();
} catch (err) {
console.log(`Error codegenRunner.compileAll(): ${err}`);
throw err;
}
const queryMapOutputFile = `${srcDir}/queryMap.json`;
try {
fs.writeFileSync(queryMapOutputFile, JSON.stringify(queryMap, null, 2));
console.log(`Query map written to: ${queryMapOutputFile}`);
} catch (err) {
if (err) {
return console.log(err);
}
}
console.log(`Done! ${result}`);
};
// Collect args
const argv = yargs // eslint-disable-line prefer-destructuring
.usage('Usage: $0 --schema <schemaPath> --src <srcDir>')
.options({
schema: {
describe: 'Path to schema.js or schema.graphql or schema.json',
demandOption: false,
type: 'string',
},
src: {
describe: 'Root directory of application code',
demandOption: true,
type: 'string',
},
webpackConfig: {
describe: 'Custom webpack config to compile graphql-js',
demandOption: false,
type: 'string',
},
extensions: {
array: true,
default: ['js', 'jsx'],
describe: 'File extensions to compile (--extensions js jsx)',
type: 'string',
},
include: {
array: true,
default: ['**'],
describe: 'Directories to include under src',
type: 'string',
},
exclude: {
array: true,
default: [
'**/node_modules/**',
'**/__mocks__/**',
'**/__tests__/**',
'**/__generated__/**',
],
describe: 'Directories to ignore under src',
type: 'string',
},
})
.help().argv;
(async () => {
console.log('Welcome to relay-compiler-plus. Compiling now with these parameters:');
try {
await run(argv);
} catch (err) {
console.log(`error: ${err}`);
process.exit(1);
}
})();