summaryrefslogtreecommitdiff
path: root/scripts/bind_package_dependencies.js
blob: eb9a4cc643da33c046dd5e515acbcbc9d7fd0810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import fs from 'node:fs';
import path from 'node:path';
import _ from 'lodash';

function bindPackageDependencies() {
  const scriptDir = process.cwd();
  const currentPkgJsonPath = path.join(scriptDir, 'package.json');
  const currentPkg = JSON.parse(fs.readFileSync(currentPkgJsonPath));
  // assume packages are all under /<repo_root>/packages/
  const packagesDir = path.join(path.dirname(scriptDir));

  const geminiCodePkgs = fs
    .readdirSync(packagesDir)
    .filter(
      (name) =>
        fs.statSync(path.join(packagesDir, name)).isDirectory() &&
        fs.existsSync(path.join(packagesDir, name, 'package.json')),
    )
    .map((packageDirname) => {
      const packageJsonPath = path.join(
        packagesDir,
        packageDirname,
        'package.json',
      );
      return JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
    })
    .reduce((pkgs, pkg) => ({ ...pkgs, [pkg.name]: pkg }), {});
  currentPkg.dependencies = _.mapValues(
    currentPkg.dependencies,
    (value, key) => {
      if (geminiCodePkgs[key]) {
        console.log(
          `Package ${currentPkg.name} has a dependency on ${key}. Updating dependent version.`,
        );
        return geminiCodePkgs[key].version;
      }
      return value;
    },
  );
  const updatedPkgJson = JSON.stringify(currentPkg, null, 2) + '\n';
  fs.writeFileSync(currentPkgJsonPath, updatedPkgJson);
}

bindPackageDependencies();