Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rw upgrade: Update package versions from template package.json #8855

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion packages/cli/src/commands/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,16 @@ export const handler = async ({ dryRun, tag, verbose, dedupe }) => {
task: async (ctx) => setLatestVersionToContext(ctx, tag),
},
{
title: 'Updating your project package.json(s)',
title: 'Updating your Redwood version',
task: (ctx) => updateRedwoodDepsForAllSides(ctx, { dryRun, verbose }),
enabled: (ctx) => !!ctx.versionToUpgradeTo,
},
{
title: 'Updating other packages in your package.json(s)',
task: (ctx) =>
updatePackageVersionsFromTemplate(ctx, { dryRun, verbose }),
enabled: (ctx) => ctx.versionToUpgradeTo?.includes('canary'),
},
{
title: 'Running yarn install',
task: (ctx) => yarnInstall(ctx, { dryRun, verbose }),
Expand Down Expand Up @@ -272,6 +278,83 @@ function updateRedwoodDepsForAllSides(ctx, options) {
)
}

async function updatePackageVersionsFromTemplate(ctx, { dryRun, verbose }) {
if (!ctx.versionToUpgradeTo) {
throw new Error('Failed to upgrade')
}

const packageJsons = [
{
basePath: getPaths().base,
url: 'https://raw.githubusercontent.com/redwoodjs/redwood/main/packages/create-redwood-app/templates/ts/package.json',
},
{
basePath: getPaths().api.base,
url: 'https://raw.githubusercontent.com/redwoodjs/redwood/main/packages/create-redwood-app/templates/ts/api/package.json',
},
{
basePath: getPaths().web.base,
url: 'https://raw.githubusercontent.com/redwoodjs/redwood/main/packages/create-redwood-app/templates/ts/web/package.json',
},
]

return new Listr(
packageJsons.map(({ basePath, url }) => {
const pkgJsonPath = path.join(basePath, 'package.json')

return {
title: `Updating ${pkgJsonPath}`,
task: async () => {
const res = await fetch(url)
const text = await res.text()
const templatePackageJson = JSON.parse(text)

const localPackageJsonText = fs.readFileSync(pkgJsonPath, 'utf-8')
const localPackageJson = JSON.parse(localPackageJsonText)

Object.entries(templatePackageJson.dependencies || {}).forEach(
([depName, depVersion]) => {
// Redwood packages are handled in another task
if (!depName.startsWith('@redwoodjs/')) {
if (verbose || dryRun) {
console.log(
` - ${depName}: ${localPackageJson.dependencies[depName]} => ${depVersion}`
)
}

localPackageJson.dependencies[depName] = depVersion
}
}
)

Object.entries(templatePackageJson.devDependencies || {}).forEach(
([depName, depVersion]) => {
// Redwood packages are handled in another task
if (!depName.startsWith('@redwoodjs/')) {
if (verbose || dryRun) {
console.log(
` - ${depName}: ${localPackageJson.devDependencies[depName]} => ${depVersion}`
)
}

localPackageJson.devDependencies[depName] = depVersion
}
}
)

if (!dryRun) {
fs.writeFileSync(
pkgJsonPath,
JSON.stringify(localPackageJson, null, 2)
)
}
},
skip: () => !fs.existsSync(pkgJsonPath),
}
})
)
}

async function refreshPrismaClient(task, { verbose }) {
/** Relates to prisma/client issue, @see: https://github.com/redwoodjs/redwood/issues/1083 */
try {
Expand Down