Skip to content

Commit

Permalink
Simplify error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mAAdhaTTah committed Nov 9, 2020
1 parent efc71b3 commit e483b26
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions dangerfile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { danger, markdown } = require('danger');
const { markdown } = require('danger');
const fs = require('fs').promises;
const gzipSize = require('gzip-size');
const git = require('simple-git/promise')(__dirname);
const git = require('simple-git/promise')(__dirname).silent(true);

// https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
const formatBytes = (bytes, decimals = 2) => {
Expand Down Expand Up @@ -56,34 +56,36 @@ const handleRenamedError = (file, type) => async err => {
throw err;
}

const run = async () => {
const minified = [
...danger.git.modified_files.map(file => ({ file, type: 'modified' })),
...danger.git.deleted_files.map(file => ({ file, type: 'deleted' })),
...danger.git.created_files.map(file => ({ file, type: 'created' })),
].filter(({ file }) => file.includes('.min.js'));
const getChangedMinifiedFiles = async () => {
const result = await git.diff(['--name-only', '--no-renames', 'master...']);

if (minified.length === 0) {
markdown(`## No JS Changes`);
return;
}
return result
? result.split(/\r?\n/g).filter(file => file.includes('.min.js'))
: [];
};

const run = async () => {
// Check if master exists & check it out if not.
const result = await git.branch(['--list', 'master']);
if (result.all.length === 0) {
await git.branch(['master', 'origin/master']);
}

const minified = await getChangedMinifiedFiles();

if (minified.length === 0) {
markdown(`## No JS Changes`);
return;
}

const rows = [];
let totalFileSize = 0;
let totalMasterFileSize = 0;

for (const { file, type } of minified) {
for (const file of minified) {
const [fileContents, fileMasterContents] = await Promise.all([
type !== 'deleted' ? fs.readFile(file, 'utf-8') : Promise.resolve(''),
type !== 'created' ?
git.show([`master:${file}`]).catch(handleRenamedError(file, type)) :
Promise.resolve(''),
fs.readFile(file, 'utf-8').catch(() => ''),
git.show([`master:${file}`]).catch(() => ''),
]);

const [fileSize, fileMasterSize] = await Promise.all([
Expand Down

0 comments on commit e483b26

Please sign in to comment.