@turbo/codemod 1.10.0 → 1.10.1-canary.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/cli.js +1 -1
  2. package/package.json +2 -2
package/dist/cli.js CHANGED
@@ -3,4 +3,4 @@
3
3
  But before we continue, please stash or commit your git changes.`)),console.log(`
4
4
  You may use the --force flag to override this safety check.`),process.exit(1)))}var _=i(require("path")),pe=i(require("fs"));function k({directory:r}){let e=_.default.isAbsolute(r)?r:_.default.join(process.cwd(),r);return{exists:pe.default.existsSync(e),absolute:e}}var A=i(require("os")),K=i(require("path")),O=i(require("fs-extra")),ue=require("semver");var I=require("child_process");function J(r,e){switch(r){case"yarn":return(0,I.execSync)("yarn --version",{cwd:e}).toString().trim();case"pnpm":return(0,I.execSync)("pnpm --version",{cwd:e}).toString().trim();case"npm":return(0,I.execSync)("npm --version",{cwd:e}).toString().trim()}}function Ae(){return{yarn:u("yarn global bin",{cwd:A.default.tmpdir()}),npm:u("npm bin --global",{cwd:A.default.tmpdir()}),pnpm:u("pnpm bin --global",{cwd:A.default.tmpdir()})}}function Oe(r,e="latest"){switch(r){case"yarn":return`yarn global add turbo@${e}`;case"npm":return`npm install turbo@${e} --global`;case"pnpm":return`pnpm install turbo@${e} --global`}}function Ue({packageManager:r,packageManagerVersion:e,installType:t,isUsingWorkspaces:n,to:o="latest"}){let s=a=>a.filter(Boolean).join(" ");switch(r){case"yarn":return(0,ue.gte)(e,"2.0.0")?s(["yarn","add",`turbo@${o}`,t==="devDependencies"&&"--dev"]):s(["yarn","add",`turbo@${o}`,t==="devDependencies"&&"--dev",n&&"-W"]);case"npm":return s(["npm","install",`turbo@${o}`,t==="devDependencies"&&"--save-dev"]);case"pnpm":return s(["pnpm","install",`turbo@${o}`,t==="devDependencies"&&"--save-dev",n&&"-w"])}}function Fe({directory:r}){let e=K.default.join(r,"package.json"),t=K.default.join(r,"pnpm-workspace.yaml"),n=O.default.existsSync(t);if(!O.default.existsSync(e))return console.error(`Unable to find package.json at ${e}`),{installType:void 0,isUsingWorkspaces:void 0};let o=O.default.readJsonSync(e),s=o.devDependencies&&"turbo"in o.devDependencies,a=o.dependencies&&"turbo"in o.dependencies,l="workspaces"in o||n;return a||s?{installType:a?"dependencies":"devDependencies",isUsingWorkspaces:l}:{installType:void 0,isUsingWorkspaces:l}}function Y({directory:r,to:e}){let t=u("turbo bin",{cwd:r,stdio:"pipe"}),n=Ae(),o=Object.keys(n).find(s=>{let a=n[s];return a&&t?t.includes(a):!1});if(t&&o)return Oe(o,e);{let s=y({directory:r}),{installType:a,isUsingWorkspaces:l}=Fe({directory:r});if(s&&a){let f=J(s,r);return Ue({packageManager:s,packageManagerVersion:f,installType:a,isUsingWorkspaces:l,to:e})}}}var j=i(require("chalk"));var U=i(require("chalk")),F=require("diff"),$=i(require("fs-extra")),ge=i(require("os")),H=i(require("path")),M=class{constructor(e){this.changes=[];if(this.filePath=e.filePath,this.rootPath=e.rootPath,this.after=e.after,this.error=e.error,e.before===void 0)try{H.default.extname(e.filePath)===".json"?this.before=$.default.readJsonSync(e.filePath):this.before=$.default.readFileSync(e.filePath)}catch{this.before=""}else e.before===null?this.before="":this.before=e.before;e.after?typeof this.before=="object"||typeof e.after=="object"?this.changes=(0,F.diffJson)(this.before,e.after):this.changes=(0,F.diffLines)(this.before,e.after):this.changes=[]}fileName(){return H.default.relative(this.rootPath,this.filePath)}write(){this.after&&(typeof this.after=="object"?$.default.writeJsonSync(this.filePath,this.after,{spaces:2}):$.default.writeFileSync(this.filePath,this.after))}additions(){return this.changes.filter(e=>e.added).length}deletions(){return this.changes.filter(e=>e.removed).length}hasChanges(){return this.additions()>0||this.deletions()>0}log(e){e.diff?(this.changes.forEach(t=>{t.added?process.stdout.write(U.default.green(t.value)):t.removed?process.stdout.write(U.default.red(t.value)):process.stdout.write(U.default.dim(t.value))}),console.log(ge.default.EOL)):console.log(this.after)}};var d=i(require("chalk")),C=class{constructor(e){this.transform=e.transformer,this.dry=e.dry}modified(...e){console.log(d.default.green(" MODIFIED "),...e,this.dry?d.default.dim("(dry run)"):"")}unchanged(...e){console.log(d.default.gray(" UNCHANGED "),...e,this.dry?d.default.dim("(dry run)"):"")}skipped(...e){console.log(d.default.yellow(" SKIPPED "),...e,this.dry?d.default.dim("(dry run)"):"")}error(...e){console.log(d.default.red(" ERROR "),...e,this.dry?d.default.dim("(dry run)"):"")}info(...e){console.log(d.default.bold(" INFO "),...e,this.dry?d.default.dim("(dry run)"):"")}};var z=class{constructor(e){this.modifications={};this.transform=e.transformer,this.rootPath=e.rootPath,this.dry=e.dry,this.print=e.print,this.logger=new C(e)}abortTransform(e){return this.logger.error(e.reason),{fatalError:new Error(e.reason),changes:e.changes||{}}}modifyFile(e){this.modifications[e.filePath]=new M({rootPath:this.rootPath,...e})}finish(){let e={changes:{}};return Object.keys(this.modifications).forEach(n=>{let o=this.modifications[n],s={action:"unchanged",additions:o.additions(),deletions:o.deletions()};if(o.hasChanges()){if(this.dry)s.action="skipped",this.logger.skipped(j.default.dim(o.fileName()));else try{o.write(),s.action="modified",this.logger.modified(j.default.bold(o.fileName()))}catch(a){let l="Unknown error";a instanceof Error&&(l=a.message),s.error=new Error(l),s.action="error",this.logger.error(o.fileName(),l)}this.print&&o.log({diff:!0})}else this.logger.unchanged(j.default.dim(o.fileName()));e.changes[o.fileName()]=s}),Object.keys(e.changes).some(n=>e.changes[n].action==="error")?this.abortTransform({reason:"Encountered an error while transforming files",changes:e.changes}):e}static logResults(e){let t=Object.keys(e.changes);if(console.log(),t.length>0){console.log(j.default.bold("Results:"));let n={};t.forEach(o=>{var a;let s=e.changes[o];n[o]={action:s.action,additions:s.additions,deletions:s.deletions,error:((a=s.error)==null?void 0:a.message)||"None"}}),console.table(n),console.log()}}},E=z;var he=i(require("path")),ye=require("fs-extra"),Se=["package.json","turbo.json",".git"];function Q({directory:r}){return Se.some(e=>(0,ye.existsSync)(he.default.join(r,e)))}function g({message:r,success:e}){return e?(console.log(m.default.bold(m.default.green("Migration completed"))),r&&console.log(r),process.exit(0)):(console.log(m.default.bold(m.default.red("Migration failed"))),r&&console.log(r),process.exit(1))}async function S(r,e){e.dry||T({directory:r,force:e.force});let t=await be.default.prompt([{type:"input",name:"directoryInput",message:"Where is the root of the repo to migrate?",when:!r,default:".",validate:c=>{let{exists:p,absolute:w}=k({directory:c});return p?!0:`Directory ${m.default.dim(`(${w})`)} does not exist`},filter:c=>c.trim()}]),{directoryInput:n=r}=t,{exists:o,absolute:s}=k({directory:n});if(!o)return g({success:!1,message:`Directory ${m.default.dim(`(${s})`)} does not exist`});if(!Q({directory:s}))return g({success:!1,message:`Directory (${m.default.dim(s)}) does not appear to be a repository`});let a=ae(n,e);if(!a)return g({success:!1,message:`Unable to infer the version of turbo being used by ${r}`});let l=e.to;try{l=await q(e)}catch(c){let p="UNKNOWN_ERROR";return c instanceof Error&&(p=c.message),g({success:!1,message:p})}if(!l)return g({success:!1,message:"Unable to fetch the latest version of turbo"});if(a===l)return g({success:!0,message:`Nothing to do, current version (${m.default.bold(a)}) is the same as the requested version (${m.default.bold(l)})`});let f=fe({fromVersion:a,toVersion:l});if(f.length===0&&console.log(`No codemods required to migrate from ${a} to ${l}`,x.default.EOL),console.log(`Upgrading turbo from ${m.default.bold(a)} to ${m.default.bold(l)} (${f.length===0?"no codemods required":`${f.length} required codemod${f.length===1?"":"s"}`})`,x.default.EOL),f.map((c,p)=>{console.log(`(${p+1}/${f.length}) ${m.default.bold(`Running ${c.value}`)}`);let w=c.transformer({root:n,options:e});return E.logResults(w),w}).some(c=>c.fatalError||Object.keys(c.changes).some(p=>c.changes[p].error)))return g({success:!1,message:"Could not complete migration due to codemod errors. Please fix the errors and try again."});let h=Y({directory:n,to:e.to});if(!h)return g({success:!1,message:"Unable to determine upgrade command"});e.install?e.dry?console.log(`Upgrading turbo with ${m.default.bold(h)} ${m.default.dim("(dry run)")}`,x.default.EOL):(console.log(`Upgrading turbo with ${m.default.bold(h)}`,x.default.EOL),(0,ke.execSync)(h,{cwd:n})):console.log(`Upgrade turbo with ${m.default.bold(h)}`,x.default.EOL),g({success:!0})}var D=i(require("chalk")),we=i(require("inquirer"));async function N(r,e,t){let n=P();if(t.list)return console.log(n.map(c=>`- ${D.default.cyan(c.value)}`).join(`
5
5
  `)),process.exit(0);t.dry||T({directory:e,force:t.force});let o=await we.default.prompt([{type:"input",name:"directoryInput",message:"Where is the root of the repo where the transform should run?",when:!e,default:".",validate:c=>{let{exists:p,absolute:w}=k({directory:c});return p?!0:`Directory ${D.default.dim(`(${w})`)} does not exist`},filter:c=>c.trim()},{type:"list",name:"transformerInput",message:"Which transform would you like to apply?",when:!r,pageSize:n.length,choices:n}]),{directoryInput:s=e,transformerInput:a=r}=o,{exists:l,absolute:f}=k({directory:s});if(!l)return console.error(`Directory ${D.default.dim(`(${f})`)} does not exist`),process.exit(1);let ee=n.map(c=>c.value),W=n.find(c=>c.value===a);if(!W)return console.error(`Invalid transform choice ${D.default.dim(`(${r})`)}, pick one of:`),console.error(ee.map(c=>`- ${c}`).join(`
6
- `)),process.exit(1);let h=W.transformer({root:f,options:t});if(h.fatalError)return process.exit(1);E.logResults(h)}var X=i(require("chalk")),ve=i(require("update-check"));var L={name:"@turbo/codemod",version:"1.10.0",description:"Provides Codemod transformations to help upgrade your Turborepo codebase when a feature is deprecated.",homepage:"https://turbo.build/repo",license:"MPL-2.0",repository:{type:"git",url:"https://github.com/vercel/turbo",directory:"packages/turbo-codemod"},bugs:{url:"https://github.com/vercel/turbo/issues"},bin:"dist/cli.js",scripts:{build:"tsup",test:"jest",lint:"eslint src/**/*.ts","check-types":"tsc --noEmit","add-transformer":"plop"},dependencies:{axios:"0.27.2",chalk:"2.4.2",commander:"^9.5.0",diff:"^5.1.0","find-up":"4.1.0","fs-extra":"^10.0.0","gradient-string":"^2.0.0",inquirer:"^8.2.4","inquirer-file-tree-selection-prompt":"^1.0.19","is-git-clean":"^1.1.0",ora:"4.1.1",semver:"^7.3.7","update-check":"^1.5.4"},devDependencies:{"@turbo/gen":"workspace:*","@turbo/test-utils":"workspace:*","@turbo/tsconfig":"workspace:*","@turbo/types":"workspace:*","@turbo/utils":"workspace:*","@types/chalk-animation":"^1.6.0","@types/diff":"^5.0.2","@types/fs-extra":"^9.0.13","@types/gradient-string":"^1.1.2","@types/inquirer":"^8.2.0","@types/jest":"^27.4.0","@types/node":"^16.11.12","@types/semver":"^7.3.9","@types/uuid":"^9.0.0",deepmerge:"^4.2.2",eslint:"^7.23.0",jest:"^27.4.3",plop:"^3.1.1",semver:"^7.3.5","ts-jest":"^27.1.1",tsup:"^6.7.0",typescript:"^4.5.5"},files:["dist"],publishConfig:{access:"public"}};var Le=(0,ve.default)(L).catch(()=>null);async function G(){try{let r=await Le;if(r!=null&&r.latest){let e=y();console.log(),console.log(X.default.yellow.bold("A new version of `@turbo/codemod` is available!")),console.log("You can update by running: "+X.default.cyan(e==="yarn"?"yarn global add @turbo/codemod":e==="pnpm"?"pnpm i -g @turbo/codemod":"npm i -g @turbo/codemod")),console.log()}process.exit()}catch{}}var V=new xe.Command;V.name("@turbo/codemod").description("Codemod transformations to help upgrade your Turborepo codebase when a feature is deprecated.").version(L.version,"-v, --version","output the current version");V.command("migrate").aliases(["update","upgrade"]).description("Migrate a project to the latest version of Turborepo").argument("[path]","Directory where the transforms should be applied").option("--from <version>","Specify the version to migrate from (default: current version)").option("--to <version>","Specify the version to migrate to (default: latest)").option("--install","Install new version of turbo after migration",!0).option("--force","Bypass Git safety checks and forcibly run codemods",!1).option("--dry","Dry run (no changes are made to files)",!1).option("--print","Print transformed files to your terminal",!1).action(S);V.command("transform",{isDefault:!0}).description("Apply a single code transformation to a project").argument("[transform]","The transformer to run").argument("[path]","Directory where the transforms should be applied").option("--force","Bypass Git safety checks and forcibly run codemods",!1).option("--list","List all available transforms",!1).option("--dry","Dry run (no changes are made to files)",!1).option("--print","Print transformed files to your terminal",!1).action(N);V.parseAsync().then(G).catch(async r=>{console.log(),r.command?console.log(` ${Z.default.cyan(r.command)} has failed.`):(console.log(Z.default.red("Unexpected error. Please report it as a bug:")),console.log(r)),console.log(),await G(),process.exit(1)});
6
+ `)),process.exit(1);let h=W.transformer({root:f,options:t});if(h.fatalError)return process.exit(1);E.logResults(h)}var X=i(require("chalk")),ve=i(require("update-check"));var L={name:"@turbo/codemod",version:"1.10.1-canary.0",description:"Provides Codemod transformations to help upgrade your Turborepo codebase when a feature is deprecated.",homepage:"https://turbo.build/repo",license:"MPL-2.0",repository:{type:"git",url:"https://github.com/vercel/turbo",directory:"packages/turbo-codemod"},bugs:{url:"https://github.com/vercel/turbo/issues"},bin:"dist/cli.js",scripts:{build:"tsup",test:"jest",lint:"eslint src/**/*.ts","check-types":"tsc --noEmit","add-transformer":"plop"},dependencies:{axios:"0.27.2",chalk:"2.4.2",commander:"^9.5.0",diff:"^5.1.0","find-up":"4.1.0","fs-extra":"^10.0.0","gradient-string":"^2.0.0",inquirer:"^8.2.4","inquirer-file-tree-selection-prompt":"^1.0.19","is-git-clean":"^1.1.0",ora:"4.1.1",semver:"^7.3.7","update-check":"^1.5.4"},devDependencies:{"@turbo/gen":"workspace:*","@turbo/test-utils":"workspace:*","@turbo/tsconfig":"workspace:*","@turbo/types":"workspace:*","@turbo/utils":"workspace:*","@types/chalk-animation":"^1.6.0","@types/diff":"^5.0.2","@types/fs-extra":"^9.0.13","@types/gradient-string":"^1.1.2","@types/inquirer":"^8.2.0","@types/jest":"^27.4.0","@types/node":"^16.11.12","@types/semver":"^7.3.9","@types/uuid":"^9.0.0",deepmerge:"^4.2.2",eslint:"^7.23.0",jest:"^27.4.3",plop:"^3.1.1",semver:"^7.3.5","ts-jest":"^27.1.1",tsup:"^6.7.0",typescript:"^4.5.5"},files:["dist"],publishConfig:{access:"public"}};var Le=(0,ve.default)(L).catch(()=>null);async function G(){try{let r=await Le;if(r!=null&&r.latest){let e=y();console.log(),console.log(X.default.yellow.bold("A new version of `@turbo/codemod` is available!")),console.log("You can update by running: "+X.default.cyan(e==="yarn"?"yarn global add @turbo/codemod":e==="pnpm"?"pnpm i -g @turbo/codemod":"npm i -g @turbo/codemod")),console.log()}process.exit()}catch{}}var V=new xe.Command;V.name("@turbo/codemod").description("Codemod transformations to help upgrade your Turborepo codebase when a feature is deprecated.").version(L.version,"-v, --version","output the current version");V.command("migrate").aliases(["update","upgrade"]).description("Migrate a project to the latest version of Turborepo").argument("[path]","Directory where the transforms should be applied").option("--from <version>","Specify the version to migrate from (default: current version)").option("--to <version>","Specify the version to migrate to (default: latest)").option("--install","Install new version of turbo after migration",!0).option("--force","Bypass Git safety checks and forcibly run codemods",!1).option("--dry","Dry run (no changes are made to files)",!1).option("--print","Print transformed files to your terminal",!1).action(S);V.command("transform",{isDefault:!0}).description("Apply a single code transformation to a project").argument("[transform]","The transformer to run").argument("[path]","Directory where the transforms should be applied").option("--force","Bypass Git safety checks and forcibly run codemods",!1).option("--list","List all available transforms",!1).option("--dry","Dry run (no changes are made to files)",!1).option("--print","Print transformed files to your terminal",!1).action(N);V.parseAsync().then(G).catch(async r=>{console.log(),r.command?console.log(` ${Z.default.cyan(r.command)} has failed.`):(console.log(Z.default.red("Unexpected error. Please report it as a bug:")),console.log(r)),console.log(),await G(),process.exit(1)});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turbo/codemod",
3
- "version": "1.10.0",
3
+ "version": "1.10.1-canary.0",
4
4
  "description": "Provides Codemod transformations to help upgrade your Turborepo codebase when a feature is deprecated.",
5
5
  "homepage": "https://turbo.build/repo",
6
6
  "license": "MPL-2.0",
@@ -29,7 +29,7 @@
29
29
  "update-check": "^1.5.4"
30
30
  },
31
31
  "devDependencies": {
32
- "@turbo/gen": "1.10.0",
32
+ "@turbo/gen": "1.10.1-canary.0",
33
33
  "@turbo/test-utils": "0.0.0",
34
34
  "@turbo/tsconfig": "0.0.0",
35
35
  "@turbo/types": "0.0.0",