Skip to content

Commit

Permalink
refactor(/lib/release.js): use async/await instead of callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGenash committed Aug 3, 2020
1 parent 1013e3a commit 720fbb1
Showing 1 changed file with 114 additions and 140 deletions.
254 changes: 114 additions & 140 deletions lib/release/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,115 +4,66 @@ const os = require('os');
const uuid = require('uuid4');
const fs = require('fs');
const path = require('path');
const util = require('util');
const themePath = process.cwd();
const git = require('simple-git')(themePath);
const git = require('simple-git/promise')(themePath);
const themeConfig = require('../theme-config').getInstance(themePath);
const questions = require('./questions');
const askQuestions = require('./questions');
const { Octokit } = require('@octokit/rest');
const Bundle = require('../stencil-bundle');

module.exports = () => {
module.exports = async () => {
const gitData = await getGitData();

getGitData((err, gitData) => {
if (err) {
throw err;
}

if (gitData.remotes.length === 0) {
return printError('No git remote repository found');
}

if (gitData.current !== 'master') {
return printError('Not on master branch, please checkout master branch to proceed');
}

if (gitData.dirty) {
return printError('git tree is dirty, please commit changes to proceed');
}

if (gitData.behind !== 0) {
return printError(`Your branch is behind by ${gitData.behind} commits`);
}

if (gitData.ahead !== 0) {
printWarning(`your branch is ahead by ${gitData.ahead} commits`);
}
if (!checkGitData(gitData)) {
return;
}

questions(themeConfig, getGithubToken(), gitData.remotes, (err, answers) => {
if (err) {
return printError(err.message);
}
try {
const answers = await util.promisify(askQuestions)(themeConfig, getGithubToken(), gitData.remotes);

saveGithubToken(answers.githubToken);
saveGithubToken(answers.githubToken);

doRelease(answers, err => {
if (err) {
return printError(err.message);
}
await doRelease(answers);

console.log('done'.green);
});
});
});
console.log('done'.green);
} catch (err) {
return printError(err.message);
}
};

function doRelease(options, callback) {
async function doRelease(options) {
// Update changelog and get text for release notes
const changelog = parseChangelog(options.version, options.date);

bumpJsonFileVersion(path.join(themePath, 'config.json'), options.version);
bumpJsonFileVersion(path.join(themePath, 'package.json'), options.version);

bundleTheme((err, bundlePath) => {
if (err) {
return callback(err);
}
const bundlePath = await bundleTheme();

commitAndPush(options.version, options.remote, (err, commit) => {
if (err) {
fs.unlinkSync(bundlePath);
return callback(err);
}

createRelease(options, commit, bundlePath, changelog, err => {
fs.unlinkSync(bundlePath);
if (err) {
return callback(err);
}

callback();
});
});
});
}
try {
const commit = await commitAndPush(options.version, options.remote);

function commitAndPush(version, remote, callback) {
git.add(['config.json', 'package.json', 'CHANGELOG.md']).commit(`Releasing ${version}`, (err, summary) => {
if (err) {
return callback(err);
if (options.createGithubRelease) {
await createGithubRelease(commit, options.version, changelog, options.remote, bundlePath);
}
} finally {
fs.unlinkSync(bundlePath);
}
}

console.log(`Pushing Changes to ${remote.name}...`);
async function commitAndPush(version, remote) {
await git.add(['config.json', 'package.json', 'CHANGELOG.md']);
const summary = await git.commit(`Releasing ${version}`);

git.push(remote.name, 'master', err => {
if (err) {
return callback(err);
}
console.log(`Pushing Changes to ${remote.name}...`);

callback(null, summary.commit);
});
});
}
await git.push(remote.name, 'master');

function createRelease(options, commit, bundlePath, changelog, callback) {
if (options.createGithubRelease) {
createGithubRelease(commit, options.version, changelog, options.remote, bundlePath, callback);
} else {
process.nextTick(callback);
}
return summary.commit;
}

function createGithubRelease(commit, version, changelog, remote, bundlePath, callback) {
async function createGithubRelease(commit, version, changelog, remote, bundlePath) {
const github = getGithubClient();

const releaseParams = {
Expand All @@ -125,28 +76,22 @@ function createGithubRelease(commit, version, changelog, remote, bundlePath, cal

console.log('Creating Github Release...');

github.repos.createRelease(releaseParams)
.then(release => {
const uploadParams = {
release_id: release.data.id,
owner: remote.owner,
repo: remote.repo,
data: bundlePath,
name: `${themeConfig.getName()}-${version}.zip`,
};

console.log('Uploading Bundle File...');

github.repos.uploadReleaseAsset(uploadParams)
.then(asset => {
console.log(`Release url: ${release.data.html_url.green}`);
console.log(`Bundle download url: ${asset.data.browser_download_url.green}`);

callback();
})
.catch(callback);
})
.catch(callback);
const release = await github.repos.createRelease(releaseParams);

const uploadParams = {
release_id: release.data.id,
owner: remote.owner,
repo: remote.repo,
data: bundlePath,
name: `${themeConfig.getName()}-${version}.zip`,
};

console.log('Uploading Bundle File...');

const asset = await github.repos.uploadReleaseAsset(uploadParams);

console.log(`Release url: ${release.data.html_url.green}`);
console.log(`Bundle download url: ${asset.data.browser_download_url.green}`);
}

function bumpJsonFileVersion(filePath, version) {
Expand All @@ -158,7 +103,7 @@ function bumpJsonFileVersion(filePath, version) {

function parseChangelog(version, date) {
const filePath = path.join(themePath, 'CHANGELOG.md');
var changelog = '';
let changelog = '';

try {
changelog = fs.readFileSync(filePath).toString();
Expand All @@ -182,7 +127,7 @@ function parseChangelog(version, date) {

function saveGithubToken(githubToken) {
const dotStencilPath = path.join(themePath, '.stencil');
var data = {};
let data = {};

try {
data = JSON.parse(fs.readFileSync(dotStencilPath));
Expand All @@ -196,7 +141,7 @@ function saveGithubToken(githubToken) {

function getGithubToken() {
const dotStencilPath = path.join(themePath, '.stencil');
var data = {};
let data = {};

try {
data = JSON.parse(fs.readFileSync(dotStencilPath));
Expand All @@ -207,48 +152,77 @@ function getGithubToken() {
return data.githubToken;
}

function bundleTheme(callback) {
async function bundleTheme() {
const bundle = new Bundle(themePath, themeConfig, themeConfig.getRawConfig(), {
dest: os.tmpdir(),
name: uuid(),
});

bundle.initBundle(callback);
return await util.promisify(bundle.initBundle.bind(bundle))();
}

function getGitData(callback) {
git.status((err, status) => {
if (err) {
return callback(err);
}
async function getGitData() {
const status = await git.status();

const response = await git.getRemotes(true);

git.getRemotes(true, (err, response) => {
if (err) {
return callback(err);
}

const remotes = response.map(remote => {
const url = remote.refs.push || '';
const match = url.match(/github\.com[\/|:](.+?)\/(.+?)[\/|\.]/);

return {
name: remote.name,
url,
owner: match ? match[1] : null,
repo: match ? match[2] : null,
};
});

const dirty = status.not_added.length > 0 ||
status.conflicted.length > 0 ||
status.created.length > 0 ||
status.deleted.length > 0 ||
status.modified.length > 0 ||
status.renamed.length > 0;

callback(null, Object.assign({ dirty }, status, { remotes }));
});
const remotes = response.map(remote => {
const url = remote.refs.push || '';
const match = url.match(/github\.com[\/|:](.+?)\/(.+?)[\/|\.]/);

return {
name: remote.name,
url,
owner: match ? match[1] : null,
repo: match ? match[2] : null,
};
});

const dirty = status.not_added.length > 0 ||
status.conflicted.length > 0 ||
status.created.length > 0 ||
status.deleted.length > 0 ||
status.modified.length > 0 ||
status.renamed.length > 0;

return Object.assign({ dirty }, status, { remotes });
}

/**
* @param gitData
* @param gitData.remotes
* @param gitData.current
* @param gitData.dirty
* @param gitData.behind
* @param gitData.ahead
* @returns {boolean}
*/
function checkGitData (gitData) {
if (gitData.remotes.length === 0) {
printError('No git remote repository found');
return false;
}

if (gitData.current !== 'master') {
printError('Not on master branch, please checkout master branch to proceed');
return false;
}

if (gitData.dirty) {
printError('git tree is dirty, please commit changes to proceed');
return false;
}

if (gitData.behind !== 0) {
printError(`Your branch is behind by ${gitData.behind} commits`);
return false;
}

if (gitData.ahead !== 0) {
printWarning(`your branch is ahead by ${gitData.ahead} commits`);
}

return true;
}

function printError(message) {
Expand Down

0 comments on commit 720fbb1

Please sign in to comment.