UNPKG

@wyze/github-release

Version:

Generate a GitHub release.

67 lines (54 loc) 1.85 kB
#!/usr/bin/env node 'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var fs = require('fs'); var ghGot = require('gh-got'); var util = require('util'); var execa = _interopDefault(require('execa')); var readPkg = _interopDefault(require('read-pkg')); const readFile = util.promisify(fs.readFile); const getUrlAndVersion = async () => { const { repository, version } = await readPkg(); const url = (repository || { url: '' }).url.replace(/(^git\+|\.git$)/g, ''); const slug = url.split('/').slice(-2).join('/'); return { slug, url, version } }; const getChanges = async () => (await readFile('changelog.md')).toString().split('\n'); const getIndex = (changes, startIndex) => changes.findIndex( (value, index) => value.startsWith('### ') && index > startIndex ); const createGithubRelease = async ({ changes, latest, slug, url, version, }) => await ghGot.post({ body: { body: changes.join('\n') + `\n\n${url}/compare/${latest}...v${version}`, tag_name: `v${version}`, }, url: `repos/${slug}/releases`, }); // Run it var lib = async () => { // Push existing tags so the tag is there to create the release on await execa('git', ['push', '--follow-tags']); const rawChanges = await getChanges(); const startIndex = getIndex(rawChanges, 0) + 1; const endIndex = getIndex(rawChanges, startIndex); const changes = rawChanges .slice(startIndex, endIndex) .filter((value) => value !== ''); const latest = endIndex > 0 ? (rawChanges[endIndex].match(/\[([^\]]+)\]/) || ['', ''])[1] : ( await execa('git', ['rev-list', '--max-parents=0', 'HEAD']) ).stdout.slice(0, 7); await createGithubRelease({ changes, latest, ...(await getUrlAndVersion()) }); }; lib();