Skip to content

Commit

Permalink
Add support for added & deleted files
Browse files Browse the repository at this point in the history
  • Loading branch information
mAAdhaTTah committed Nov 8, 2020
1 parent 25082c6 commit 423ac9f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions components/prism-abap-rename.min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prism.languages.abap={comment:/^\*.*/m,string:/(`|')(?:\\.|(?!\1)[^\\\r\n])*\1/m,};
35 changes: 25 additions & 10 deletions dangerfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const formatBytes = (bytes, decimals = 2) => {
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

const i = Math.floor(Math.log(bytes) / Math.log(k));
const i = Math.floor(Math.log(Math.abs(bytes)) / Math.log(k));

return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
Expand All @@ -32,7 +32,7 @@ const percDiff = (from, to) => {
}

return `${maybePlus(from, to)}${
(100 * (to - from) / ((from + to) / 2 )).toFixed(1)
(100 * (to - from) / (from || to)).toFixed(1)
}%`;
}

Expand All @@ -45,8 +45,23 @@ const getSummary = (rows, totalMasterFileSize, totalFileSize) => {
return `A total of ${numFiles} file${maybeS} have changed, with a diff of ${byteDiff} (${percentDiff}).`;
}

const handleRenamedError = (file, type) => async err => {
// Renamed files show up in modified but don't exist in master,
// so this is the only error here we want to handle.
if (type === 'modified' && err.message.includes("exists on disk, but not in 'master'")) {
return '';
}

// Otherwise, rethrow, cuz we haven't handled any other errors.
throw err;
}

const run = async () => {
const minified = danger.git.modified_files.filter(file => file.includes('.min.js'));
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'));

if (minified.length === 0) {
markdown(`## No JS Changes`);
Expand All @@ -63,10 +78,12 @@ const run = async () => {
let totalFileSize = 0;
let totalMasterFileSize = 0;

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

const [fileSize, fileMasterSize] = await Promise.all([
Expand All @@ -86,15 +103,13 @@ const run = async () => {
]);
}

markdown(`
${getSummary(rows,totalMasterFileSize, totalFileSize)}
markdown(`${getSummary(rows, totalMasterFileSize, totalFileSize)}
## JS File Size Changes (gzipped)
| file | master | pull | size diff | % diff |
| --- | --- | --- | --- | --- |
${rows.map(row => `| ${row.join(' | ')} |`).join('\n')}
`);
${rows.map(row => `| ${row.join(' | ')} |`).join('\n')}`);
}

run().catch(err => {
Expand Down

0 comments on commit 423ac9f

Please sign in to comment.