UNPKG

bit-ship

Version:

Bit-Ship CLI is tool that analyses your code and generates a custom environment for your needs You can use if to local development, CI/CD or even production.

2 lines (1 loc) 8.41 kB
import{defineCommand as t,runMain as o}from"citty";import e,{consola as i}from"consola";import*as n from"@sentry/node";import{createStorage as r}from"unstorage";import s from"unstorage/drivers/fs";import a from"os";import{parseYAML as d,stringifyYAML as c}from"confbox";import{readFile as p,mkdir as m,writeFile as l}from"fs/promises";import{writeFile as u}from"unstorage/drivers/utils/node-fs";import h from"joi";import{ofetch as g}from"ofetch";import{v4 as f}from"uuid";import{fileURLToPath as v}from"url";import{dirname as y}from"path";import b from"fs";import*as j from"node:path";import{exec as $,spawn as S}from"child_process";function w(t,o,e,i){return new(e||(e=Promise))((function(n,r){function s(t){try{d(i.next(t))}catch(t){r(t)}}function a(t){try{d(i.throw(t))}catch(t){r(t)}}function d(t){var o;t.done?n(t.value):(o=t.value,o instanceof e?o:new e((function(t){t(o)}))).then(s,a)}d((i=i.apply(t,o||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const k=a.homedir();let D;const C=()=>(D||e.error("Storage not initialized"),D),x=".bit-ship";let I={};const O=()=>({getConfig:()=>I,getConfigPath:(t,o)=>function(t,o,e){const i=Array.isArray(o)?o:o.match(/[^.[\]]+/g)||[],n=i.reduce(((t,o)=>t&&void 0!==t[o]?t[o]:void 0),t);return void 0===n?e:n}(I,t,o),setConfig:_,loadConfig:L});function _(t){return w(this,void 0,void 0,(function*(){e.start("Updating bit-ship.yml"),I=Object.assign(Object.assign({},I),t),yield m("path",{recursive:!0}),u(`${x}/bit-ship.yml`,c(I))}))}function L(){return w(this,void 0,void 0,(function*(){try{const t=yield p(`${x}/bit-ship.yml`,"utf8");I=d(t);const o=U.validate(I);if(o.error)return void e.error("Invalid bit-ship.yml file",o.error)}catch(t){e.warn("No bit-ship.yml file found. You can create it manually or run `bit-ship init` to create a default one.")}}))}const P=h.object({on:h.object({commit:h.object({on:h.string().valid("pre-commit","post-commit")})}),tasks:h.array().items(h.string()).required()}),R=h.object({script:h.string().required(),location:h.string().optional(),env:h.object().pattern(h.string(),h.string()).optional(),volumes:h.array().items(h.string()).optional()}).optional(),F=h.object({name:h.string().required(),build:h.string(),dependencies:h.object().optional()}).optional(),N=h.object({expose:h.array().items(h.object({localHost:h.string().optional(),port:h.number(),access:h.string().valid("public","proxy-public","internal")})).optional(),task:h.string().required()}).optional(),U=h.object({version:h.string().optional(),name:h.string().optional(),images:h.object().pattern(h.string(),F).optional(),tasks:h.object().pattern(h.string(),R).optional(),jobs:h.object().pattern(h.string(),P).optional(),apps:h.object().pattern(h.string(),N).optional()}),E=()=>({apiURL:process.env.API_URL||"https://app.bit-ship.dev/api"}),{apiURL:M}=E();function q(t,o){return w(this,void 0,void 0,(function*(){try{yield g(`${M}/analytics/cli/${t}`,{method:"POST",body:o,headers:{"Content-Type":"application/json"}})}catch(t){e.log("Failed to send analytics event",t)}}))}const B=t=>{const o=new Date(t),e=new Date;return o.getDate()===e.getDate()&&o.getMonth()===e.getMonth()&&o.getFullYear()===e.getFullYear()},Y=()=>({runContainer:A,removeContainer:T,stopContainer:z}),A=t=>w(void 0,void 0,void 0,(function*(){return new Promise((o=>{const e=H("",t);let i={};t.detach||(i=Object.assign(Object.assign({},i),{stdio:"inherit",shell:!0}));const n=S("docker",function(t){var o,e;const i=t.containerName?["--name",t.containerName]:"",n=(null==t?void 0:t.env)?function(t){return Object.entries(t).map((([t,o])=>`-e ${t}=${o}`))}(t.env):[],r=(null===(o=t.volumes)||void 0===o?void 0:o.reduce(((t,o)=>(t.push("-v",o),t)),[]))||[],s=t.platform?["--platform",t.platform]:[],a=t.restart?["--restart",t.restart]:[],d=t.remove?["--rm"]:[],c=(null===(e=t.ports)||void 0===e?void 0:e.reduce(((t,o)=>(t.push("-p",o),t)),[]))||[],p=t.detach?["-d"]:["-it"],m=["run",...d,...i,"-w","/app",...n,...r,...s,...a,...c,...p,t.image,...t.script.split(" ")];return m}(t),i);t.onSpawn&&t.onSpawn(),e("-------------------------- Running task","start",!1),t.detach&&(n.stdout.on("data",(t=>e(`${t}`,"log"))),n.stderr.on("data",(t=>e(`${t}`,"log"))),n.on("close",(t=>{o(!0),e(`--------------------------/ Task finished code: ${t}`,"success",!1)}))),function(t,o){if(t.storeLogs){const e=new Date,i=`${e.getFullYear()}-${e.getMonth()+1}-${e.getDate()}_${e.getHours()}-${e.getMinutes()}`,n=".bit-ship/logs";b.existsSync(n)||b.mkdirSync(n,{recursive:!0}),l(`${n}/${i}_${t.taskName}.log`,o)}}(t,"")}))}));function T(t){return w(this,void 0,void 0,(function*(){return new Promise((o=>{$(`docker stop ${t} && docker rm ${t}`,(()=>{o(!0)}))}))}))}function z(t){return new Promise((o=>{$(`docker stop ${t}`,(()=>{o(!0)}))}))}const H=(t,o)=>o.silentLog?(t,o,e=!0)=>{}:(t,o,i=!0)=>{e[o](t)};const{runContainer:J,removeContainer:G}=Y(),K="bit-ship-demon";var Q=t({meta:{name:"demon",description:"Start or stop bit-ship demon"},subCommands:{start:{meta:{name:"start",description:"Start bit-ship demon"},run(){return w(this,void 0,void 0,(function*(){e.log("Bit-Ship demon starting"),yield X(),e.log("Demon proxy is running on port 80")}))}},restart:{meta:{name:"restart",description:"restart bit-ship demon"},run(){return w(this,void 0,void 0,(function*(){e.log("Bit-Ship demon restarting");try{yield W(),e.log("Demon restarted")}catch(t){e.error("Error restarting bit-ship demon",t)}}))}},stop:{meta:{name:"stop",description:"Stop bit-ship demon"},run(){return w(this,void 0,void 0,(function*(){e.log("Bit-Ship demon stopping");try{yield V(),e.log("Demon stopped")}catch(t){e.error("Error stopping bit-ship demon",t)}}))}}}});function V(){return w(this,void 0,void 0,(function*(){yield G(K)}))}function W(){return w(this,void 0,void 0,(function*(){yield V(),yield X()}))}function X(){return w(this,void 0,void 0,(function*(){const{getItem:t}=C(),o=y(v(import.meta.url)),e=yield t("projects"),i=[`${o}/demon.mjs:/app/demon.mjs`,`${a.homedir()}/.bit-ship:/.bit-ship`];e&&Object.keys(e).forEach((t=>{i.push(`${t}:/projects/${e[t]}`)})),yield J({containerName:K,restart:"unless-stopped",image:"node:22-alpine",detouched:!0,silentLog:!0,remove:!1,storeLogs:!1,script:"node /app/demon.mjs",ports:["80:80"],volumes:i})}))}var Z=Object.freeze({__proto__:null,default:Q,restartDemon:W,startDemon:X,stopDemon:V});!function(){w(this,void 0,void 0,(function*(){const a=yield function(){return w(this,void 0,void 0,(function*(){const t=v(import.meta.url),o=y(t),e=yield p(j.join(o,"..","package.json"),"utf8");return JSON.parse(e).version||"0.0.0"}))}();yield(t=>w(void 0,void 0,void 0,(function*(){D=yield r({driver:s({base:`${k}/.bit-ship/data`})});let o=yield D.getItem("projects");o||(o={}),o[process.cwd()]||(o[process.cwd()]=""+Date.now(),yield D.setItem("projects",o),t())})))(W),w(void 0,void 0,void 0,(function*(){const t=C();(yield t.hasItem("isSentryDisabled"))||(n.init({dsn:"https://41b66ad2d847d5fbc79561d55988ddee@o4507703272996864.ingest.de.sentry.io/4507793192124496",integrations:[],tracesSampleRate:.5,profilesSampleRate:1}),i.addReporter({log:t=>{0===t.level&&n.captureMessage(t.args)}}))})),yield w(void 0,void 0,void 0,(function*(){try{const t=C();let o=yield t.getItem("lastUsageDate"),e=yield t.getItem("uuid");e||(e=f(),t.setItem("uuid",e),q("install",{uuid:e}),o=(new Date).toISOString(),yield t.setItem("lastUsageDate",o)),B(o)||q("usage-ping",{uuid:e})}catch(t){e.error("Failed to init analytics",t)}})),yield w(void 0,void 0,void 0,(function*(){yield L()})),yield w(void 0,void 0,void 0,(function*(){const t=O().getConfig(),o=".git/hooks";b.existsSync(o)&&t&&(yield l(`${o}/pre-commit`,"#!/bin/sh\n\nbit-ship hook --name=pre-commit"),yield l(`${o}/post-commit`,"#!/bin/sh\n\nbit-ship hook --name=post-commit"))}));const d=t({meta:{name:"bit-ship",version:a,description:"Bit-Ship CLI https://www.bit-ship.dev"},subCommands:{init:()=>import("./init-BscCf4Zo.js").then((t=>t.default)),run:()=>import("./run-Chhqqmb0.js").then((t=>t.run)),stop:()=>import("./stop-BWeaZ1mQ.js").then((t=>t.stop)),exec:()=>import("./exec-Bnd_ssv8.js").then((t=>t.exec)),demon:()=>Promise.resolve().then((function(){return Z})).then((t=>t.default)),image:()=>import("./image-D4JOaLv5.js").then((t=>t.default)),settings:()=>import("./settings-B3MarUQ5.js").then((t=>t.default)),hook:()=>import("./hook-DzK-Mqcm.js").then((t=>t.hook))}});o(d)}))}();export{w as _,Y as a,E as b,C as c,O as u};