update-example-versions.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import path from 'node:path';
  2. import fs from 'node:fs/promises';
  3. import { globby as glob } from 'globby';
  4. /*
  5. This file updates the dependencies' versions in `examples/*` to match the workspace packages' versions.
  6. This should be run after `changeset version` so the release PR updates all the versions together.
  7. */
  8. const rootUrl = new URL('../..', import.meta.url);
  9. const rootPackageJson = JSON.parse(await fs.readFile(new URL('./package.json', rootUrl), 'utf-8'));
  10. // get all workspace package name to versions
  11. /** @type {Map<string, string>} */
  12. const packageToVersions = new Map();
  13. // Changeset detects workspace packages to publish via `workspaces` in package.json.
  14. // Although this conflicts with the `pnpm-workspace.yaml` config, it's easier to configure what gets
  15. // published through this field, so this file also respects this field when updating the versions.
  16. const workspaceDirs = await glob(rootPackageJson.workspaces, {
  17. onlyDirectories: true,
  18. cwd: rootUrl,
  19. });
  20. for (const workspaceDir of workspaceDirs) {
  21. const packageJsonPath = path.join(workspaceDir, './package.json');
  22. const packageJson = await readAndParsePackageJson(packageJsonPath);
  23. if (!packageJson) continue;
  24. if (!packageJson.name) {
  25. throw new Error(`${packageJsonPath} does not contain a "name" field.`);
  26. }
  27. if (!packageJson.version) {
  28. throw new Error(`${packageJsonPath} does not contain a "version" field.`);
  29. }
  30. packageToVersions.set(packageJson.name, packageJson.version);
  31. }
  32. // Update all examples' package.json
  33. const exampleDirs = await glob('examples/*', {
  34. onlyDirectories: true,
  35. cwd: rootUrl,
  36. });
  37. for (const exampleDir of exampleDirs) {
  38. const packageJsonPath = path.join(exampleDir, './package.json');
  39. const packageJson = await readAndParsePackageJson(packageJsonPath);
  40. if (!packageJson) continue;
  41. // Update dependencies
  42. for (const depName of Object.keys(packageJson.dependencies ?? [])) {
  43. if (packageToVersions.has(depName)) {
  44. packageJson.dependencies[depName] = `^${packageToVersions.get(depName)}`;
  45. }
  46. }
  47. // Update devDependencies
  48. for (const depName of Object.keys(packageJson.devDependencies ?? [])) {
  49. if (packageToVersions.has(depName)) {
  50. packageJson.devDependencies[depName] = `^${packageToVersions.get(depName)}`;
  51. }
  52. }
  53. await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n');
  54. }
  55. /**
  56. * @param {string} packageJsonPath
  57. * @returns {Promise<Record<string, any> | undefined>}
  58. */
  59. async function readAndParsePackageJson(packageJsonPath) {
  60. try {
  61. return JSON.parse(await fs.readFile(packageJsonPath, 'utf-8'));
  62. } catch {}
  63. }