import{pathExists,writeTextFile}from"../files.js";import{readFile,writeFile}from"node:fs/promises";const PNPM_WORKSPACE_PATH=`pnpm-workspace.yaml`,PNPM_WORKSPACE_CONTENT=`minimumReleaseAgeExclude:
  - eve
allowBuilds:
  sharp: false
`,EVE_RELEASE_AGE_EXCLUSION=`  - eve`,SHARP_BUILD_POLICY=`  sharp: false`;function findYamlBlockEnd(e,t){let n=t+1;for(;n<e.length;){let t=e[n]??``;if(t.length>0&&!t.startsWith(` `)&&!t.startsWith(`	`))break;n+=1}return n}function withSharpBuildPolicy(e){let t=e.endsWith(`
`)?e:`${e}\n`,n=t.split(`
`),r=n.findIndex(e=>e===`allowBuilds:`);if(r<0)return`${t.trim().length===0?``:`${t}\n`}allowBuilds:\n${SHARP_BUILD_POLICY}\n`;let i=findYamlBlockEnd(n,r);if(n.slice(r+1,i).some(e=>/^\s+sharp:/.test(e)))return e;let a=i;for(;a>r+1&&n[a-1]===``;)--a;return n.splice(a,0,SHARP_BUILD_POLICY),n.join(`
`)}function withExperimentalEveReleaseAgeExclusion(e){let t=e.endsWith(`
`)?e:`${e}\n`,n=t.split(`
`),r=n.findIndex(e=>e===`minimumReleaseAgeExclude:`);if(r<0)return`${t.trim().length===0?``:`${t}\n`}minimumReleaseAgeExclude:\n${EVE_RELEASE_AGE_EXCLUSION}\n`;let i=findYamlBlockEnd(n,r);if(n.slice(r+1,i).some(e=>e.trim()===`- eve`))return e;let a=i;for(;a>r+1&&n[a-1]===``;)--a;return n.splice(a,0,EVE_RELEASE_AGE_EXCLUSION),n.join(`
`)}async function ensurePnpmWorkspacePolicy(i){if(!await pathExists(i))return await writeTextFile(i,PNPM_WORKSPACE_CONTENT,{force:!0}),`written`;let o=await readFile(i,`utf8`),s=withExperimentalEveReleaseAgeExclusion(withSharpBuildPolicy(o));return s===o?`skipped`:(await writeFile(i,s,`utf8`),`written`)}export{PNPM_WORKSPACE_CONTENT,PNPM_WORKSPACE_PATH,ensurePnpmWorkspacePolicy};