envsyncer
Version:
Sync your root .env files into monorepo subfolders with ease
19 lines • 3.11 kB
JavaScript
import S from"fs";import u from"path";import g from"chalk";function E(n,e=[]){let o=S.readdirSync(n,{withFileTypes:!0});S.existsSync(u.join(n,"package.json"))&&n!==process.cwd()&&e.push(u.relative(process.cwd(),n));for(let t of o)if(t.isDirectory()){if(t.name==="node_modules"||t.name.startsWith("."))continue;let r=u.join(n,t.name);E(r,e)}return e}async function x(){try{console.log(g.blue("\u{1F50D} Scanning for projects in monorepo..."));let n=E(process.cwd());return n.length===0?(console.log(g.yellow("\u26A0\uFE0F No projects found in the monorepo")),[]):(console.log(g.blue(`
\u{1F4C1} Found the following projects:`)),n.forEach(e=>console.log(g.gray(` - ${e}`))),n)}catch(n){return console.error(g.red("Error scanning directories:"),n),[]}}import T from"inquirer";import j from"chalk";async function $(n){if(n.length===0)return[];let{selected:e}=await T.prompt([{type:"checkbox",name:"selected",message:"Select folders to sync .env into:",choices:n.map(o=>({name:o,checked:!0,value:o})),validate:o=>o.length>0||"You must choose at least one directory!"}]);return console.log(j.blue(`
\u{1F3AF} Selected directories:`),e.map(o=>j.gray(o)).join(", ")),e}import m from"fs";import v from"path";import c from"chalk";var b=[".env",".env.local",".env.development",".env.production"];function D(n){let e=new Set,o=n.split(`
`);for(let t of o){let r=t.trim();if(r&&!r.startsWith("#")){let s=r.indexOf("=");if(s>0){let i=r.substring(0,s);e.add(i)}}}return e}function L(n,e){let o=n.split(`
`),t=[];for(let r of o){let s=r.trim();if(!s||s.startsWith("#")){t.length>0&&t.push(r);continue}let i=s.indexOf("=");if(i>0){let f=s.substring(0,i);e.has(f)||t.push(r)}}for(;t.length>0&&!t[t.length-1].trim();)t.pop();return t}async function k(n){let e=b.filter(o=>m.existsSync(v.join(process.cwd(),o)));if(e.length===0){console.log(c.red("\u274C No .env files found in root directory.")),console.log(c.gray("Looked for:",b.join(", ")));return}console.log(c.blue(`
\u{1F4C4} Found env files:`),e.join(", "));for(let o of e){let t=v.join(process.cwd(),o),r=m.readFileSync(t,"utf-8"),s=D(r);for(let i of n){let f=v.join(process.cwd(),i,o);try{let a=[];if(m.existsSync(f)){let p=m.readFileSync(f,"utf-8");a=L(p,s)}let l=r;a.length>0&&(l.endsWith(`
`)||(l+=`
`),l+=`
# Local environment variables
`,l+=a.join(`
`),l.endsWith(`
`)||(l+=`
`)),m.writeFileSync(f,l);let w=a.filter(p=>p.trim()&&!p.trim().startsWith("#")).length,F=w>0?`\u2705 Synced ${o} to ${c.gray(`${i}/${o}`)} ${c.cyan(`(preserved ${w} local vars)`)}`:`\u2705 Synced ${o} to ${c.gray(`${i}/${o}`)}`;console.log(c.green(F))}catch(a){console.error(c.red(`\u274C Failed to sync ${o} to ${i}:`),a)}}}}import d from"chalk";async function N(){console.log(d.green.bold(`
\u{1F331} EnvSync \u2014 syncing envs into your monorepo like magic
`));let n=await x(),e=await $(n);e.length||(console.log(d.yellow("No folders selected. Exiting.")),process.exit(0)),await k(e),console.log(d.green(`
\u2705 Done syncing env files!
`))}N().catch(n=>{console.error(d.red("\u274C Something went wrong:"),n),process.exit(1)});
//# sourceMappingURL=index.mjs.map