UNPKG

13.2 kBSource Map (JSON)View Raw
1{"version":3,"file":"index.cjs.js","sources":["../../../dist/plugins/typescript/oclif/src/utils.js","../../../dist/plugins/typescript/oclif/src/visitor.js","../../../dist/plugins/typescript/oclif/src/index.js"],"sourcesContent":["export const getFlagConfigForVariableDefinition = (definition) => {\n const { list, required, innerType } = getInnerType(definition.type);\n const oclifType = mapVariableTypeToOclifType(innerType);\n const parser = getParserForType(innerType);\n return `${definition.variable.name.value}: flags.${oclifType}({\n multiple: ${list},\n required: ${required},${parser ? `\\n parse: ${parser}` : ''}\n})`;\n};\n// Supply a custom parser for oclif flag configuration\nconst getParserForType = (type) => {\n if (type.name.value === 'Float') {\n return 'input => Number(input)';\n }\n};\nconst mapVariableTypeToOclifType = (type) => {\n if (type.name.value === 'Boolean') {\n return 'boolean';\n }\n else if (['Float', 'Int'].includes(type.name.value)) {\n // A quirk of oclif is that \"integer\" allows for any `number`-typed response, and then\n // we supply our own parsing function to make sure it's a float and not an integer\n return 'integer';\n }\n else {\n return 'string';\n }\n};\n// Retrieve the inner type if nested within List and/or NonNull\nconst getInnerType = (type) => {\n const result = {\n list: false,\n required: false,\n };\n let _type = type;\n while (_type.kind !== 'NamedType') {\n if (_type.kind === 'ListType') {\n result.list = true;\n }\n else if (_type.kind === 'NonNullType') {\n result.required = true;\n }\n _type = _type.type;\n }\n result.innerType = _type;\n return result;\n};\n// remove all @oclif directives from the document for transmission to the server\nexport const omitOclifDirectives = (node) => {\n const directives = node.directives.filter(directive => directive.name.value !== 'oclif');\n return Object.assign({}, node, { directives });\n};\n//# sourceMappingURL=utils.js.map","import { ClientSideBaseVisitor, indentMultiline, } from '@graphql-codegen/visitor-plugin-common';\nimport autoBind from 'auto-bind';\nimport { print } from 'graphql';\nimport { getFlagConfigForVariableDefinition, omitOclifDirectives } from './utils';\nexport class GraphQLRequestVisitor extends ClientSideBaseVisitor {\n constructor(schema, fragments, rawConfig, info) {\n super(schema, fragments, rawConfig, {});\n this._operationsToInclude = [];\n this._info = info;\n const { handlerPath = '../../handler' } = rawConfig;\n // FIXME: This is taken in part from\n // presets/near-operation-file/src/index.ts:139. How do I build a path relative to the outputFile in the same way?\n // A plugin doesn't appear to have access to the same \"options.baseOutputDir\" that the preset does.\n // const absClientPath = resolve(info.outputFile, join(options.baseOutputDir, options.presetConfig.baseTypesPath));\n autoBind(this);\n this._additionalImports.push(`import { Command, flags } from '@oclif/command'`);\n this._additionalImports.push(`import handler from '${handlerPath}'`);\n }\n buildOperation(node, documentVariableName, operationType, operationResultType, operationVariablesTypes) {\n this._operationsToInclude.push({\n node,\n documentVariableName,\n operationType,\n operationResultType,\n operationVariablesTypes,\n });\n return null;\n }\n // Clean client-side content (ie directives) out of the GraphQL document prior to sending to the server\n get definition() {\n const operation = this._operationsToInclude[0];\n const clientOperation = print(omitOclifDirectives(operation.node));\n return `const ${operation.documentVariableName} = \\`\\n${clientOperation}\\``;\n }\n // Generate the code required for this CLI operation\n get cliContent() {\n if (this._operationsToInclude.length !== 1) {\n throw new Error(`Each graphql document should have exactly one operation; found ${this._operationsToInclude.length} while generating ${this._info.outputFile}.`);\n }\n const operation = this._operationsToInclude[0];\n // Find the @oclif directive in the client document, if it's there\n const directive = operation.node.directives.find(directive => directive.name.value === 'oclif');\n // Remap the directive's fields ie @oclif(description: \"a name\") to a more usable format\n const directiveValues = {};\n if (directive) {\n directiveValues.examples = [];\n directive.arguments.forEach(arg => {\n const value = 'value' in arg.value ? arg.value.value.toString() : null;\n const { value: name } = arg.name;\n if (name === 'description') {\n directiveValues.description = value;\n }\n else if (name === 'example') {\n directiveValues.examples.push(value);\n }\n else {\n throw new Error(`Invalid field supplied to @oclif directive: ${name}`);\n }\n });\n }\n const { description, examples } = directiveValues;\n const flags = operation.node.variableDefinitions.map(getFlagConfigForVariableDefinition);\n return `\n${this.definition}\n\nexport default class ${operation.node.name.value} extends Command {\n ${description ? `\\nstatic description = \"${description}\";\\n` : ''}\n ${examples ? `\\nstatic examples: string[] = ${JSON.stringify(examples)};\\n` : ''}\n static flags = {\n help: flags.help({ char: 'h' }),\n${indentMultiline(flags.join(',\\n'), 2)}\n };\n\n async run() {\n const { flags } = this.parse(${operation.node.name.value});\n await handler({ command: this, query: ${operation.documentVariableName}, variables: flags });\n }\n}\n`;\n }\n}\n//# sourceMappingURL=visitor.js.map","import { visit, concatAST, Kind } from 'graphql';\nimport { GraphQLRequestVisitor } from './visitor';\nimport { extname } from 'path';\nexport const plugin = (schema, documents, config, info) => {\n const allAst = concatAST(documents.reduce((prev, v) => {\n return [...prev, v.document];\n }, []));\n const allFragments = [\n ...allAst.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({\n node: fragmentDef,\n name: fragmentDef.name.value,\n onType: fragmentDef.typeCondition.name.value,\n isExternal: false,\n })),\n ...(config.externalFragments || []),\n ];\n const visitor = new GraphQLRequestVisitor(schema, allFragments, config, info);\n visit(allAst, { leave: visitor });\n return {\n prepend: visitor.getImports(),\n content: visitor.cliContent,\n };\n};\nexport const validate = async (schema, documents, config, outputFile) => {\n if (extname(outputFile) !== '.ts') {\n throw new Error(`Plugin \"typescript-oclif\" requires output file extensions to be \".ts\"!`);\n }\n};\nexport { GraphQLRequestVisitor };\n//# sourceMappingURL=index.js.map"],"names":["ClientSideBaseVisitor","print","indentMultiline","concatAST","Kind","visit","extname"],"mappings":";;;;;;;;;;;AAAO,MAAM,kCAAkC,GAAG,CAAC,UAAU,KAAK;AAClE,IAAI,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,YAAY,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;AACxE,IAAI,MAAM,SAAS,GAAG,0BAA0B,CAAC,SAAS,CAAC,CAAC;AAC5D,IAAI,MAAM,MAAM,GAAG,gBAAgB,CAAC,SAAS,CAAC,CAAC;AAC/C,IAAI,OAAO,CAAC,EAAE,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,SAAS,CAA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