Skip to content

Commit

Permalink
Merge 2439b5d into 2f5b2be
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkli committed Oct 27, 2020
2 parents 2f5b2be + 2439b5d commit 59842eb
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 1.1.0

New Comparators:

- [#14](https://github.com/adobe/sizewatcher/issues/14) npm package size

Improvements:

- [#32](https://github.com/adobe/sizewatcher/issues/32) Improved PR comment that can be collapsed as whole, shows the summary result and is collapsed by default if the result is ok.
Expand Down
56 changes: 52 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@
- addition of large dependencies (and transient dependency trees)
- accidental addition of large binary files to the git repository
- sudden increase in build artifact size
- etc.

Currently supported are git repository size itself, Node.js/npm modules and measuring any custom files or folders - see [Comparators reference](#comparators-reference). More built-in languages & options are possible in the future.
While any custom file or folder path can be measured via configuration, various types are automatically measured, including git repository, Node module dependencies and npm package size - see [comparators reference](#comparators-reference). More built-in languages & options are added over time and [contributions are welcome](#new-comparator).

`sizewatcher` runs as part of your CI and reports results as comment on the pull request or as github commit status (optional), allowing to block PRs if a certain threshold was exceeded.

This is an example of a Github PR comment with a failure (ignore the small numbers):
This is an example of a `sizewatcher` Github PR comment with a failure (ignore the small numbers):

---

Expand Down Expand Up @@ -73,7 +72,7 @@ comparators:

---

And here if everything is great:
And here if everything looks good:

---

Expand Down Expand Up @@ -228,6 +227,12 @@ To run `sizewatcher` in your CI, which is where it needs to run automatically fo
npx @adobe/sizewatcher
```

This command will always use the latest published version. In some cases it might be (temporarily) desireable to stick to a certain version, which can be achieved using:

```
npx @adobe/sizewatcher@1.0.0
```

#### GITHUB_TOKEN

To be able to automatically comment on the PR or report a commit status, a **github token** must be set as environment variable:
Expand Down Expand Up @@ -413,6 +418,7 @@ comparators:

- [git](#git)
- [node_modules](#node_modules)
- [npm_package](#npm_package)
- [custom](#custom)

### git
Expand Down Expand Up @@ -477,6 +483,48 @@ Largest node modules:

---

### npm_package

Compares the size of an npm package tarball by running `npm publish --dry-run`.

Name: `npm_package`

Trigger: Runs if a `package.json` is found.

Details: Prints the package contents and metadata using the output of `npm publish --dry-run`.

---
Package contents:

```
📦 @adobe/sizewatcher@1.0.0
=== Tarball Contents ===
11.3kB LICENSE
4.8kB lib/checkout.js
5.2kB lib/compare.js
1.7kB lib/config.js
3.9kB lib/comparators/custom.js
2.3kB lib/comparators/git.js
2.6kB lib/github.js
2.1kB index.js
2.2kB lib/comparators/node_modules.js
3.9kB lib/render.js
4.6kB lib/report.js
1.1kB package.json
695B CHANGELOG.md
23.9kB README.md
=== Tarball Details ===
name: @adobe/sizewatcher
version: 1.0.0
package size: 18.6 kB
unpacked size: 70.3 kB
shasum: 80846caccca2194f3dd1122e8113206e20c202dc
integrity: sha512-c3VjMQQvqcqN8[...]ybMS6kg2chjpA==
total files: 14
```

---

### custom

Compares the size of a custom file or folder.
Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

// index.js - main cli entry point

const debug = require("debug")("sizewatcher");
const gitCheckoutBeforeAndAfter = require("./lib/checkout");
const compare = require("./lib/compare");
const report = require("./lib/report");
const path = require("path");
const fs = require("fs");

function printUsage() {
console.log(`Usage: ${process.argv[1]} [<options>] [<before> [<after>]]`);
Expand All @@ -40,6 +43,10 @@ async function main(argv) {
process.exit(1);
}

if (!fs.existsSync(path.join(process.cwd(), ".git"))) {
throw new Error(`Not inside the root of a git checkout: ${process.cwd()}`);
}

// TODO: detect main = main case

console.log(`Cloning git repository...`);
Expand All @@ -52,7 +59,8 @@ async function main(argv) {
await report(deltas);

} catch (e) {
console.error(e);
debug(e);
console.error("Error:", e.message || e);
process.exit(1);
} finally {
console.log("Done. Cleaning up...");
Expand Down
64 changes: 64 additions & 0 deletions lib/comparators/npm_package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

'use strict';

// comparators/npm_package.js - compares npm package (tarball) size

const debug = require("debug")("sizewatcher:node_modules");
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const path = require("path");
const fs = require("fs");
const xbytes = require("xbytes");

const PACKSIZE_REGEX = /package size: (.*)/;

async function getSize(dir) {
if (!fs.existsSync(path.join(dir, "package.json"))) {
return {
size: 0
};
}

debug(`running npm publish --dry-run inside ${dir}...`);
let { stderr } = await exec("npm publish --dry-run", { cwd: dir });

stderr = stderr.replace(/npm notice /g, "");

const found = stderr.match(PACKSIZE_REGEX);
const size = found ? xbytes.parseSize(found[1].trim()) : 0;

return {
size,
output: stderr
};
}

module.exports = {

shouldRun: async function(beforeDir, afterDir) {
return fs.existsSync(path.join(afterDir, "package.json"));
},

compare: async function(before, after) {
const beforeResult = await getSize(before.dir);
const afterResult = await getSize(after.dir);

return {
beforeSize: beforeResult.size,
afterSize: afterResult.size,
detailsLabel: "Package contents",
details: afterResult.output
};
}
};

0 comments on commit 59842eb

Please sign in to comment.