UNPKG

@getcronit/pylon-dev

Version:

CLI for creating a Pylon

22 lines (15 loc) 3.88 kB
#!/usr/bin/env node import{build as g}from"@getcronit/pylon-builder";import{fetchSchema as b,generateClient as w}from"@gqty/cli";import{program as p}from"commander";import e from"consola";import S from"path";var f="1.0.4-canary-20250311173056.573ae5085c663f87cbd7704d3975323a23288851";import{spawn as C}from"child_process";import y from"treekill";import*as a from"@getcronit/pylon-telemetry";import x from"dotenv";x.config();p.name("pylon-dev").description("Pylon Development CLI").version(f);p.command("build").description("Build the Pylon Schema").action(async()=>{e.start("[Pylon]: Building schema");let{totalFiles:n,totalSize:d,duration:t}=await g({sfiFilePath:"./src/index.ts",outputFilePath:"./.pylon"});await a.sendBuildEvent({duration:t,totalFiles:n,totalSize:d,isDevelopment:!1}),e.success("[Pylon]: Schema built")});p.name("dev").option("-c, --command <command>","Command to run the server","bun run .pylon/index.js").option("--client","Generate the client from the server's schema").option("--test","Test").option("--client-path <clientPath>","Path to generate the client to","gqty/index.ts").option("--client-port <clientPort>","Port of the pylon server to generate the client from","3000").action(I);var k=Date.now();async function I(n,d){e.log(`[Pylon]: ${d.name()} version ${d.version()}`);let t=null,h=async(i=!1)=>{t&&(t.removeAllListeners(),y(t.pid,"SIGINT",o=>{o&&e.error(o)}));let[r,...s]=n.command.split(" ");if(t=C(r,s,{shell:!0,stdio:"inherit",env:{...process.env,NODE_ENV:"development"}}),t.on("exit",o=>{o===0&&(e.success("Pylon server stopped"),process.exit(0)),e.error(`Pylon exited with code ${o}, fix the error and save the file to restart the server`)}),i&&n.client&&n.clientPath&&n.clientPort){let o=S.resolve(process.cwd(),n.clientPath),l=`http://localhost:${n.clientPort}/graphql`;console.log("Generating client...",l);let u=async()=>{e.start("[Pylon]: Fetching schema from server");let P=await b(l,{silent:!0});e.success("[Pylon]: Schema fetched"),e.start("[Pylon]: Generating client"),await w(P,{endpoint:l,destination:o,react:!0,scalarTypes:{Number:"number",Object:"Record<string, unknown>"}}),e.success("[Pylon]: Client generated")},m=0,c=async()=>{try{await u()}catch{m++,m<5&&setTimeout(()=>{c()},1e3)}};c()}};e.start("[Pylon]: Building schema");try{let{duration:i,totalFiles:r,totalSize:s}=await g({sfiFilePath:"./src/index.ts",outputFilePath:"./.pylon",watch:!0,onWatch:async({schemaChanged:o,totalFiles:l,totalSize:u,duration:m})=>{let c=t!==null;c?e.start("[Pylon]: Reloading server"):e.start("[Pylon]: Starting server"),await h(o),c?e.ready("[Pylon]: Server reloaded"):(e.ready("[Pylon]: Server started"),e.box(` Pylon is up and running! Press \`Ctrl + C\` to stop the server. Encounter any issues? Report them here: https://github.com/getcronit/pylon/issues We value your feedback\u2014help us make Pylon even better! `)),o&&(e.info("[Pylon]: Schema updated"),await a.sendBuildEvent({duration:m,totalFiles:l,totalSize:u,isDevelopment:!0}))}});await a.sendBuildEvent({duration:i,totalFiles:r,totalSize:s,isDevelopment:!0}),e.success("[Pylon]: Schema built"),e.start("[Pylon]: Starting server"),await h(!0),e.ready("[Pylon]: Server started"),e.box(` Pylon is up and running! Press \`Ctrl + C\` to stop the server. Encounter any issues? Report them here: https://github.com/getcronit/pylon/issues We value your feedback\u2014help us make Pylon even better! `)}catch(i){e.error("[Pylon]: Couldn't build schema",i);let r=t;r&&(r.removeAllListeners(),y(r.pid,"SIGINT",s=>{s&&e.error(s)}))}process.on("SIGINT",async i=>{try{t&&(t.removeAllListeners(),y(t.pid,"SIGINT",r=>{r&&e.error(r)}))}catch{}finally{await a.sendDevEvent({duration:Date.now()-k,clientPath:n.clientPath,clientPort:parseInt(n.clientPort)}),process.exit(0)}})}p.parse(); //# sourceMappingURL=index.js.map