package-latest-version.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env node
  2. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  3. // SPDX-License-Identifier: Apache-2.0
  4. // SPDX-License-Identifier: MIT
  5. /*
  6. This script is solely intended to be run as part of the `covector publish` step to
  7. check the latest version of a crate, considering the current minor version.
  8. */
  9. const https = require('https')
  10. const kind = process.argv[2]
  11. const packageName = process.argv[3]
  12. const packageVersion = process.argv[4]
  13. const target = packageVersion.substring(0, packageVersion.lastIndexOf('.'))
  14. let url = null
  15. switch (kind) {
  16. case 'cargo':
  17. url = `https://crates.io/api/v1/crates/${packageName}`
  18. break
  19. case 'npm':
  20. url = `https://registry.npmjs.org/${packageName}`
  21. break
  22. default:
  23. throw new Error('unexpected kind ' + kind)
  24. }
  25. const options = {
  26. headers: {
  27. 'Content-Type': 'application/json',
  28. Accept: 'application/json',
  29. 'User-Agent': 'tauri (https://github.com/tauri-apps/tauri)'
  30. }
  31. }
  32. https.get(url, options, (response) => {
  33. let chunks = []
  34. response.on('data', function (chunk) {
  35. chunks.push(chunk)
  36. })
  37. response.on('end', function () {
  38. const data = JSON.parse(chunks.join(''))
  39. if (kind === 'cargo') {
  40. const versions =
  41. data.versions?.filter((v) => v.num.startsWith(target)) ?? []
  42. console.log(versions.length ? versions[0].num : '0.0.0')
  43. } else if (kind === 'npm') {
  44. const versions = Object.keys(data.versions).filter((v) =>
  45. v.startsWith(target)
  46. )
  47. console.log(versions[versions.length - 1] || '0.0.0')
  48. }
  49. })
  50. })