mirror of
https://github.com/changesets/action
synced 2026-10-26 16:39:56 +08:00
12 lines
11 KiB
JavaScript
12 lines
11 KiB
JavaScript
import{T as e,n as t,r as n,t as r}from"./manypkg-get-packages-BoFCbqAx.js";import i from"node:fs/promises";import{PassThrough as a}from"node:stream";import{spawn as o}from"node:child_process";import{cwd as s}from"node:process";import c,{basename as ee,delimiter as l,dirname as u,normalize as te,resolve as d}from"node:path";import{pipeline as f}from"node:stream/promises";import p from"node:readline";import{closeSync as m,openSync as h,readSync as g,statSync as _}from"node:fs";const ne=/^path$/i,v={key:`PATH`,value:``};function y(e){for(let t in e){if(!Object.prototype.hasOwnProperty.call(e,t)||!ne.test(t))continue;let n=e[t];return n?{key:t,value:n}:v}return v}function re(e,t){let n=t.value.split(l),r=[],i=e,a;do r.push(d(i,`node_modules`,`.bin`)),a=i,i=u(i);while(i!==a);r.push(u(process.execPath));let o=r.concat(n).join(l);return{key:t.key,value:o}}function b(e,t,n=!0){let r={...process.env,...t};if(!n)return r;let i=re(e,y(r));return r[i.key]=i.value,r}const x=e=>{let t=e.length,n=new a,r=()=>{--t===0&&n.end()};for(let t of e)f(t,n,{end:!1}).then(r).catch(r);return n},S=/([()\][%!^"`<>&|;, *?])/g,C=/^#!\s*(.+)/,w=/\.(?:com|exe)$/i,T=/node_modules[\\/]\.bin[\\/][^\\/]+\.cmd$/i,E=process.platform===`win32`,D=[`.EXE`,`.CMD`,`.BAT`,`.COM`];function O(e,t=[],n={}){if(n.shell===!0||!E)return{command:e,args:t,options:n};let r=k(e,n),i=null;if(r!==null){let e=Buffer.alloc(150),t=null;try{t=h(r,`r`),g(t,e,0,150,0)}catch{}finally{t!==null&&m(t)}let n=e.toString().match(C);if(n!==null){let e=n[1].trim(),t=e.indexOf(` `),r=t===-1?e:e.slice(0,t),a=t===-1?``:e.slice(t+1),o=ee(r);i=o===`env`?a||null:o}}if(i!==null&&r!==null&&(t=[r,...t],e=i,r=k(e,n)),r===null||!w.test(r)){let i=r!==null&&T.test(r);e=te(e),e=e.replace(S,`^$1`),t=t.map(e=>(e=e.replace(/(?=(\\+?)?)\1"/g,`$1$1\\"`),e=e.replace(/(?=(\\+?)?)\1$/,`$1$1`),e=`"${e}"`,e=e.replace(S,`^$1`),i&&(e=e.replace(S,`^$1`)),e)),t=[`/d`,`/s`,`/c`,`"${[e,...t].join(` `)}"`],e=n.env?.comspec??`cmd.exe`,n={...n,windowsVerbatimArguments:!0}}return{command:e,args:t,options:n}}function k(e,t){let n=(t.cwd??s()).toString(),r=t.env??process.env,i=y(r).value,a=e.includes(`/`)||e.includes(`\\`)?[``]:[n,...i.split(l)],o=r.PATHEXT?r.PATHEXT.split(l):D;e.includes(`.`)&&o[0]!==``&&o.unshift(``);for(let t of a){let r=d(n,t.startsWith(`"`)&&t.endsWith(`"`)&&t.length>1?t.slice(1,-1):t,e);for(let e of o){let t=r+e;try{if(_(t).isFile())return t}catch{}}}return null}var A=class extends Error{result;output;get exitCode(){if(this.result.exitCode!==null)return this.result.exitCode}constructor(e,t){super(`Process exited with non-zero status (${e.exitCode})`),this.result=e,this.output=t}};const j={timeout:void 0,persist:!1},M={windowsHide:!0};function N(e){let t=new AbortController;for(let n of e){if(n.aborted)return t.abort(),n;n.addEventListener(`abort`,()=>{t.abort(n.reason)},{signal:t.signal})}return t.signal}async function P(e){let t=``;try{for await(let n of e)t+=n.toString()}catch{}return t}var F=class{_process;_aborted=!1;_options;_command;_args;_resolveClose;_processClosed;_thrownError;get process(){return this._process}get pid(){return this._process?.pid}get exitCode(){if(this._process&&this._process.exitCode!==null)return this._process.exitCode}constructor(e,t,n){this._options={...j,...n},this._command=e,this._args=t??[],this._processClosed=new Promise(e=>{this._resolveClose=e})}kill(e){return this._process?.kill(e)===!0}get aborted(){return this._aborted}get killed(){return this._process?.killed===!0}pipe(e,t,n){return I(e,t,{...n,stdin:this})}async*[Symbol.asyncIterator](){let e=this._process;if(!e)return;let t=[];this._streamErr&&t.push(this._streamErr),this._streamOut&&t.push(this._streamOut);let n=x(t),r=p.createInterface({input:n});for await(let e of r)yield e.toString();if(await this._processClosed,e.removeAllListeners(),this._thrownError)throw this._thrownError;if(this._options?.throwOnError&&this.exitCode!==0&&this.exitCode!==void 0)throw new A(this)}async _waitForOutput(){let e=this._process;if(!e)throw Error(`No process was started`);let[t,n]=await Promise.all([this._streamOut?P(this._streamOut):``,this._streamErr?P(this._streamErr):``]);await this._processClosed;let{stdin:r}=this._options;if(r&&typeof r!=`string`&&await r,e.removeAllListeners(),this._thrownError)throw this._thrownError;let i={stderr:n,stdout:t,exitCode:this.exitCode};if(this._options.throwOnError&&this.exitCode!==0&&this.exitCode!==void 0)throw new A(this,i);return i}then(e,t){return this._waitForOutput().then(e,t)}_streamOut;_streamErr;spawn(){let e=s(),t=this._options,n={...M,...t.nodeOptions},r=[];this._resetState(),t.timeout!==void 0&&r.push(AbortSignal.timeout(t.timeout)),t.signal!==void 0&&r.push(t.signal),t.persist===!0&&(n.detached=!0),r.length>0&&(n.signal=N(r)),n.env=b(e,n.env,t.nodePath);let i=O(this._command,this._args,n),a=o(i.command,i.args,i.options);if(a.stderr&&(this._streamErr=a.stderr),a.stdout&&(this._streamOut=a.stdout),this._process=a,a.once(`error`,this._onError),a.once(`close`,this._onClose),a.stdin){let{stdin:e}=t;typeof e==`string`?a.stdin.end(e):e?.process?.stdout?.pipe(a.stdin)}}_resetState(){this._aborted=!1,this._processClosed=new Promise(e=>{this._resolveClose=e}),this._thrownError=void 0}_onError=e=>{if(e.name===`AbortError`&&(!(e.cause instanceof Error)||e.cause.name!==`TimeoutError`)){this._aborted=!0;return}this._thrownError=e};_onClose=()=>{this._resolveClose&&this._resolveClose()}};const I=(e,t,n)=>{let r=new F(e,t,n);return r.spawn(),r};var L=class extends Error{code;constructor(e,t){super(`${t}, exit code: ${e}`),this.code=e}},R=class extends Error{constructor(e){super(e)}};async function z(e,t){await i.mkdir(c.dirname(e),{recursive:!0}),await i.writeFile(e,t,`utf8`)}async function ie(e){let t=c.resolve(e,`.changeset`,`pre.json`),n;try{let e=await i.readFile(t,`utf8`);try{n=JSON.parse(e)}catch(t){throw t instanceof SyntaxError&&console.error(`error parsing json:`,e),t}}catch(e){if(e.code!==`ENOENT`)throw e}return n=n?await B(e,n):void 0,n}async function B(e,t){return t.initialVersions==null?t:(delete t.initialVersions,await z(c.resolve(e,`.changeset`,`pre.json`),JSON.stringify(t,null,2)+`
|
|
`),t)}var V=e(n(),1);async function H(e,t){let n=await I(`git`,[`merge-base`,t,`HEAD`],{nodeOptions:{cwd:e}});if(n.exitCode!==0)throw Error(`Failed to find where HEAD diverged from "${t}". Does "${t}" exist and it's synced with remote?`);return n.stdout.toString().trim()}async function U({cwd:e}){let t=(await I(`git`,[`rev-parse`,`--is-shallow-repository`],{nodeOptions:{cwd:e}})).stdout.toString().trim();if(t===`--is-shallow-repository`){let t=(await I(`git`,[`rev-parse`,`--git-dir`],{nodeOptions:{cwd:e}})).stdout.toString().trim(),n=c.resolve(e,t);try{return await i.access(c.join(n,`shallow`)),!0}catch{return!1}}else return t===`true`}async function W({cwd:e}){let{stdout:t,exitCode:n,stderr:r}=await I(`git`,[`rev-parse`,`--show-toplevel`],{nodeOptions:{cwd:e}});if(n!==0)throw Error(r.toString());return t.toString().trim().replace(/\n|\r/g,``)}async function G({cwd:e,ref:t,fullPath:n=!1}){let r=await H(e,t),i=await I(`git`,[`diff`,`--name-only`,`--no-relative`,r],{nodeOptions:{cwd:e}});if(i.exitCode!==0)throw Error(`Failed to diff against ${r}. Is ${r} a valid ref?`);let a=i.stdout.toString().trim().split(`
|
|
`).filter(e=>e);if(!n)return a;let o=await W({cwd:e});return a.map(e=>c.resolve(o,e))}async function K({cwd:e,ref:t}){try{let n=await I(`git`,[`diff`,`--name-only`,`--diff-filter=d`,`--no-relative`,await H(e,t)],{nodeOptions:{cwd:e}}),r=/.changeset\/[^/]+\.md$/;return n.stdout.toString().trim().split(`
|
|
`).filter(e=>r.test(e))}catch(e){if(e instanceof L)return[];throw e}}async function q({cwd:e,ref:t,changedFilePatterns:n=[`**`]}){let i=await G({ref:t,cwd:e,fullPath:!0});return(await r(e)).packages.toSorted((e,t)=>t.dir.length-e.dir.length).filter(e=>{let t=[];for(let n=i.length-1;n>=0;n--){let r=i[n];if(!c.relative(e.dir,r).startsWith(`..`)){i.splice(n,1);let a=r.slice(e.dir.length+1);t.push(a)}}return t.length>0&&J(t,n)})}function J(e,t){if(!t)return e.length>0;let n=t.map(e=>(0,V.default)(e,void 0,!0));return e.some(e=>{e.includes(`\\`)&&(e=e.replace(/\\/g,`/`));let t=!1;for(let r of n)t?r.state.negated&&!r(e)&&(t=!1):!r.state.negated&&r(e)&&(t=!0);return t})}var Y=e(t(),1);const ae=/\s*---([^]*?)\r?\n\s*---(\s*(?:\n|$)[^]*)/,X=`---
|
|
"package-name": patch
|
|
---`,Z=[`major`,`minor`,`patch`,`none`];function Q(e,t=200){return e.length>t?e.slice(0,t)+`...`:e}function oe(e,t){for(let n of e){if(typeof n.name!=`string`||n.name.trim()===``)throw Error(`could not parse changeset - invalid package name in frontmatter.\nExpected a non-empty string for package name, but got: ${JSON.stringify(n.name)}\nChangeset contents:\n${Q(t)}`);if(typeof n.type!=`string`)throw Error(`could not parse changeset - invalid release type for package "${n.name}".\nExpected a string for release type, but got: ${typeof n.type}\nChangeset contents:\n${Q(t)}`);if(!Z.includes(n.type))throw Error(`could not parse changeset - invalid version type ${JSON.stringify(n.type)} for package "${n.name}".\nValid version types are: ${Z.join(`, `)}\nChangeset contents:\n${Q(t)}`)}}function se(e){let t=e.trim();if(!t)throw Error(`could not parse changeset - file is empty.
|
|
Changesets must have frontmatter with package names and version types.
|
|
Example:\n${X}\n\nYour changeset summary here.`);let n=ae.exec(e);if(!n)throw Error(`could not parse changeset - missing or invalid frontmatter.
|
|
Changesets must start with frontmatter delimited by "---".
|
|
Example:\n${X}\n\nYour changeset summary here.\nReceived content:\n${Q(t)}`);let[,r,i]=n,a=i.trim(),o;try{o=Y.parse(r)}catch(e){throw Error(`could not parse changeset - invalid YAML in frontmatter.
|
|
The frontmatter between the "---" delimiters must be valid YAML.
|
|
YAML error: ${e instanceof Error?e.message:String(e)}\nFrontmatter content:\n${r}`,{cause:e})}let s=[];if(o){if(typeof o!=`object`||Array.isArray(o))throw Error(`could not parse changeset - frontmatter must be an object mapping package names to version types.\nExpected format:\n${X}\nReceived:\n${r}`);s=Object.entries(o).map(([e,t])=>({name:e,type:t}))}return oe(s,e),{releases:s,summary:a}}const ce=[/^README\.md$/i,`AGENTS.md`,`CLAUDE.md`,`GEMINI.md`];async function le(e,t,n){let r=(await K({cwd:t,ref:n})).map(e=>e.split(`/`).pop());return e.filter(e=>r.includes(e))}async function $(e,t){let n=c.join(e,`.changeset`),r;try{r=await i.readdir(n)}catch(e){throw e.code===`ENOENT`?Error(`There is no .changeset directory in this project`,{cause:e}):e}t!=null&&(r=await le(r,n,t));let a=r.filter(e=>!e.startsWith(`.`)&&e.endsWith(`.md`)&&!ce.some(t=>typeof t==`string`?t===e:t.test(e))).map(async e=>({...se(await i.readFile(c.join(n,e),`utf8`)),id:e.replace(`.md`,``)}));return await Promise.all(a)}export{R as a,ie as i,q as n,I as o,U as r,$ as t}; |