nightly-version.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // node
  2. const fs = require('fs')
  3. const path = require('path')
  4. // npm
  5. const fetch = require('node-fetch')
  6. const semver = require('semver')
  7. // pkg
  8. const pkg = require('../package.json')
  9. const ghBase = 'https://api.github.com'
  10. const repoUrlPath = 'fomantic/Fomantic-UI'
  11. const npmBase = 'https://registry.npmjs.org'
  12. const npmPackage = 'fomantic-ui'
  13. const getGitHubVersion = async function () {
  14. return fetch(`${ghBase}/repos/${repoUrlPath}/milestones`)
  15. .then(r => r.json())
  16. .then(milestones => milestones.filter(m => m.title.indexOf('x') === -1)[0].title)
  17. }
  18. const getCurrentNpmVersion = async function () {
  19. return fetch(`${npmBase}/${npmPackage}`)
  20. .then(r => r.json())
  21. .then(p => p['dist-tags'].nightly)
  22. }
  23. const getNpmPreRelease = async function () {
  24. return fetch(`${npmBase}/${npmPackage}`)
  25. .then(r => r.json())
  26. .then(p => p['dist-tags'].nightly)
  27. .then(v => semver.prerelease(v))
  28. .then(pr => pr === null ? ['beta', 0] : pr)
  29. }
  30. const getNightlyVersion = async function () {
  31. const nextVersion = await getGitHubVersion()
  32. const currentNightlyWithPre = semver.parse(await getCurrentNpmVersion())
  33. const currentNightly = `${currentNightlyWithPre.major}.${currentNightlyWithPre.minor}.${currentNightlyWithPre.patch}`
  34. if (!semver.gt(nextVersion, currentNightly)) {
  35. if (semver.minor(nextVersion) === semver.minor(currentNightly)) {
  36. const preRelease = await getNpmPreRelease()
  37. return semver.inc(
  38. `${nextVersion}-${preRelease[0]}.${preRelease[1]}`,
  39. 'prerelease'
  40. )
  41. }
  42. }
  43. return `${nextVersion}-beta.0`
  44. }
  45. getNightlyVersion()
  46. .then(nightlyVersion => {
  47. pkg.version = nightlyVersion
  48. })
  49. .then(() => {
  50. fs.writeFileSync(
  51. path.resolve(__dirname, '../package.json'),
  52. JSON.stringify(pkg, null, 2)
  53. )
  54. })
  55. .then(() => console.log(`Done (${pkg.version})`))