-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
129 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
}; |