wishthis/node_modules/fomantic-ui/scripts/nightly-version.js

92 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-01-13 13:00:31 +00:00
// node
const fs = require('fs')
const path = require('path')
2022-04-07 07:06:43 +00:00
const childProcess = require('child_process')
2022-01-13 13:00:31 +00:00
// npm
const fetch = require('node-fetch')
const semver = require('semver')
2022-04-07 07:06:43 +00:00
const actions = require('@actions/core')
2022-01-13 13:00:31 +00:00
// pkg
const pkg = require('../package.json')
2022-04-07 07:06:43 +00:00
const process = require('process')
2022-01-13 13:00:31 +00:00
const ghBase = 'https://api.github.com'
const repoUrlPath = 'fomantic/Fomantic-UI'
const npmBase = 'https://registry.npmjs.org'
const npmPackage = 'fomantic-ui'
2022-04-07 07:06:43 +00:00
const currentRev = childProcess // get the current rev from the repo
.execSync('git rev-parse HEAD')
.toString()
.trim()
.slice(0, 7)
2022-01-13 13:00:31 +00:00
2022-04-07 07:06:43 +00:00
const getNextVersion = async function () {
const versions = await fetch(`${ghBase}/repos/${repoUrlPath}/milestones`)
2022-01-13 13:00:31 +00:00
.then(r => r.json())
2022-04-07 07:06:43 +00:00
.then(milestones => milestones.filter(m => m.title.indexOf('x') === -1)) // remove all versions with `x` in it
.then(versions => versions.map(m => m.title)) // create array of versions
.then(versions => semver.sort(versions))
2022-01-13 13:00:31 +00:00
2022-04-07 07:06:43 +00:00
// Return first entry aka the smallest version in milestones which would therefore
// be the next version
return semver.parse(versions[0])
2022-01-13 13:00:31 +00:00
}
2022-04-07 07:06:43 +00:00
const getPublishedVersion = async function () {
// get the latest published nightly tagged version
return semver.parse(
await fetch(`${npmBase}/${npmPackage}`)
.then(r => r.json())
.then(p => {
let nightly = p['dist-tags'].nightly ?? '';
let versionInfo = p.versions[nightly] ?? {};
let buildCommit = nightly.indexOf('+')===-1 && versionInfo.gitHead ? '+'+(versionInfo.gitHead ?? '').slice(0,7) : '';
return nightly+buildCommit;
})
)
2022-01-13 13:00:31 +00:00
}
const getNightlyVersion = async function () {
2022-04-07 07:06:43 +00:00
const next = semver.parse(await getNextVersion())
const current = semver.parse(await getPublishedVersion())
2022-01-13 13:00:31 +00:00
2022-04-07 07:06:43 +00:00
if (current.build[0] === currentRev) {
actions.setOutput('shouldPublish', false)
2022-01-13 13:00:31 +00:00
2022-04-07 07:06:43 +00:00
console.log('No new commits since last publish. Exiting.')
process.exit(0)
return
}
let nightlyVersion = `${next.version}-beta.0`
// Check if published version is the same version as next version.
// Only check major, minor and patch as previously published nightly
// versions would include prerelease tag and build metadata
if (semver.eq(`${next.major}.${next.minor}.${next.patch}`, `${current.major}.${current.minor}.${current.patch}`)) {
// If they match then a nightly version has already been published, so we need to increment
// the prerelease and add the new rev as build metadata
nightlyVersion = semver.inc(
`${next.version}-beta.${current.prerelease[1]}`,
'prerelease'
)
2022-01-13 13:00:31 +00:00
}
2022-04-07 07:06:43 +00:00
actions.setOutput('shouldPublish', 'yes')
return `${nightlyVersion}+${currentRev}`
2022-01-13 13:00:31 +00:00
}
getNightlyVersion()
.then(nightlyVersion => {
pkg.version = nightlyVersion
})
.then(() => {
fs.writeFileSync(
path.resolve(__dirname, '../package.json'),
JSON.stringify(pkg, null, 2)
)
})
.then(() => console.log(`Done (${pkg.version})`))