npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eco-index-audit

v4.4.0

Published

This module will generate the ecoindex score for a website

Downloads

86

Readme

Eco Index Audit

Actions Status npm version Quality Gate Status

This tool is the CLI version of this website

Installation

If you just need to run an audit, you can use NPX

npx eco-index-audit --url=https://www.google.com/ --ecoIndex=50 --visits=2000 --output=table

npx eco-index-audit --url=https://www.google.com/ --ecoIndex=50 --visits=2000 --output=csv

npx eco-index-audit --url=https://www.google.com/ --ecoIndex=50 --visits=2000 --output=json

npx eco-index-audit --url=https://www.google.com/ --ecoIndex=50 --visits=2000 --output=sonar --sonarFilePath=index.html --outputPathDir=./reports

But you can also clone, install and run the project locally.

git clone https://github.com/EmmanuelDemey/eco-index-audit
cd eco-index-audit
npm i

Usage/Examples

node ./src/cli.js --url=https://www.google.com/ --ecoIndex=50 --visits=2000 --output=table
node ./src/cli.js --url=https://www.google.com/ --ecoIndex=50 --visits=2000 --output=csv
node ./src/cli.js --url=https://www.google.com/ --ecoIndex=50 --visits=2000 --output=json

CI Integration

You can execute this module directly from your CI. Here is an example for Github Actions :

name: Running Eco Index via a Github Action

on:
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 16
      - run: npm i -g eco-index-audit
      - run: npx eco-index-audit --url=https://www.google.com/ --ecoIndex=90 --output=table

Integration with Sonar

Since the version 3.3.0 the CLI can generate a external sonar report you can add to the Sonar configuration (via the sonar.externalIssuesReportPaths option).

You need to define the path to one of your file managed by Sonar, in order to make the rule visible in Sonar Cloud.

npx eco-index-audit --url=https://www.google.com/ --ecoIndex=50 --visits=2000 --output=sonar --sonarFilePath=index.html --outputPathDir=./reports

Integration with Cypress

For the moment, you can only use eco-index-audit inside a Cypress test suites running on a Chromium-based browser.

npx cypress run -b chrome

You are able to run this module during your Cypress test. The first step is to define a new task in the cypress.config.js file.

const { defineConfig } = require("cypress");
const { prepareAudit, checkEcoIndex } = require("eco-index-audit/src/cypress");

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on) {
      on("before:browser:launch", (_browser, launchOptions) => {
        prepareAudit(launchOptions);
      });

      on("task", {
        checkEcoIndex: ({ url, options }) => checkEcoIndex({ 
          url, 
          options: {
            headless: false,
            globals: { data: 'data'},
            beforeScript: (globals) => console.log(globals),
            afterScript: (globals) => console.log(globals),
          } 
        }),
      });
    },
  },
});

If the headless parameter is set to false, the UI will opened with the Devtools enabled and will automatically stopped running everything after loading the page (using a debugger statement),

The globals object can be used if you need to share some data during the execution of the beforeScript and afterScript hooks. This object will be available as a parameter to these two hooks.

Inside a Cypress test, eco-index-audit will reuse the same Chromium-based browser used by Cypress.

And then use this task inside your test. Inside your test, you can check if the ecoIndex is below a threshold.

describe('Cypress test', () => {
  const url = 'https://google.com'
  beforeEach(() => {
    cy.visit(url)
  })

  it('should have a good ecoindex', () => {
    const threshold = 50
    cy.task("checkEcoIndex", { url }).its('ecoIndex', { timeout: 0 }).should('be.greaterThan', threshold);
  })
})

If you have interceptors inside your Cypress test, your eco-index will be better than expected. In order to be as closed as the reality, you can update the initial values of the metrics we used to. For example, if you intercept 4 HTTP requests of 1024 bytes

describe('Cypress test', () => {
  const url = 'https://google.com'
  beforeEach(() => {
    cy.visit(url)
  })

  it('should have a good ecoindex', () => {
    const threshold = 50
    cy.task("checkEcoIndex", {
      url,
      initialValues: {
        numberOfRequests: 4,
        sizeOfRequests: 4 * 1024
      }
    }).its('ecoIndex', { timeout: 0 }).should('be.greaterThan', threshold);
  })
})

You can also define the outputPathDir option in order to save the result in a file. These properties are only use for the json and csv formats.

const path = require('path');

describe('Cypress test', () => {
  const url = 'https://google.com'
  beforeEach(() => {
    cy.visit(url)
  })

  const outputPathDir = path.join(__dirname, 'reports');

  it('should have a good ecoindex', () => {
    const threshold = 50
    cy.task("checkEcoIndex", {
      url,
      options: {
        output: "json",
        outputPathDir
      }
    }).its('ecoIndex', { timeout: 0 }).should('be.greaterThan', threshold);
  })
})

You can also add a timeout before closing the Puppeteer page in order to debug the page thank to the beforeClosingPageTimeout options. The value is in ms

describe('Cypress test', () => {
  const url = 'https://google.com'
  beforeEach(() => {
    cy.visit(url)
  })

  it('should have a good ecoindex', () => {
    const threshold = 50
    cy.task("checkEcoIndex", {
      url,
      options: {
        beforeClosingPageTimeout: 10000
      }
    }).its('ecoIndex', { timeout: 0 }).should('be.greaterThan', threshold);
  })
})

Configure via the eco-index-audit.js file

You can also configure this module via a eco-index-audit.js configuration file. This file should be located in the root folder of your project.

module.exports = {
  url: ["https://www.google.com/"],
  output: [
    "table"
  ]
};

Thanks to this file, you can enable dynamic output in order to archive results into any external service. In the following snippet, we will sotre the result into Elasticsearch.

module.exports = {
  url: ["https://www.google.com/"],
  output: [
    "table",
    (results) => {
      const { Client } = require("@elastic/elasticsearch");
      const client = new Client({
        cloud: {
          id: "...",
        },
        auth: {
          username: "...",
          password: "...",
        },
      });
      client.index({
        index: 'eco-index',
        document: results
      })
    },
  ],
};

Environment Variables

You can add environment variables in order to configure eco-index-audit:

  • ECOINDEX_VERBOSE: if set to true, will log more information in the terminal.
  • ECOINDEX_DISPLAY_HTML: if set to true, will log the HTML of the audited page.
ECOINDEX_VERBOSE=true npx eco-index-audit --url=https://www.google.com/ --ecoIndex=50 --visits=2000 --output=table

Tests

Update snapshots with npm test -- -u

Tech Stack

Typescript, Puppeteer, Cypress

Resources

Authors

Disclaimer

The LCA values used by ecoindex_api to evaluate environmental impacts are not under free license - ©Frédéric Bordage Please also refer to the mentions provided in the code files for specifics on the IP regime.