Created
March 27, 2017 18:19
-
-
Save kalinchernev/1baab65db80984430893754af17def8e to your computer and use it in GitHub Desktop.
release.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const standardVersion = require('standard-version'); | |
const conventionalGithubReleaser = require('conventional-github-releaser'); | |
const ghReleaseAssets = require('gh-release-assets'); | |
const simpleGit = require('simple-git')(); | |
const fs = require('fs'); | |
const archiver = require('archiver'); | |
// Retrieve GitHub token from environment | |
const token = process.env.RELEASE_TOKEN; | |
if (token) { | |
const AUTH = { | |
type: 'oauth', | |
token, | |
}; | |
// Checkout and pull master | |
simpleGit.checkout('master', (checkoutError) => { | |
if (checkoutError) { | |
console.error(`git checkout master failed with message: ${checkoutError.message}`); | |
} | |
simpleGit.pull((pullErr) => { | |
if (pullErr) { | |
console.error(`git pull master failed with message: ${pullErr.message}`); | |
} | |
console.info('Start releasing process...'); | |
// Options are the same as command line, except camelCase | |
standardVersion({}, (err) => { | |
if (err) { | |
console.error(`standard-version failed with message: ${err.message}`); | |
} | |
// Push tag | |
simpleGit.push(['--follow-tags', 'origin', 'master'], (err1) => { | |
if (err1) { | |
console.error(`git push --follow-tags origin master with message: ${err1.message}`); | |
} | |
// Make a release | |
conventionalGithubReleaser(AUTH, { | |
preset: 'angular', | |
}, (err2, responses) => { | |
if (err2) { | |
console.error(`GitHub Release failed with message: ${err2.message}`); | |
// Remove tag? | |
// $ git tag -d release01 | |
// $ git push origin :refs/tags/release01 | |
} | |
if (responses[0].state === 'fulfilled') { | |
// Make sure the .tmp folder exists | |
if (!fs.existsSync(path.resolve(__dirname, '../.tmp'))) { | |
fs.mkdirSync(path.resolve(__dirname, '../.tmp')); | |
} | |
const output = fs.createWriteStream(path.resolve(__dirname, '../.tmp/dist.zip')); | |
const archive = archiver('zip'); | |
output.on('close', () => { | |
const url = responses[0].value.upload_url; | |
ghReleaseAssets({ | |
url, | |
token: [token], | |
assets: [{ | |
name: 'framework.zip', | |
path: path.resolve(__dirname, '../.tmp/dist.zip'), | |
}], | |
}, (err3) => { | |
if (err3) { | |
console.error(`GitHub Release Assets failed with message: ${err3.message}`); | |
} | |
console.info('Everything went fine!'); | |
}); | |
}); | |
archive.on('error', (archiveError) => { | |
throw archiveError; | |
}); | |
archive.pipe(output); | |
archive.glob('**', { | |
expand: true, | |
cwd: path.resolve(__dirname, '../dist'), | |
}); | |
archive.finalize(); | |
} else { | |
console.error('Couldn\'t create GitHub Release...'); | |
console.error(responses[0].reason.message); | |
} | |
}); | |
}); | |
}); | |
}); | |
}); | |
} else { | |
console.error('Please provide a GitHub token in order to make a release.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment