- Use async/await instead of callbacks - Use execa to run command line - Use AVA for tests - Add several assertions in the unit tests - Add documentation (comments) in the tests - Run tests with a real git repo instead of mocking child_process and add test helpers to create repos, commits and checkout - Simplify test directory structure - Simplify code readability (mostly with async/await) - Use eslint for for linting, prettier for formatting
39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
const {promisify} = require('util');
|
|
const url = require('url');
|
|
const gitHead = require('git-head');
|
|
const GitHubApi = require('github');
|
|
const parseSlug = require('parse-github-repo-url');
|
|
|
|
module.exports = async config => {
|
|
const {pkg, options: {branch, debug, githubUrl, githubToken, githubApiPathPrefix}, plugins} = config;
|
|
const [owner, repo] = parseSlug(pkg.repository.url);
|
|
const name = `v${pkg.version}`;
|
|
const tag = {owner, repo, ref: `refs/tags/${name}`, sha: await promisify(gitHead)()};
|
|
const body = await promisify(plugins.generateNotes)(config);
|
|
const release = {owner, repo, tag_name: name, name, target_commitish: branch, draft: !!debug, body};
|
|
|
|
if (debug && !githubToken) {
|
|
return {published: false, release};
|
|
}
|
|
|
|
const {port, protocol, hostname} = githubUrl ? url.parse(githubUrl) : {};
|
|
const github = new GitHubApi({
|
|
port,
|
|
protocol: (protocol || '').split(':')[0] || null,
|
|
host: hostname,
|
|
pathPrefix: githubApiPathPrefix || null,
|
|
});
|
|
|
|
github.authenticate({type: 'token', token: githubToken});
|
|
|
|
if (debug) {
|
|
await github.repos.createRelease(release);
|
|
return {published: true, release};
|
|
}
|
|
|
|
await github.gitdata.createReference(tag);
|
|
await github.repos.createRelease(release);
|
|
|
|
return {published: true, release};
|
|
};
|