UNPKG

4.31 kBJavaScriptView Raw
1import { grey } from './helpers';
2import { Tags } from './types';
3import { plugins } from './plugins';
4export function getQuestions(possibleTargets) {
5 return [
6 {
7 type: 'checkbox',
8 name: 'targets',
9 message: `What type of application are you building?`,
10 choices: getApplicationTypeChoices(possibleTargets)
11 },
12 {
13 type: 'input',
14 name: 'schema',
15 message: 'Where is your schema?:',
16 suffix: grey(' (path or url)'),
17 default: 'http://localhost:4000',
18 validate: (str) => str.length > 0
19 },
20 {
21 type: 'input',
22 name: 'documents',
23 message: 'Where are your operations and fragments?:',
24 when: answers => {
25 // flatten targets
26 // I can't find an API in Inquirer that would do that
27 answers.targets = normalizeTargets(answers.targets);
28 return answers.targets.includes(Tags.browser);
29 },
30 default: '**/*.graphql',
31 validate: (str) => str.length > 0
32 },
33 {
34 type: 'checkbox',
35 name: 'plugins',
36 message: 'Pick plugins:',
37 choices: getPluginChoices,
38 validate: (plugins) => plugins.length > 0
39 },
40 {
41 type: 'input',
42 name: 'output',
43 message: 'Where to write the output:',
44 default: 'src/generated/graphql.ts',
45 validate: (str) => str.length > 0
46 },
47 {
48 type: 'confirm',
49 name: 'introspection',
50 message: 'Do you want to generate an introspection file?'
51 },
52 {
53 type: 'input',
54 name: 'config',
55 message: 'How to name the config file?',
56 default: 'codegen.yml',
57 validate: (str) => {
58 const isNotEmpty = str.length > 0;
59 const hasCorrectExtension = ['json', 'yml', 'yaml'].some(ext => str.toLocaleLowerCase().endsWith(`.${ext}`));
60 return isNotEmpty && hasCorrectExtension;
61 }
62 },
63 {
64 type: 'input',
65 name: 'script',
66 message: 'What script in package.json should run the codegen?',
67 validate: (str) => str.length > 0
68 }
69 ];
70}
71export function getApplicationTypeChoices(possibleTargets) {
72 function withFlowOrTypescript(tags) {
73 if (possibleTargets.TypeScript) {
74 tags.push(Tags.typescript);
75 }
76 else if (possibleTargets.Flow) {
77 tags.push(Tags.flow);
78 }
79 else {
80 tags.push(Tags.flow, Tags.typescript);
81 }
82 return tags;
83 }
84 return [
85 {
86 name: 'Backend - API or server',
87 key: 'backend',
88 value: withFlowOrTypescript([Tags.node]),
89 checked: possibleTargets.Node
90 },
91 {
92 name: 'Application built with Angular',
93 key: 'angular',
94 value: [Tags.angular, Tags.browser, Tags.typescript],
95 checked: possibleTargets.Angular
96 },
97 {
98 name: 'Application built with React',
99 key: 'react',
100 value: withFlowOrTypescript([Tags.react, Tags.browser]),
101 checked: possibleTargets.React
102 },
103 {
104 name: 'Application built with Stencil',
105 key: 'stencil',
106 value: [Tags.stencil, Tags.browser, Tags.typescript],
107 checked: possibleTargets.Stencil
108 },
109 {
110 name: 'Application built with other framework or vanilla JS',
111 key: 'client',
112 value: [Tags.browser, Tags.typescript, Tags.flow],
113 checked: possibleTargets.Browser && !possibleTargets.Angular && !possibleTargets.React && !possibleTargets.Stencil
114 }
115 ];
116}
117export function getPluginChoices(answers) {
118 return plugins
119 .filter(p => p.available(answers.targets))
120 .map(p => {
121 return {
122 name: p.name,
123 value: p,
124 checked: p.shouldBeSelected(answers.targets)
125 };
126 });
127}
128function normalizeTargets(targets) {
129 return [].concat(...targets);
130}
131//# sourceMappingURL=questions.js.map
\No newline at end of file