Skip to content
David Vegh edited this page Apr 30, 2022 · 10 revisions

Tutorial

write-npmstat makes it easy to collect, filter and save npm statistics to csv files.

Visit our documentation site for code reference.

Initilaize a new WriteNpmStat class

In these examples we initilaize a WriteNpmStat class in order to collect statistics about npm-stat-api npm package.

Parameters:

  • packageName: name of the target npm package
  • outDir: path of the directory where the gathered data will be saved into csv files

Example - Initilaize without outDir

Because outDir is null the gathered statistics will be only printed to the console.

const WriteNpmStat = require("write-npmstat").default;

const targetPackage = "npm-stat-api";
const writenpmstat = new WriteNpmStat(targetPackage);

Example - Initilaize with outDir

Because outDir is not null the gathered statistics will be saved into csv files too.

const WriteNpmStat = require("write-npmstat").default;

const targetPackage = "npm-stat-api";
const csvDir = "stats/npm-stat-api";
const writenpmstat = new WriteNpmStat(targetPackage, csvDir);

Properties of the WriteNpmStat class

Properties:

  • outDir: path of the directory where the gathered data will be saved into csv files
  • datePeriod: grouping of the statistics
  • writePackageName: flag used to write the name of the package into a csv column
  • mergeStoredData: flag used to merge actual npm statistics with previously stored

Example - Change outDir

outDir can be changed or set at anytime.

const WriteNpmStat = require("write-npmstat").default;

const targetPackage = "npm-stat-api";
const writenpmstat = new WriteNpmStat(targetPackage);

writenpmstat.outDir = "stats/npm-stat-api";

Example - Change datePeriod

const WriteNpmStat = require("write-npmstat").default;

const targetPackage = "npm-stat-api";
const writenpmstat = new WriteNpmStat(targetPackage);

writenpmstat.datePeriod = "month";

Example - Change writePackageName

const WriteNpmStat = require("write-npmstat").default;

const targetPackage = "npm-stat-api";
const writenpmstat = new WriteNpmStat(targetPackage);

writenpmstat.writePackageName = true;

Example - Change mergeStoredData

const WriteNpmStat = require("write-npmstat").default;

const targetPackage = "npm-stat-api";
const writenpmstat = new WriteNpmStat(targetPackage);

writenpmstat.mergeStoredData = false;

Methods of the WriteNpmStat class

getNpmStat

Parameters:

  • startDate: start date of the statistics
  • endDate: end date of the statistics

Example - Get npm statistics between 2022-03-01 and 2022-04-10

const WriteNpmStat = require("write-npmstat").default;

const targetPackage = "npm-stat-api";
const writenpmstat = new WriteNpmStat(targetPackage);

writenpmstat.getNpmStat("2022-03", "2022-04-10").then(stats => {
    console.log(stats);
});

getLastWeekNpmStat

Example - Get last week npm statistics

const WriteNpmStat = require("write-npmstat").default;

const targetPackage = "npm-stat-api";
const writenpmstat = new WriteNpmStat(targetPackage);

writenpmstat.getLastWeekNpmStat().then(stats => {
    console.log(stats);
});

writeNpmStat

Parameters:

  • startDate: start date of the statistics
  • endDate: end date of the statistics
  • postfix: postfix of the csv file

Example - Write npm statistics between 2022-01-01 and 2022-03-31, grouped by month

const WriteNpmStat = require("write-npmstat").default;

const targetPackage = "npm-stat-api";
const csvDir = "stats/npm-stat-api";
const writenpmstat = new WriteNpmStat(targetPackage, csvDir);

writenpmstat.datePeriod = "month";
writenpmstat.writeNpmStat("2022", "2022-03");

writeLastWeekNpmStat

Parameters:

  • postfix: postfix of the csv file

Example - Write last week npm statistics

const WriteNpmStat = require("write-npmstat").default;

const targetPackage = "npm-stat-api";
const csvDir = "stats/npm-stat-api";

const writenpmstat = new WriteLastWeekNpmStat(targetPackage, csvDir);

writenpmstat.writeLastWeekNpmStat();