UNPKG

@alessiofrittoli/node-scripts

Version:
4 lines 3.26 kB
import {execSync,execFileSync}from'child_process';import R from'fs';import N from'path';var f=()=>process.env.INIT_CWD||process.cwd(),h=()=>new Map(process.argv.map(e=>({value:e,isValue:!e.startsWith("-")})).map((e,t,r)=>{switch(t){case 0:return ["--executable",e.value];case 1:return ["--scriptPath",e.value];default:{let o=r[t+1];if(!e.isValue)return o!=null&&o.isValue?[e.value,o.value]:[e.value,"true"]}}}).filter(Boolean));var x=e=>JSON.parse(R.readFileSync(N.resolve(e,"package.json")).toString());var y=e=>{let t=e.match(/-(\w+)\.\d+/);return t?t[1]:null};var S=()=>{let t=execSync("git remote -v",{encoding:"buffer"}).toString().split(` `),r=new Map;return t.map(o=>{var m;let[s,a]=o.split(" ");if(!s||!a)return null;let i=r.get(s)||new Map,p=i.get("urls")||new Map,g=a.split(" "),l=((m=g.at(1))==null?void 0:m.replace(/\(|\)/g,""))||"fetch",n=p.has(l)?l==="fetch"?"push":"fetch":l,c=g.at(0);if(!c)return null;p.set(n,c),i.set("name",s),i.set("urls",p),r.has(s)||r.set(s,i);}),r},w=()=>{var e,t,r;return ((r=(t=(e=execSync("git branch -rl '*/HEAD'"))==null?void 0:e.toString().split(` `).filter(Boolean).at(0))==null?void 0:t.split(" -> ").at(1))==null?void 0:r.split("/"))||[null,null]},v=()=>{var t;let[e]=w();return e?S().get(e):(t=S().entries().next().value)==null?void 0:t[1]},B=()=>execSync("git stash list").toString().split(` `).map(G).filter(Boolean),G=e=>{var a;if(!e)return null;let t=e.split(": "),r=Number(t.at(0).split("@{").pop().split("}").at(0)||"invalid");if(isNaN(r))return null;let o=(t.at(2)?t.at(1).split(" ").pop():null)||"main",s=t.at(2)||((a=t.at(1))==null?void 0:a.split(" ").pop())||null;return {index:r,branch:o,name:s}};var b=e=>B().find(t=>"name"in e?t.name===e.name:t.index===e.index),P=e=>execFileSync("git",["stash","pop","--index",e.toString()],{stdio:"inherit"});var T=e=>JSON.parse(execSync(`npm list --json${" -g"}`).toString()),k=(e,t)=>e in(T().dependencies||{});var F=()=>{let e=null;try{e=x(f());}catch(n){}let t=h(),r=t.has("--verbose"),o=t.get("--build")||"build",s=t.get("--version")||(e==null?void 0:e.version),a=t.get("--origin")||t.get("--o"),i=t.has("--npm"),p=t.get("--access")||"public",g="pre-release",l="npm run";try{l=k("pnpm",!0)?"pnpm":"npm run";}catch(n){let c=n;console.log({package:e==null?void 0:e.name,message:"Couldn't check if `pnpm` is installed. Using `npm` instead.",error:c.message});}if((!s||typeof s!="string")&&(console.error("No `version` found in `package.json`"),process.exit(1)),i)switch(p){case "public":break;case "restricted":break;default:console.error("Invalid `--access` option. `public` or `restricted` accepted."),process.exit(1);}if(!a){let n=v();a=(n==null?void 0:n.get("name"))||"origin";}try{let n=y(s);if(execSync(`git stash save -u -m "${g}"`,{stdio:"inherit"}),execSync(`${l} ${o}`,{stdio:"inherit"}),execSync(`git tag v${s}`,{stdio:"inherit"}),execSync(`git push ${a} tag v${s}`,{stdio:"inherit"}),i){let m=[`--access ${p}`,n&&`--tag ${n}`].filter(Boolean).join(" ");execSync(`npm publish ${m}`,{stdio:"inherit"});}let c=b({name:g});c&&P(c.index),r&&console.log({package:e==null?void 0:e.name,message:`Released version ${s}`,origin:a,tag:`v${s}`,npmPublish:i});}catch(n){console.error("Error during release process:",n),process.exit(1);}};export{F as publish};