insomnia/packages/insomnia-app/scripts/release.js

95 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-04-26 20:33:39 +00:00
const { appConfig } = require('../config');
const glob = require('fast-glob');
const fs = require('fs');
const path = require('path');
const packageTask = require('./package');
const buildTask = require('./build');
2020-04-26 20:33:39 +00:00
const { Octokit } = require('@octokit/rest');
// Configure Octokit
const octokit = new Octokit({
2020-04-26 20:33:39 +00:00
auth: process.env.GH_TOKEN,
});
// Start package if ran from CLI
if (require.main === module) {
process.nextTick(async () => {
try {
const buildContext = await buildTask.start(true);
await packageTask.start();
2020-04-26 20:33:39 +00:00
await start(buildContext.app, buildContext.version);
} catch (err) {
console.log('[release] ERROR:', err);
process.exit(1);
}
});
}
2020-04-26 20:33:39 +00:00
async function start(app, version) {
console.log(`[release] Creating release for ${app} ${version}`);
const appId = appConfig().appId;
const distGlob = ext => path.join('dist', appId, '**', `*${ext}`);
2020-04-26 20:33:39 +00:00
const assetGlobs = {
darwin: [distGlob('.zip'), distGlob('.dmg')],
win32: [path.join('dist', appId, 'squirrel-windows', '*')],
linux: [
distGlob('.snap'),
distGlob('.rpm'),
distGlob('.deb'),
distGlob('.AppImage'),
distGlob('.tar.gz'),
],
};
2020-04-26 20:33:39 +00:00
const paths = await glob(assetGlobs[process.platform]);
2020-04-26 20:33:39 +00:00
const { data } = await getOrCreateRelease(app, version);
for (const p of paths) {
const name = path.basename(p);
2020-04-26 20:33:39 +00:00
console.log(`[release] Uploading ${name}`);
await octokit.request({
method: 'POST',
url: 'https://uploads.github.com/repos/:owner/:repo/releases/:id/assets{?name,label}"',
id: data.id,
name: name,
2020-04-26 20:33:39 +00:00
owner: appConfig().githubOrg,
repo: appConfig().githubRepo,
headers: {
'Content-Type': 'application/octet-stream',
},
data: fs.readFileSync(p),
});
}
console.log(`[release] Release created ${data.url}`);
}
2020-04-26 20:33:39 +00:00
async function getOrCreateRelease(app, version) {
const tag = `${app}@${version}`;
const releaseName = `${appConfig().productName} ${version} 📦`;
const changelogUrl = `https://insomnia.rest/changelog/${app}/${version}`;
try {
return await octokit.repos.getReleaseByTag({
2020-04-26 20:33:39 +00:00
owner: appConfig().githubOrg,
repo: appConfig().githubRepo,
tag,
});
} catch (err) {
// Doesn't exist
}
return octokit.repos.createRelease({
2020-04-26 20:33:39 +00:00
owner: appConfig().githubOrg,
repo: appConfig().githubRepo,
tag_name: tag,
name: releaseName,
body: `Full changelog ⇒ ${changelogUrl}`,
draft: false,
2020-04-26 20:33:39 +00:00
prerelease: true,
});
}