UNPKG

buildstamp

Version:
4 lines (3 loc) 3.04 kB
"use strict";var h=Object.create;var m=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames,B=Object.getOwnPropertySymbols,T=Object.getPrototypeOf,w=Object.prototype.hasOwnProperty,D=Object.prototype.propertyIsEnumerable;var R=(t,s,r)=>s in t?m(t,s,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[s]=r,U=(t,s)=>{for(var r in s||(s={}))w.call(s,r)&&R(t,r,s[r]);if(B)for(var r of B(s))D.call(s,r)&&R(t,r,s[r]);return t};var G=(t,s)=>{for(var r in s)m(t,r,{get:s[r],enumerable:!0})},O=(t,s,r,i)=>{if(s&&typeof s=="object"||typeof s=="function")for(let o of C(s))!w.call(t,o)&&o!==r&&m(t,o,{get:()=>s[o],enumerable:!(i=x(s,o))||i.enumerable});return t};var l=(t,s,r)=>(r=t!=null?h(T(t)):{},O(s||!t||!t.__esModule?m(r,"default",{value:t,enumerable:!0}):r,t)),H=t=>O(m({},"__esModule",{value:!0}),t);var d=(t,s,r)=>new Promise((i,o)=>{var a=n=>{try{u(r.next(n))}catch(e){o(e)}},c=n=>{try{u(r.throw(n))}catch(e){o(e)}},u=n=>n.done?i(n.value):Promise.resolve(n.value).then(a,c);u((r=r.apply(t,s)).next())});var L={};G(L,{buildstamp:()=>y});module.exports=H(L);var b=l(require("node:child_process"),1),_=l(require("node:fs/promises"),1),f=l(require("node:path"),1),g=l(require("node:process"),1);var N=({cwd:t=g.cwd(),output:s="buildstamp.json",ci:r=!0,git:i=!0,date:o=!0,safe:a=!1,extra:c={}}={})=>({ci:r,cwd:t,output:s,git:i,date:o,extra:c,safe:a}),y=t=>d(void 0,null,function*(){let{ci:s,git:r,date:i,cwd:o,output:a,extra:c,safe:u}=N(t),n=U({},c),e=u?A:p=>p;if(i&&(n.date=new Date().toISOString()),r&&Object.assign(n,yield e(S)(o,g.env)),s&&Object.assign(n,e(j)(g.env)),a){let p=e(f.resolve)(o,a);yield e(_.mkdir)(e(f.dirname)(p),{recursive:!0}),yield e(_.writeFile)(p,JSON.stringify(n,null,2))}return n}),E=(t,s)=>d(void 0,null,function*(){return s.CI_COMMIT_BRANCH||s.GITHUB_REF_NAME||(yield I("git",["rev-parse","--abbrev-ref","HEAD"],t)).stdout}),S=(t,s)=>d(void 0,null,function*(){let{stdout:r}=yield I("git",["rev-parse","HEAD"],t),i=yield P(t),o=yield E(t,s),a=(i.match(/([^./:]+\/[^./]+)(\.git)?$/)||[])[1];return{git_commit_branch:o,git_commit_id:r,git_repo_url:i,git_repo_name:a}}),P=t=>d(void 0,null,function*(){let s=(yield I("git",["remote","show"],t)).stdout.split(` `)[0],{stdout:r}=yield I("git",["config","--get",`remote.${s}.url`],t);return r}),j=t=>({ci_run_id:t.BUILD_NUMBER||t.CI_JOB_ID||t.GITHUB_RUN_ID,ci_run_url:t.BUILD_URL||t.CI_JOB_URL||t.GITHUB_RUN_ID&&`${t.GITHUB_SERVER_URL}/${t.GITHUB_REPOSITORY}/actions/runs/${t.GITHUB_RUN_ID}`}),I=(t,s,r)=>new Promise((i,o)=>{let a=0,c=Date.now(),u=[],n=[],e=b.spawn(t,s,{cwd:r});e.stdout.on("data",p=>n.push(p.toString())),e.stderr.on("data",p=>u.push(p.toString())),e.on("error",p=>u.push(p.toString())).on("exit",p=>{a=p}).on("close",()=>{(a?o:i)({stderr:u.join("").trim(),stdout:n.join("").trim(),status:a,signalCode:e.signalCode,duration:Date.now()-c})})}),A=(t,s={})=>(...r)=>{try{let i=t(...r);return(i==null?void 0:i.then(o=>o,()=>s))||i}catch(i){return s}};0&&(module.exports={buildstamp}); //# sourceMappingURL=index.cjs.map