pglaunch
Version:
Generate multiple PostgreSQL connection strings/databases using CLI for development environments!
33 lines (27 loc) • 3.71 kB
JavaScript
import $ from"path";import{parseArgs as w}from"util";var i="pglaunch",g="5.5.7";var s={name:"Neeraj Dalal",email:"admin@nrjdalal.com",url:"https://nrjdalal.com"};import P from"get-port";import r from"nano-spawn";import{customAlphabet as d}from"nanoid";import u from"terminal-link";import{bold as f,green as p,red as v}from"yoctocolors";var h=`
With ${f(`${u("PGLaunch","https://github.com/nrjdalal/pglaunch")}`)} instantly launch disposable PostgreSQL containers!
Usage:
$ ${i} [options]
Options:
-n, --name <name> Name for PostgresSQL database
(default: current directory name)
-p, --port <port> Port for PostgresSQL database
(default: random available port)
-k, --keep Keep the container after exit
(default: false)
-c, --confirm Confirm starting another container with the same name
-v, --version Display version
-h, --help Display help
Author:
${s.name} <${s.email}> (${s.url})`,S=n=>{try{return w(n)}catch(t){throw new Error(`Error parsing arguments: ${t.message}`)}},x=async()=>{try{let{positionals:n,values:t}=S({allowPositionals:!0,options:{name:{type:"string",short:"n"},port:{type:"string",short:"p"},keep:{type:"boolean",short:"k",default:!1},confirm:{type:"boolean",short:"c",default:!1},help:{type:"boolean",short:"h"},version:{type:"boolean",short:"v"}}});n.length||(t.version&&(console.log(`${i}@${g}`),process.exit(0)),t.help&&(console.log(h),process.exit(0))),console.log(`
With ${f(`${u("PGLaunch","https://github.com/nrjdalal/pglaunch")}`)} instantly launch disposable PostgreSQL containers!
`);try{await r("docker",["--version"],{stdio:"ignore"})}catch(e){console.error(`- Docker is not installed. Please install Docker and try again.
Download it here: ${p("https://docs.docker.com/desktop")}`),process.exit(1)}try{await r("docker",["info"],{stdio:"ignore"})}catch(e){console.error(`- Docker is installed but not running.
${p("Please start the Docker application/daemon and try again.")}`),process.exit(1)}let o={name:t.name||$.basename(process.cwd()),port:t.port||String(await P())},{stdout:y}=await r("docker",["ps","--filter","ancestor=postgres:alpine","--format","{{.Names}}:{{.Ports}}"]),l=y.split(`
`).filter(Boolean).map(e=>{let c=e.indexOf(":"),k=e.slice(0,c),m=e.slice(c+1).match(/(\d+)->5432\/tcp/),b=m?m[1]:void 0;return{name:k,port:b}});if(!t.confirm&&l.some(e=>e.name.split("-").slice(0,-1).join("-")===o.name)){for(let e of l)e.name.split("-").slice(0,-1).join("-")===o.name&&console.info(`- A container by similar name "${e.name}" running at port ${e.port}.
${v(`POSTGRES_URL=postgres://postgres:postgres@localhost:${e.port}/postgres`)}
`);console.error(" Error:\n - Specify a different name with the `-n` flag, e.g. -n my-project.\n - Or use `-c` flag to start another container with a similar name."),process.exit(1)}try{o.name=`${o.name}-${d("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",4)()}`;let e=d("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",7)();await r("docker",["run","-d",...t.keep?[]:["--rm"],"--name",o.name,"-p",`${o.port}:5432`,"-e","POSTGRES_USER=postgres","-e","POSTGRES_PASSWORD=postgres","-e","POSTGRES_DB=postgres","postgres:alpine"]),console.log(`- A container with name "${o.name} :${o.port}" started successfully.
${p(`POSTGRES_URL=postgres://postgres:postgres@localhost:${o.port}/postgres`)}`),t.keep||console.log("\n If you want to --keep the container after exit, use the `-k` flag.")}catch(e){throw new Error(`Failed to start the Postgres container: ${e.message}`)}process.exit(0)}catch(n){console.error(h),console.error(`
${n.message}
`),process.exit(1)}};x();