js.mts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env zx
  2. // Script for working with JavaScript projects.
  3. import 'zx/globals';
  4. import {
  5. parseCliArguments,
  6. partitionArguments,
  7. } from './helpers/utils.mts';
  8. enum Command {
  9. Format = 'format',
  10. Lint = 'lint',
  11. Test = 'test',
  12. Publish = 'publish',
  13. }
  14. const { command, libraryPath, args } = parseCliArguments();
  15. async function pnpm(
  16. command: string,
  17. build = false,
  18. ) {
  19. const [pnpmArgs, commandArgs] = partitionArguments(args, '--');
  20. cd(libraryPath);
  21. await $`pnpm install`;
  22. if (build) {
  23. await $`pnpm build`;
  24. }
  25. await $`pnpm ${command} ${pnpmArgs} -- ${commandArgs}`;
  26. }
  27. async function format() {
  28. return pnpm('format');
  29. }
  30. async function lint() {
  31. return pnpm('lint');
  32. }
  33. async function test() {
  34. // Start the local validator, or restart it if it is already running.
  35. await $`pnpm validator:restart`;
  36. // Build the client and run the tests.
  37. return pnpm('test', true);
  38. }
  39. async function publish() {
  40. const [level, tag = 'latest'] = args;
  41. if (!level) {
  42. throw new Error('A version level — e.g. "path" — must be provided.');
  43. }
  44. // Go to the directory and install the dependencies.
  45. cd(libraryPath);
  46. await $`pnpm install`;
  47. // Update the version.
  48. const versionArgs = [
  49. '--no-git-tag-version',
  50. ...(level.startsWith('pre') ? [`--preid ${tag}`] : []),
  51. ];
  52. let { stdout } = await $`pnpm version ${level} ${versionArgs}`;
  53. const newVersion = stdout.slice(1).trim();
  54. // Expose the new version to CI if needed.
  55. if (process.env.CI) {
  56. await $`echo "new_version=${newVersion}" >> $GITHUB_OUTPUT`;
  57. }
  58. // Publish the package.
  59. // This will also build the package before publishing (see prepublishOnly script).
  60. await $`pnpm publish --no-git-checks --tag ${tag}`;
  61. // Commit the new version.
  62. await $`git commit -am "Publish JS client v${newVersion}"`;
  63. // Tag the new version.
  64. await $`git tag -a js@v${newVersion} -m "JS client v${newVersion}"`;
  65. }
  66. switch (command) {
  67. case Command.Format:
  68. await format();
  69. break;
  70. case Command.Lint:
  71. await lint();
  72. break;
  73. case Command.Test:
  74. await test();
  75. break;
  76. case Command.Publish:
  77. await publish();
  78. break;
  79. default:
  80. throw new Error(`Unknown command: ${command}`);
  81. }