UNPKG

@gqty/cli

Version:

Interactive codegen for GQty

6 lines (5 loc) 3.3 kB
#! /usr/bin/env node import{existsSync as q,promises as C}from"fs";import*as S from"graphql";import{resolve as u,extname as h}from"path";import{defaultConfig as r,loadOrGenerateConfig as E,DUMMY_ENDPOINT as w}from"./config.mjs";import"@commander-js/extra-typings";import"@graphql-codegen/core";import"@graphql-codegen/typescript";import"@graphql-tools/utils";import"@graphql-tools/wrap";import"@inquirer/prompts";import"cosmiconfig";import"cross-fetch";import O from"fast-glob";import"lodash-es/sortBy.js";import"prettier";import{getRemoteSchema as k}from"./introspection.mjs";import{writeGenerate as x}from"./writeGenerate.mjs";const{buildClientSchema:R,buildSchema:N}=S;async function W({endpoint:t,destination:o,generateOptions:y,cli:b,headers:d,transformSchemaOptions:j}={}){o&&(r.destination=o);const{config:n,filepath:g}=await E({writeConfigFile:!0});if(t)r.introspection.endpoint=t;else if(q(u("./schema.gql")))t="./schema.gql",r.introspection.endpoint=t;else{const e=n.introspection&&n.introspection.endpoint;if(e&&e!==w)t=e;else throw console.error(` Please modify "${g.endsWith("package.json")?"gqty":"config"}.introspection.endpoint" in: "${g}".`),Error("ERROR: No introspection endpoint specified in configuration file.")}o||(o=n.destination||r.destination),o=u(o);const a=Object.assign({},n,y);let l;if(r.introspection.endpoint=t,r.introspection.headers=d||{},t.startsWith("http://")||t.startsWith("https://"))l=await k(t,{headers:d});else{r.introspection.endpoint=w;const e=await O(t);if(e.length){const m=[".graphql",".gql"],s=e.filter(i=>h(i)===".json"),f=e.filter(i=>m.includes(h(i)));if(e.length>s.length+f.length)throw Error("Received invalid files. Type generation can only use .gql, .graphql or .json files");if(s.length&&f.length)throw Error("Received mixed file inputs. Can not combine JSON and GQL files");if(s.length&&s.length>1)throw Error("Received multiple JSON introspection files, shoud only be one");const c=await Promise.all(e.map(i=>C.readFile(i,{encoding:"utf-8"})));if(h(e[0])===".json"){const i=JSON.parse(c[0]);let p;if(typeof i=="object"&&("data"in i&&i.data?p=i.data:"__schema"in i&&(p=i)),typeof p!="object")throw Error('Invalid JSON introspection result, expected "__schema" or "data.__schema" field.');l=R(p)}else l=N(c.join(` `))}else throw Error(`File "${t}" doesn't exists. If you meant to inspect a GraphQL API, make sure to put http:// or https:// in front of it.`)}const v=await x(l,o,a,async e=>{const m=a.subscriptions??n.subscriptions,s=a.frameworks?.includes("react")??n.frameworks?.includes("react")??a.react??n.react??!1,f=a.frameworks?.includes("solid-js")??n.frameworks?.includes("solid-js")??!1,c=` If you meant to change this, please remove "${o}" and re-run code generation.`;m&&(e.includes("createSubscriptionsClient")||console.warn(`[Warning] You've changed the option "subscriptions" to 'true', which is different from your existing "${o}".`+c)),s&&(e.includes("createReactClient")||console.warn(`[Warning] You've included "react" in frameworks, which is different from your existing "${o}".`+c)),f&&(e.includes("createSolidClient")||console.warn(`[Warning] You've included "solid-js" in frameworks, which is different from your existing "${o}".`+c))},j);b&&console.log("Code generated successfully at "+v)}export{W as inspectWriteGenerate};