Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transformFilePath option #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/plugins/size.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ const POSITIVE_EMOJI = '✅';
const NEGATIVE_EMOJI = '🚫';

class SizePlugin extends PluginInterface {
constructor({globPattern, globOptions} = {}) {
constructor({globPattern, globOptions, transformFilePath} = {}) {
super('PR-Bot Size Plugin');

this._globPattern = globPattern;
this._globOptions = globOptions;
this._transformFilePath = transformFilePath;
}

run({beforePath, afterPath} = {}) {
Expand Down Expand Up @@ -107,8 +108,9 @@ class SizePlugin extends PluginInterface {
const fileContents = fs.readFileSync(filePath);
const gzippedSize = gzipSize.sync(fileContents);
const relativePath = path.relative(directory, filePath);
fileInfo[relativePath] = {
relativePath,
const transformedRelativePath = this._transformFilePath ? this._transformFilePath(relativePath) : relativePath;
fileInfo[transformedRelativePath] = {
relativePath: transformedRelativePath,
sizeInBytes: stats.size,
gzipSizeInBytes: gzippedSize,
};
Expand Down
83 changes: 81 additions & 2 deletions test/node/plugins/test-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ stays-the-same.txt 29 B > 50 B +72%

New Files
---------
new-file.txt 21 B
hashed.asdf123.txt 1.67 KB
new-file.txt 21 B
`);

expect(results.markdownLog).to.exist;
Expand All @@ -100,7 +101,8 @@ new-file.txt 21 B

| File | Size | GZipped |
| --- | --- | --- |
| new-file.txt | 21 B | 41 B |
| hashed.asdf123.txt | 1.67 KB | 475 B |
| new-file.txt | 21 B | 41 B |

#### All File Sizes

Expand All @@ -113,6 +115,7 @@ new-file.txt 21 B
| dino.jpg | 268.63 KB | 104.00 KB | -61% | 103.24 KB | 🎉 |
| empty-to-content.txt | 0 B | 38 B | +Infinity% | 56 B | ☠️ |
| empty.txt | 0 B | 0 B | | 20 B | |
| hashed.asdf123.txt | | 1.67 KB | | 475 B | |
| minor-change.txt | 7.13 KB | 7.13 KB | -0% | 2.52 KB | |
| new-file.txt | | 21 B | | 41 B | |
| stays-the-same.txt | 29 B | 50 B | +72% | 67 B | ☠️ |
Expand Down Expand Up @@ -168,6 +171,7 @@ No new files have been added.
| dino.jpg | 104.00 KB | 104.00 KB | 0% | 103.24 KB | |
| empty-to-content.txt | 38 B | 38 B | 0% | 56 B | |
| empty.txt | 0 B | 0 B | | 20 B | |
| hashed.asdf123.txt | 1.67 KB | 1.67 KB | 0% | 475 B | |
| minor-change.txt | 7.13 KB | 7.13 KB | 0% | 2.52 KB | |
| new-file.txt | 21 B | 21 B | 0% | 41 B | |
| stays-the-same.txt | 50 B | 50 B | 0% | 67 B | |
Expand All @@ -176,6 +180,81 @@ No new files have been added.
});
})


it.only('should handle hashed files', function() {
const plugin = new SizePlugin({
globPattern: '**/*',
transformFilePath: (path) => path.replace(/(.+?\.)(.+?\.)?(txt|jpg)/i, '$1$3'),
});
return plugin.run({
beforePath: path.join(__dirname, '..', '..', 'static', 'size-example-before'),
afterPath: path.join(__dirname, '..', '..', 'static', 'size-example-after'),
})
.then((results) => {
expect(results.prettyLog).to.exist;

console.log(results.prettyLog);

// Print all logs when nothings changed.
const cleanLog = results.prettyLog.replace(
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
'');
expect(cleanLog).to.equal(`
Changed File Sizes
------------------
content-to-empty.txt 45 B > 0 B -100%
dino.jpg 268.63 KB > 104.00 KB -61%
empty-to-content.txt 0 B > 38 B +Infinity%
hashed.txt 834 B > 1.67 KB +100%
minor-change.txt 7.13 KB > 7.13 KB -0%
stays-the-same.txt 29 B > 50 B +72%

New Files
---------
new-file.txt 21 B
`);

expect(results.markdownLog).to.exist;

console.log(results.markdownLog);

expect(results.markdownLog).to.equal(`#### Changed File Sizes

| File | Before | After | Change | GZipped | |
| --- | --- | --- | --- | --- | --- |
| content-to-empty.txt | 45 B | 0 B | -100% | 20 B | 🎉 |
| dino.jpg | 268.63 KB | 104.00 KB | -61% | 103.24 KB | 🎉 |
| empty-to-content.txt | 0 B | 38 B | +Infinity% | 56 B | ☠️ |
| hashed.txt | 834 B | 1.67 KB | +100% | 475 B | ☠️ |
| minor-change.txt | 7.13 KB | 7.13 KB | -0% | 2.52 KB | |
| stays-the-same.txt | 29 B | 50 B | +72% | 67 B | ☠️ |

#### New Files

| File | Size | GZipped |
| --- | --- | --- |
| new-file.txt | 21 B | 41 B |

#### All File Sizes

<details>
<summary>View Table</summary>

| File | Before | After | Change | GZipped | |
| --- | --- | --- | --- | --- | --- |
| content-to-empty.txt | 45 B | 0 B | -100% | 20 B | 🎉 |
| dino.jpg | 268.63 KB | 104.00 KB | -61% | 103.24 KB | 🎉 |
| empty-to-content.txt | 0 B | 38 B | +Infinity% | 56 B | ☠️ |
| empty.txt | 0 B | 0 B | | 20 B | |
| hashed.txt | 834 B | 1.67 KB | +100% | 475 B | ☠️ |
| minor-change.txt | 7.13 KB | 7.13 KB | -0% | 2.52 KB | |
| new-file.txt | | 21 B | | 41 B | |
| stays-the-same.txt | 29 B | 50 B | +72% | 67 B | ☠️ |

</details>`);
});
});

it('should return 1KB', function() {
const result = SizePlugin._convertSize(1000);
expect(result).to.deep.equal({
Expand Down
3 changes: 3 additions & 0 deletions test/static/size-example-after/hashed.asdf123.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras auctor libero vitae elementum volutpat. Ut porta dapibus accumsan. Donec id massa ac nunc placerat dignissim. Proin vulputate tincidunt odio et dignissim. Phasellus vel porttitor massa, varius tincidunt nisl. Fusce ornare mauris sit amet odio cursus, sed tincidunt velit porttitor. Curabitur congue, lorem vel sollicitudin hendrerit, justo nisl posuere quam, eget euismod est ex eget enim. In molestie sapien sit amet purus iaculis, eget viverra dui scelerisque. Sed pulvinar libero nec urna bibendum finibus. Nulla elementum auctor molestie. Vestibulum et magna at ante luctus scelerisque. Suspendisse imperdiet erat nec purus mattis, at euismod mauris porta. Aenean non nisi vel turpis semper aliquam. Praesent dui ipsum, congue eget sollicitudin id, tincidunt non nisl.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras auctor libero vitae elementum volutpat. Ut porta dapibus accumsan. Donec id massa ac nunc placerat dignissim. Proin vulputate tincidunt odio et dignissim. Phasellus vel porttitor massa, varius tincidunt nisl. Fusce ornare mauris sit amet odio cursus, sed tincidunt velit porttitor. Curabitur congue, lorem vel sollicitudin hendrerit, justo nisl posuere quam, eget euismod est ex eget enim. In molestie sapien sit amet purus iaculis, eget viverra dui scelerisque. Sed pulvinar libero nec urna bibendum finibus. Nulla elementum auctor molestie. Vestibulum et magna at ante luctus scelerisque. Suspendisse imperdiet erat nec purus mattis, at euismod mauris porta. Aenean non nisi vel turpis semper aliquam. Praesent dui ipsum, congue eget sollicitudin id, tincidunt non nisl.
1 change: 1 addition & 0 deletions test/static/size-example-before/hashed.sadf274.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras auctor libero vitae elementum volutpat. Ut porta dapibus accumsan. Donec id massa ac nunc placerat dignissim. Proin vulputate tincidunt odio et dignissim. Phasellus vel porttitor massa, varius tincidunt nisl. Fusce ornare mauris sit amet odio cursus, sed tincidunt velit porttitor. Curabitur congue, lorem vel sollicitudin hendrerit, justo nisl posuere quam, eget euismod est ex eget enim. In molestie sapien sit amet purus iaculis, eget viverra dui scelerisque. Sed pulvinar libero nec urna bibendum finibus. Nulla elementum auctor molestie. Vestibulum et magna at ante luctus scelerisque. Suspendisse imperdiet erat nec purus mattis, at euismod mauris porta. Aenean non nisi vel turpis semper aliquam. Praesent dui ipsum, congue eget sollicitudin id, tincidunt non nisl.