/ scripts / syncLocalDependencies.js
syncLocalDependencies.js
 1  const fs = require("fs")
 2  const path = require("path")
 3  const { execSync } = require("child_process")
 4  
 5  // Get the version argument from the command line
 6  const version = process.argv[2]
 7  if (!version) {
 8    console.error("Usage: node update-workspace-dependencies.js <version>")
 9    process.exit(1)
10  }
11  
12  // Get the list of workspaces with mismatched dependencies
13  const output = execSync("yarn --silent workspaces info --json", {
14    encoding: "utf-8",
15  })
16  const data = JSON.parse(output)
17  
18  const workspaces = Object.keys(data).filter(key => {
19    return data[key].mismatchedWorkspaceDependencies?.length
20  })
21  
22  // Loop through each workspace and update the dependencies
23  workspaces.forEach(workspace => {
24    const dependencies = data[workspace].mismatchedWorkspaceDependencies
25  
26    // Loop through each dependency and update its version in package.json
27    const packageJsonPath = path.join(data[workspace].location, "package.json")
28    const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"))
29    let hasChanges = false
30  
31    dependencies.forEach(dependency => {
32      if (packageJson.dependencies?.[dependency]) {
33        packageJson.dependencies[dependency] = version
34        hasChanges = true
35      }
36      if (packageJson.devDependencies?.[dependency]) {
37        packageJson.devDependencies[dependency] = version
38        hasChanges = true
39      }
40      if (packageJson.peerDependencies?.[dependency]) {
41        packageJson.peerDependencies[dependency] = version
42        hasChanges = true
43      }
44    })
45  
46    // Write changes to package.json if there are any
47    if (hasChanges) {
48      fs.writeFileSync(
49        packageJsonPath,
50        JSON.stringify(packageJson, null, 2) + "\n"
51      )
52    }
53  })
54  
55  const rootPackageJson = JSON.parse(fs.readFileSync("package.json", "utf-8"))
56  delete rootPackageJson["resolutions"]
57  fs.writeFileSync(
58    "package.json",
59    JSON.stringify(rootPackageJson, null, 2) + "\n"
60  )