UNPKG

toes

Version:

14 lines (12 loc) 2.49 kB
#!/usr/bin/env node import R from"meow";import v from"express";import b from"fs";import{homedir as k}from"os";import{promisify as w}from"util";import{exec as h}from"child_process";import y from"fs";import P from"os";var $=`${k()}/.toad/srv-config`;function x(){try{let o=JSON.parse(y.readFileSync($).toString());return o.token?(o.dir=o.dir||P.homedir()+"/projects",o.port=o.port||3535,g(o),o):void 0}catch{return}}function g(o){y.writeFileSync($,JSON.stringify(o))}var s=(...o)=>h(o[0],o[1],(t,n,e)=>{if(t){console.error(t);return}n&&console.log(n),e&&console.error(e)}),D=w(h);var u,c=[],i=v();i.use(v.json());i.use((o,t,n)=>{if(o.headers.authorization!==u.token){t.status(401).send("Unauthorized");return}n()});i.post("/up",async(o,t)=>{let{project:n,config:e}=o.body,{domain:S,port:C}=e,{remote:m}=n;if(!S||!C){t.status(400).json({error:"Missing domain or port"});return}if(!m){t.status(400).json({error:"Missing remote"});return}let p=n.remote.split("/").pop().split(".").shift(),a=`${u.dir}/${p}`;if(b.existsSync(a))s(`cd ${a}`),s("git pull");else try{console.log(`Cloning ${m} into ${a}`),s(`git clone ${m} ${a}`),s(`cd ${a}`)}catch(r){console.log(r)}if(e.install&&(console.log(`Installing dependencies for ${p} with ${e.install}`),s(e.install)),e.build&&(console.log(`Building ${p} with ${e.build}`),s(e.build)),e.start){console.log(`Starting ${p} with ${e.start}`);let r=s(e.start);r.on("spawn",()=>{c.push({pid:r.pid,name:p,command:r.spawnargs.join(" ")})}),r.on("exit",()=>{c.splice(c.findIndex(d=>d.pid===r.pid),1)}),r.on("error",d=>{console.log(d)})}t.status(200).json({success:!0})});i.get("/ps",(o,t)=>t.json(c));i.get("/",(o,t)=>t.send("hi"));var j={run:o=>{u=o,i.listen(o.port,()=>{console.log(`toad running on port ${o.port}`)})}};import{homedir as q}from"os";var f=R(` Usage $ toad-server --port 3000 --token 1234 --dir /home/user/projects Options --port, -p Port to run the server on --token, -t Token to authenticate requests with --dir, -d Directory to watch for projects Examples $ toad-server --port 3000 --token 1234 --dir /home/user/projects `,{importMeta:import.meta,flags:{port:{type:"string",alias:"p",default:"3535"},token:{type:"string",alias:"t"},dir:{type:"string",alias:"d",default:q()+"/projects"}},autoHelp:!0}),l=x();l||(f.flags.token||(console.log("No token provided"),process.exit(1)),l={port:f.flags.port,token:f.flags.token,dir:f.flags.dir},g(l));j.run(l); //# sourceMappingURL=index.mjs.map