• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

adobe / sizewatcher / 14210030476

02 Apr 2025 02:14AM UTC coverage: 58.752%. Remained the same
14210030476

Pull #112

github

web-flow
Merge 43ee9e512 into 91dff1b30
Pull Request #112: fix(node_modules): replace broken cost-of-modules with howfat

112 of 239 branches covered (46.86%)

Branch coverage included in aggregate %.

2 of 3 new or added lines in 1 file covered. (66.67%)

1 existing line in 1 file now uncovered.

321 of 498 relevant lines covered (64.46%)

4.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.38
/lib/comparators/node_modules.js
1
/*
2
 * Copyright 2020 Adobe. All rights reserved.
3
 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License. You may obtain a copy
5
 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
 *
7
 * Unless required by applicable law or agreed to in writing, software distributed under
8
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
 * OF ANY KIND, either express or implied. See the License for the specific language
10
 * governing permissions and limitations under the License.
11
 */
12

13
'use strict';
14

15
// comparators/node_modules.js - compares nodejs/npm module dependency size (node_modules folder)
16

17
const debug = require("debug")("sizewatcher:node_modules");
1✔
18
const { execSync } = require("child_process");
1✔
19
const path = require("path");
1✔
20
const fs = require("fs");
1✔
21
const { getFileSize } = require("../size");
1✔
22

23

24
function isEmptyObject(obj) {
25
    return Object.keys(obj).length === 0 && obj.constructor === Object;
1!
26
}
27

28
function hasPackageJson(dir) {
29
    return fs.existsSync(path.join(dir, "package.json"));
22✔
30
}
31

32
async function getSize(dir) {
33
    if (!hasPackageJson(dir)) {
8✔
34
        return 0;
4✔
35
    }
36
    process.chdir(dir);
4✔
37

38
    debug(`installing node dependencies inside ${dir}...`);
4✔
39
    if (fs.existsSync("package-lock.json") || fs.existsSync("npm-shrinkwrap.json")) {
4!
40
        execSync("npm ci");
×
41
    } else {
42
        execSync("npm install");
4✔
43
    }
44

45
    const nodeModules = path.join(dir, "node_modules");
4✔
46
    if (fs.existsSync(nodeModules)) {
4✔
47
        debug(`calculating folder size of ${nodeModules}...`);
2✔
48
        return getFileSize(nodeModules);
2✔
49
    } else {
50
        return 0;
2✔
51
    }
52
}
53

54
async function costOfModules(dir) {
55
    if (!hasPackageJson(dir)) {
4✔
56
        return "(no package.json)";
2✔
57
    }
58
    process.chdir(dir);
2✔
59

60
    const pkgJsonPath = path.join(dir, "package.json");
2✔
61
    const pkgJson = require(pkgJsonPath);
2✔
62
    if (!pkgJson.dependencies || isEmptyObject(pkgJson.dependencies)) {
2✔
63
        return "(no production dependencies)";
1✔
64
    }
65

66
    debug(`running npx howfat...`);
1✔
67
    try {
1✔
68
        const out = execSync("npx howfat -r table --sort size-").toString();
1✔
69
        const PREFIX = "\nCalculating...\n\n\n";
1✔
70
        if (out.startsWith(PREFIX)) {
1!
UNCOV
71
            return out.substring(PREFIX.length);
×
72
        }
73
        return out;
1✔
74
    } catch (e) {
NEW
75
        throw new Error(`howfat failed with: ${e.stdout ? e.stdout.toString() : ""} ${e.stdout ? e.stderr.toString() : ""}`);
×
76
    }
77

78
}
79

80
module.exports = {
1✔
81

82
    shouldRun: async function(beforeDir, afterDir) {
83
        return hasPackageJson(beforeDir) || hasPackageJson(afterDir);
6✔
84
    },
85

86
    compare: async function(before, after) {
87
        const beforeSize = await getSize(before.dir);
4✔
88
        const afterSize = await getSize(after.dir);
4✔
89
        const details = await costOfModules(after.dir);
4✔
90

91
        return {
4✔
92
            beforeSize,
93
            afterSize,
94
            detailsLabel: "Largest production node modules",
95
            details
96
        };
97
    }
98
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc