publish.mts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env zx
  2. import 'zx/globals';
  3. import {
  4. cliArguments,
  5. getCargo,
  6. popArgument,
  7. workingDirectory,
  8. } from './setup/shared.mts';
  9. const [folder, ...args] = cliArguments();
  10. const manifestPath = path.join(workingDirectory, folder, 'Cargo.toml');
  11. const fix = popArgument(args, '--dry-run');
  12. const dryRun = argv['dry-run'] ?? false;
  13. const [level] = args;
  14. if (!level) {
  15. throw new Error('A version level — e.g. "patch" — must be provided.');
  16. }
  17. // Get the package information from the crate TOML file.
  18. const crate = getCargo(folder).package;
  19. const previous = crate['version'];
  20. const name = crate['name'];
  21. // Go to the crate folder to release.
  22. cd(path.dirname(manifestPath));
  23. // Publish the new version.
  24. const releaseArgs = dryRun
  25. ? []
  26. : ['--tag-name', `${name}@v{{version}}`, '--no-confirm', '--execute'];
  27. await $`cargo release ${level} ${releaseArgs}`;
  28. // Stop here if this is a dry run.
  29. if (dryRun) {
  30. process.exit(0);
  31. }
  32. // Get the updated version number.
  33. const version = getCargo(folder).package['version'];
  34. // Git tag for the new and old versions.
  35. const newGitTag = `${name}@v${version}`;
  36. const oldGitTag = `${name}@v${previous}`;
  37. // Expose the versions to CI if needed.
  38. if (process.env.CI) {
  39. await $`echo "new_git_tag=${newGitTag}" >> $GITHUB_OUTPUT`;
  40. await $`echo "old_git_tag=${oldGitTag}" >> $GITHUB_OUTPUT`;
  41. }