fix(scripts): github release

This commit is contained in:
Manu Mtz.-Almeida
2018-04-28 14:43:15 +02:00
parent a367f1e3c8
commit 545d3c29a2
4 changed files with 589 additions and 446 deletions

View File

@ -14,6 +14,10 @@ const path = require('path');
async function main() { async function main() {
try { try {
if (!process.env.GH_TOKEN) {
throw new Error('env.GH_TOKEN is undefined');
}
const version = await askVersion(); const version = await askVersion();
// compile and verify packages // compile and verify packages
@ -250,12 +254,10 @@ function updatePackageVersion(tasks, package, version) {
async function generateChangeLog(tasks) { async function generateChangeLog(tasks) {
tasks.push( tasks.push({
{ title: `Generate CHANGELOG.md`,
title: `Generate CHANGELOG.md`, task: () => execa('npm', ['run', 'changelog'], { cwd: common.rootDir }),
task: () => execa('npm', ['run', 'changelog'], { cwd: common.rootDir }), });
}
);
} }

View File

@ -5,16 +5,32 @@
const chalk = require('chalk'); const chalk = require('chalk');
const execa = require('execa'); const execa = require('execa');
const Listr = require('listr'); const Listr = require('listr');
const octokit = require('@octokit/rest')()
const common = require('./common'); const common = require('./common');
const fs = require('fs-extra');
async function main() { async function main() {
try { try {
const {version} = common.readPkg('core'); if (!process.env.GH_TOKEN) {
throw new Error('env.GH_TOKEN is undefined');
}
const tasks = [];
const { version } = common.readPkg('core');
const changelog = findChangelog();
// repo must be clean
common.checkGit(tasks);
// publish each package in NPM // publish each package in NPM
await publishPackages(common.packages, version); publishPackages(tasks, common.packages, version);
// push tag to git remote
publishGit(tasks, version);
const listr = new Listr(tasks);
await listr.run();
console.log(`\nionic ${version} published!! 🎉\n`); console.log(`\nionic ${version} published!! 🎉\n`);
} catch (err) { } catch (err) {
@ -24,28 +40,22 @@ async function main() {
} }
async function publishPackages(packages, version) { async function publishPackages(tasks, packages, version) {
const tasks = [];
// repo must be clean
common.checkGit(tasks);
// first verify version // first verify version
packages.forEach(package => { packages.forEach(package => {
if (package === 'core') return; if (package === 'core') {
return;
}
const pkg = common.readPkg(package); const pkg = common.readPkg(package);
tasks.push({
tasks.push( title: `${pkg.name}: check version (must match: ${version})`,
{ task: () => {
title: `${pkg.name}: check version (must match: ${version})`, if (version !== pkg.version) {
task: () => { throw new Error(`${pkg.name} version ${pkg.version} must match ${version}`);
if (version !== pkg.version) {
throw new Error(`${pkg.name} version ${pkg.version} must match ${version}`);
}
} }
} }
); });
}); });
// next publish // next publish
@ -53,23 +63,14 @@ async function publishPackages(packages, version) {
const pkg = common.readPkg(package); const pkg = common.readPkg(package);
const projectRoot = common.projectPath(package); const projectRoot = common.projectPath(package);
tasks.push( tasks.push({
{ title: `${pkg.name}: publish ${pkg.version}`,
title: `${pkg.name}: publish ${pkg.version}`, task: () => execa('npm', ['publish', '--tag', 'latest'], { cwd: projectRoot })
task: () =>execa('npm', ['publish', '--tag', 'latest'], { cwd: projectRoot }) });
}
);
}); });
// push tag to git remote
publishGitTag(tasks, version);
const listr = new Listr(tasks);
await listr.run();
} }
function publishGit(tasks, version, changelog) {
function publishGitTag(tasks, version) {
const tag = `v${version}`; const tag = `v${version}`;
tasks.push( tasks.push(
@ -78,15 +79,57 @@ function publishGitTag(tasks, version) {
task: () => execa('git', ['tag', `${tag}`], { cwd: common.rootDir }) task: () => execa('git', ['tag', `${tag}`], { cwd: common.rootDir })
}, },
{ {
title: 'Push branches to Github', title: 'Push branches to remote',
task: () => execa('git', ['push'], { cwd: common.rootDir }) task: () => execa('git', ['push'], { cwd: common.rootDir })
}, },
{ {
title: 'Push tags to Github', title: 'Push tags to remove',
task: () => execa('git', ['push', '--tags'], { cwd: common.rootDir }) task: () => execa('git', ['push', '--tags'], { cwd: common.rootDir })
},
{
title: 'Publish Github release',
tash: () => publishGithub(version, tag, changelog)
} }
); );
} }
function findChangelog() {
const lines = fs.readFileSync('CHANGELOG.md', 'utf-8').toString().split('\n');
let start = -1;
let end = -1;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.startsWith('# [')) {
if (start === -1) {
start = i + 1;
} else {
end = i - 1;
break;
}
}
}
if(start === -1 || end === -1) {
throw new Error('changelog diff was not found');
}
return lines.slice(start, end).join('\n').trim();
}
async function publishGithub(version, tag, changelog) {
octokit.authenticate({
type: 'oauth',
token: process.env.GH_TOKEN
});
await octokit.repos.createRelease({
owner: 'ionic-team',
repo: 'ionic',
target_commitish: 'master',
tag_name: tag,
name: version,
body: changelog,
});
}
main(); main();

907
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -9,12 +9,13 @@
"changelog": "conventional-changelog -p angular -i ./CHANGELOG.md -k core -s" "changelog": "conventional-changelog -p angular -i ./CHANGELOG.md -k core -s"
}, },
"devDependencies": { "devDependencies": {
"@octokit/rest": "^15.2.6",
"chalk": "^2.3.2", "chalk": "^2.3.2",
"conventional-changelog-cli": "^1.3.16", "conventional-changelog-cli": "^1.3.16",
"execa": "^0.10.0", "execa": "^0.10.0",
"fs-extra": "^5.0.0", "fs-extra": "^5.0.0",
"inquirer": "^5.1.0", "inquirer": "^5.1.0",
"listr": "^0.13.0", "listr": "^0.14.0",
"semver": "^5.5.0" "semver": "^5.5.0"
} }
} }