1 | import * as inquirer from 'inquirer';
|
2 | import { getQuestions } from './questions';
|
3 | import { guessTargets } from './targets';
|
4 | import { Tags } from './types';
|
5 | import { writeConfig, writePackage, bold } from './helpers';
|
6 | function log(...msgs) {
|
7 |
|
8 | console.log(...msgs);
|
9 | }
|
10 | export async function init() {
|
11 | log(`
|
12 | Welcome to ${bold('GraphQL Code Generator')}!
|
13 | Answer few questions and we will setup everything for you.
|
14 | `);
|
15 | const possibleTargets = await guessTargets();
|
16 | const answers = await inquirer.prompt(getQuestions(possibleTargets));
|
17 |
|
18 | const config = {
|
19 | overwrite: true,
|
20 | schema: answers.schema,
|
21 | documents: answers.targets.includes(Tags.browser) ? answers.documents : null,
|
22 | generates: {
|
23 | [answers.output]: {
|
24 | plugins: answers.plugins.map(p => p.value)
|
25 | }
|
26 | }
|
27 | };
|
28 |
|
29 | if (answers.introspection) {
|
30 | addIntrospection(config);
|
31 | }
|
32 |
|
33 | const { relativePath } = writeConfig(answers, config);
|
34 |
|
35 | writePackage(answers, relativePath);
|
36 |
|
37 | log(`
|
38 | Config file generated at ${bold(relativePath)}
|
39 |
|
40 | ${bold('$ npm install')}
|
41 |
|
42 | To install the plugins.
|
43 |
|
44 | ${bold(`$ npm run ${answers.script}`)}
|
45 |
|
46 | To run GraphQL Code Generator.
|
47 | `);
|
48 | }
|
49 |
|
50 | function addIntrospection(config) {
|
51 | config.generates['./graphql.schema.json'] = {
|
52 | plugins: ['introspection']
|
53 | };
|
54 | }
|
55 |
|
\ | No newline at end of file |