UNPKG

@gqty/cli

Version:

## Visit [https://gqty.dev/docs/cli/codegen](https://gqty.dev/docs/cli/codegen)

8 lines (6 loc) 3.4 kB
import{promises as b}from"fs";import{createRequire as j}from"module";import{resolve as O}from"path";import*as h from"./deps.js";import{__innerState as $}from"./innerState.mjs";import{formatPrettier as S}from"./prettier.mjs";const u=globalThis.require||j(import.meta.url.toString());function f(i){return i!=null&&typeof i=="object"&&!Array.isArray(i)}function d(i){return f(i)&&Object.values(i).every(n=>typeof n=="string")}const y="SPECIFY_ENDPOINT_OR_SCHEMA_FILE_PATH_HERE",o={react:(()=>{try{return u.resolve("react"),!0}catch{}return!1})(),scalarTypes:{DateTime:"string"},introspection:{endpoint:y,headers:{}},endpoint:"/api/graphql",destination:"./src/gqty/index.ts",subscriptions:!1,javascriptOutput:!1,enumsAsStrings:!1,enumsAsConst:!1,preImport:""};function c(i,n,t,e){console.warn(`Warning, invalid config ${i}, got: ${JSON.stringify(n)}, expected ${t}. ${JSON.stringify(e)} used instead.`)}function m(i){if(f(i)){const n={};typeof i.javascriptOutput=="boolean"&&(n.javascriptOutput=i.javascriptOutput);for(const[t,e]of Object.entries(i))if(e!==void 0)switch(t){case"destination":case"preImport":{typeof e=="string"?n[t]=e:c(t,e,"string",o[t]);break}case"javascriptOutput":case"react":case"subscriptions":case"enumsAsStrings":{typeof e=="boolean"?n[t]=e:c(t,e,"boolean",o[t]);break}case"enumsAsConst":{typeof e=="boolean"?n[t]=e:c(t,e,"boolean",o[t]);break}case"scalarTypes":{d(e)?n[t]=e:c(t,e,'"object of strings"',o[t]);break}case"endpoint":{e&&typeof e=="string"?n[t]=e:c(t,e,"string",o[t]);break}case"introspection":{if(f(e)){const a={};for(const[r,s]of Object.entries(e))if(s!==void 0)switch(r){case"endpoint":{typeof s=="string"?a[r]=s:c(`${t}.${r}`,s,"string",o.introspection.endpoint);break}case"headers":{d(s)?a[r]=s:c(`${t}.${r}`,s,'"object of strings"',o.introspection.headers);break}default:console.warn(`Warning, invalid and unused config property "${t}.${r}": ${JSON.stringify(e)}`)}n[t]=a}else c(t,e,"object",o[t]);break}case"introspections":{if(f(e)){const a={};for(const[r,s]of Object.entries(e))f(s)?a[r]=s:c(`${t}.${r}`,s,'"object of strings"',{});n[t]=a}else c(t,e,"object",o[t]);break}case"transformSchema":{typeof e=="function"?n[t]=e:c(t,e,"function","undefined");break}default:console.warn(`Warning, invalid and unused config property "${t}": ${JSON.stringify(e)}`)}return n}else return console.warn("Invalid config, using instead: "+JSON.stringify(o,null,2)),o}const l=O(process.cwd(),"gqty.config.cjs"),w={filepath:l,config:o};let g;const N=async({writeConfigFile:i=!1}={})=>(g===void 0&&(g=new Promise(async n=>{try{const t=a=>u(a),e=await h.cosmiconfig("gqty",{searchPlaces:["gqty.config.cjs","gqty.config.js","package.json"],loaders:{".cjs":t,".js":t}}).search();if(!e||e.isEmpty){const a=(e==null?void 0:e.filepath)||l,r=process.env.NODE_ENV;if(r!=="test"&&r!=="production"&&$.isCLI){const{format:s}=S({parser:"typescript"}),p={...o};delete p.preImport,delete p.enumsAsStrings,i&&await b.writeFile(l,await s(` /** * @type {import("@gqty/cli").GQtyConfig} */ const config = ${JSON.stringify(p)}; module.exports = config;`))}return n({filepath:a,config:o})}n({config:m(e.config),filepath:e.filepath})}catch(t){console.error(t),n(w)}})),g);export{y as DUMMY_ENDPOINT,o as defaultConfig,m as getValidConfig,N as loadOrGenerateConfig};