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

94 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}`);
2020-09-14 20:55:18 +00:00
// globs should only use forward slash, so force use of path.posix
const distGlob = ext => path.posix.join('dist', '**', `*${ext}`);
2020-04-26 20:33:39 +00:00
const assetGlobs = {
darwin: [distGlob('.zip'), distGlob('.dmg')],
win32: [path.posix.join('dist', 'squirrel-windows', '*'), path.posix.join('dist', '*.exe')],
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} 📦`;
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,
2021-02-25 21:33:38 +00:00
body: `Full changelog ⇒ ${appConfig().changelogUrl}`,
draft: false,
2020-04-26 20:33:39 +00:00
prerelease: true,
});
}