UNPKG

1.61 kBJavaScriptView Raw
1import * as inquirer from 'inquirer';
2import { getQuestions } from './questions';
3import { guessTargets } from './targets';
4import { Tags } from './types';
5import { writeConfig, writePackage, bold } from './helpers';
6function log(...msgs) {
7 // tslint:disable-next-line
8 console.log(...msgs);
9}
10export 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 // define config
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 // introspection
29 if (answers.introspection) {
30 addIntrospection(config);
31 }
32 // config file
33 const { relativePath } = writeConfig(answers, config);
34 // write package.json
35 writePackage(answers, relativePath);
36 // Emit status to the terminal
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// adds an introspection to `generates`
50function addIntrospection(config) {
51 config.generates['./graphql.schema.json'] = {
52 plugins: ['introspection']
53 };
54}
55//# sourceMappingURL=index.js.map
\No newline at end of file