Skip to content

perf(create-cedar-rsc-app): parallelize package.json updates in upgradeToLatestCanary #1627

@lisa-assistant

Description

@lisa-assistant

Summary

In packages/create-cedar-rsc-app/src/upgradeToLatestCanary.ts, the updatePackageJsons function updates multiple package.json files sequentially using synchronous file I/O:

// TODO: await Promise.all(packageJsons.map(async (path) => {
for (const path of packageJsons) {
  const contents = fs.readFileSync(path, 'utf8')
  // ... mutate package.json ...
  fs.writeFileSync(path, JSON.stringify(packageJson, null, 2))
}

The TODO comment already suggests the intended fix: convert to Promise.all for parallel I/O.

Fix

  1. Make updatePackageJsons async
  2. Switch from fs.readFileSync/fs.writeFileSync to fs.promises.readFile/fs.promises.writeFile
  3. Wrap in Promise.all
async function updatePackageJsons(
  config: Config,
  packageJsons: string[],
  latestRwCanary: string,
) {
  await Promise.all(
    packageJsons.map(async (path) => {
      const contents = await fs.promises.readFile(path, 'utf8')
      const packageJson = JSON.parse(contents)

      const { dependencies, devDependencies } = packageJson

      if (dependencies) {
        for (const name of Object.keys(dependencies)) {
          if (name.startsWith('@cedarjs/')) {
            dependencies[name] = latestRwCanary
          }
        }
      }

      if (devDependencies) {
        for (const name of Object.keys(devDependencies)) {
          if (name.startsWith('@cedarjs/')) {
            devDependencies[name] = latestRwCanary
          }
        }
      }

      await fs.promises.writeFile(path, JSON.stringify(packageJson, null, 2))
    }),
  )
}

The writes are to different files so there are no ordering dependencies — parallel execution is safe.

The call site in the same file will also need to await the now-async function.

Files

  • packages/create-cedar-rsc-app/src/upgradeToLatestCanary.tsupdatePackageJsons function and its call site

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions