UNPKG

@graphql-codegen/cli

Version:

<p align="center"> <img src="https://github.com/dotansimha/graphql-code-generator/blob/master/logo.png?raw=true" /> </p>

148 lines (147 loc) 5.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOutputDefaultValue = exports.getPluginChoices = exports.getApplicationTypeChoices = exports.getQuestions = void 0; const helpers_js_1 = require("./helpers.js"); const types_js_1 = require("./types.js"); const plugins_js_1 = require("./plugins.js"); function getQuestions(possibleTargets) { return [ { type: 'checkbox', name: 'targets', message: `What type of application are you building?`, choices: getApplicationTypeChoices(possibleTargets), validate: ((targets) => targets.length > 0), }, { type: 'input', name: 'schema', message: 'Where is your schema?:', suffix: (0, helpers_js_1.grey)(' (path or url)'), default: 'http://localhost:4000', validate: (str) => str.length > 0, }, { type: 'input', name: 'documents', message: 'Where are your operations and fragments?:', when: answers => { // flatten targets // I can't find an API in Inquirer that would do that answers.targets = normalizeTargets(answers.targets); return answers.targets.includes(types_js_1.Tags.browser); }, default: 'src/**/*.graphql', validate: (str) => str.length > 0, }, { type: 'checkbox', name: 'plugins', message: 'Pick plugins:', choices: getPluginChoices, validate: ((plugins) => plugins.length > 0), }, { type: 'input', name: 'output', message: 'Where to write the output:', default: getOutputDefaultValue, validate: (str) => str.length > 0, }, { type: 'confirm', name: 'introspection', message: 'Do you want to generate an introspection file?', }, { type: 'input', name: 'config', message: 'How to name the config file?', default: 'codegen.yml', validate: (str) => { const isNotEmpty = str.length > 0; const hasCorrectExtension = ['json', 'yml', 'yaml'].some(ext => str.toLocaleLowerCase().endsWith(`.${ext}`)); return isNotEmpty && hasCorrectExtension; }, }, { type: 'input', name: 'script', message: 'What script in package.json should run the codegen?', validate: (str) => str.length > 0, }, ]; } exports.getQuestions = getQuestions; function getApplicationTypeChoices(possibleTargets) { function withFlowOrTypescript(tags) { if (possibleTargets.TypeScript) { tags.push(types_js_1.Tags.typescript); } else if (possibleTargets.Flow) { tags.push(types_js_1.Tags.flow); } else { tags.push(types_js_1.Tags.flow, types_js_1.Tags.typescript); } return tags; } return [ { name: 'Backend - API or server', key: 'backend', value: withFlowOrTypescript([types_js_1.Tags.node]), checked: possibleTargets.Node, }, { name: 'Application built with Angular', key: 'angular', value: [types_js_1.Tags.angular, types_js_1.Tags.browser, types_js_1.Tags.typescript], checked: possibleTargets.Angular, }, { name: 'Application built with React', key: 'react', value: withFlowOrTypescript([types_js_1.Tags.react, types_js_1.Tags.browser]), checked: possibleTargets.React, }, { name: 'Application built with Stencil', key: 'stencil', value: [types_js_1.Tags.stencil, types_js_1.Tags.browser, types_js_1.Tags.typescript], checked: possibleTargets.Stencil, }, { name: 'Application built with other framework or vanilla JS', key: 'client', value: [types_js_1.Tags.browser, types_js_1.Tags.typescript, types_js_1.Tags.flow], checked: possibleTargets.Browser && !possibleTargets.Angular && !possibleTargets.React && !possibleTargets.Stencil, }, ]; } exports.getApplicationTypeChoices = getApplicationTypeChoices; function getPluginChoices(answers) { return plugins_js_1.plugins .filter(p => p.available(answers.targets)) .map(p => { return { name: p.name, value: p, checked: p.shouldBeSelected(answers.targets), }; }); } exports.getPluginChoices = getPluginChoices; function normalizeTargets(targets) { return [].concat(...targets); } function getOutputDefaultValue(answers) { if (answers.plugins.some(plugin => plugin.defaultExtension === '.tsx')) { return 'src/generated/graphql.tsx'; } if (answers.plugins.some(plugin => plugin.defaultExtension === '.ts')) { return 'src/generated/graphql.ts'; } return 'src/generated/graphql.js'; } exports.getOutputDefaultValue = getOutputDefaultValue;