UNPKG

@gqty/cli

Version:

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

2 lines (1 loc) 1.6 kB
import{existsSync as n,promises as a}from"fs";import{resolve as f,dirname as u}from"path";import{defaultConfig as g}from"./config.mjs";import*as v from"./deps.js";import{generate as x}from"./generate.mjs";async function y({destinationPath:r,clientCode:e,onExistingFileConflict:t}){if(n(r)){if(t){const c=await a.readFile(r,{encoding:"utf-8"});await t(c)}return}await a.writeFile(r,e,{encoding:"utf-8"})}function C(...r){return Promise.all(r.map(e=>e()))}async function F({schemaCode:r,destinationPath:e,isJavascriptOutput:t,javascriptSchemaCode:c}){await C(async()=>{const i=f(u(e),t?"./schema.generated.d.ts":"./schema.generated.ts");n(i)&&await a.readFile(i,{encoding:"utf-8"})===r||await a.writeFile(i,r,{encoding:"utf-8"})},async()=>{if(t){const i=f(u(e),"./schema.generated.js");if(n(i)&&await a.readFile(i,{encoding:"utf-8"})===c)return;await a.writeFile(i,c,{encoding:"utf-8"})}})}async function m(r,e,t,c,i){var s;const w=(s=t.javascriptOutput)!=null?s:g.javascriptOutput;if(w){if(!e.endsWith(".js")){const o=Error('You have to specify the ".js" extension, instead, it received: "'+e+'"');throw Error.captureStackTrace(o,m),o}}else if(!e.endsWith(".ts")){const o=Error('You have to specify the ".ts" extension, instead, it received: "'+e+'"');throw Error.captureStackTrace(o,m),o}e=f(e);const[{clientCode:d,schemaCode:l,javascriptSchemaCode:p}]=await Promise.all([x(r,t,i),v.mkdirp(u(e))]);return await Promise.all([y({clientCode:d,destinationPath:e,onExistingFileConflict:c}),F({schemaCode:l,destinationPath:e,isJavascriptOutput:w,javascriptSchemaCode:p})]),e}export{m as writeGenerate};