turborepo-template-upgrade
Version:
Upgrade projects based on a Turborepo template using selective git diffs, with support for ignore paths and skipped packages.
3 lines (2 loc) • 3.83 kB
JavaScript
import{a as b,b as E,c as k}from"./chunk-BN2Z7Q4M.mjs";import{a as j,b as A}from"./chunk-56HYCQFQ.mjs";import{execSync as r}from"child_process";import{writeFileSync as x,existsSync as C}from"fs";import{resolve as O}from"path";import{DEFAULT_BACKUP_DIR as S}from"git-json-resolver/utils";var g=[],U=0,P=(c,a,s,f="template",h=3)=>{var i,y;if(U++>h){U=0,s(`Max patch recursion reached (${h}), stopping`);return}let p=`git diff ${c} ${f}/main -- ${a.join(" ")} .`;s(`Running: ${p}`);let l=r(p,{encoding:"utf8"});x(".template.patch",l),s(`Patch written to .template.patch (${l.length} chars)`);try{s("Applying patch with 3-way merge"),r("git apply --3way --ignore-space-change --ignore-whitespace .template.patch",{encoding:"utf8"}),s("Patch applied successfully")}catch($){let t=(y=(i=$.stderr)==null?void 0:i.split)==null?void 0:y.call(i,`
`).filter(m=>m.startsWith("error"));s(`Patch failed with ${t.length} errors`),t.forEach(m=>{var e;let u=(e=m.split(":")[1])==null?void 0:e.trim();u&&(a.push(`:!${u}`),s(`Added to exclusions: ${u}`))}),g.push("Applied patch with errors: "),g.push({errorLines:t,exclusions:a}),g.push("^^^---Applied patch with errors"),t.length&&P(c,a,s,f,h)}},q=async(c,a={})=>{let s=b(),f=j(s),h=A(f,a),{debug:p=!1,dryRun:l=!1,templateUrl:i="https://github.com/react18-tools/turborepo-template",excludePaths:y=[],skipInstall:$=!1,remoteName:t="template",maxPatchRetries:m=3,skipCleanCheck:u=!1}=h,e=n=>p&&console.log(`\u{1F50D} [DEBUG] ${n}`);if(e(`Working directory: ${s}`),Object.keys(f).length>0&&e("Loaded config from .tt-upgrade.config.json"),u)e("Skipping git clean check");else try{r("git diff --quiet"),r("git diff --cached --quiet"),e("Git tree is clean")}catch{console.error("\u274C Error: Please commit or stash your changes before upgrading.");return}l&&console.log("\u{1F50D} Dry run mode - no changes will be applied");try{r(`git remote add ${t} ${i}`),e(`Added ${t} remote: ${i}`)}catch{e(`${t} remote already exists`)}try{r(`rm -rf ${S}`)}catch{}try{r(`git fetch ${t}`),e(`Fetched latest changes from ${t}`);let n=(c==null?void 0:c.trim())||E(),d=[...[".tkb","CHANGELOG.md","README.md","**/CHANGELOG.md","**/FUNDING.md","SECURITY.md","TODO.md","FEATURED.md","docs","lib","scripts/rebrand.config.json","pnpm-lock.yaml",".lst",".turborepo-template.lst",".vscode/settings.json"],...y].map(o=>`:!${o}`);if(e(`Base exclusions: ${d.length} items`),[".github/workflows/docs.yml","scripts/templates","examples/express","examples/nextjs/src/app/button.tsx","examples/nextjs/src/app/button.module.css","examples/remix","packages/logger","packages/jest-presets","scripts/rebrand.js","scripts/rebrander.js","plopfile.js","tsconfig.docs.json","typedoc.config.js"].forEach(o=>{C(O(s,o))||(d.push(`:!${o}`),e(`Added missing path to exclusions: ${o}`))}),[["scripts/templates",["component-generator.md"]],["docs",["scripts/add-frontmatter.mjs"]]].forEach(([o,w])=>{C(o)||w.forEach(G=>d.push(`:!${G}`))}),e(`Generating patch from ${n} to template/main`),e(`Total exclusions: ${d.length}`),l){let o=`git diff ${n} ${t}/main -- ${d.join(" ")} .`,w=r(o,{encoding:"utf8"});console.log("\u{1F4CB} Patch preview:"),console.log(w||"No changes to apply");return}P(n,d,e,t,m);let D=r(`git rev-parse ${t}/main`,{encoding:"utf8"}).trim();x(".turborepo-template.lst",D),await k(p),console.log("\u2705 Upgrade applied successfully."),!l&&!$?(console.log("Reinstalling dependencies..."),r("pnpm i",{stdio:p?"inherit":"pipe"}),e("Dependencies reinstalled")):$&&e("Skipping dependency installation");try{r("sed -i '/\\.turborepo-template\\.lst/d' .github/workflows/upgrade.yml"),r("sed -i '/\\.turborepo-template\\.lst/d' .github/workflows/docs.yml")}catch{}}catch(n){console.error("\u274C Upgrade failed:",n)}g.length>0&&(x(".error.log",JSON.stringify(g,null,2)),e(`Error log written with ${g.length} entries`))};export{q as a};