Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const path = require("path");

export default [
{
input: path.resolve(__dirname, "./src/writenpmstat.js"),
input: path.resolve(__dirname, "./src/npmstat.js"),
output: {
file: path.resolve(__dirname, "./dist/index.min.js"),
format: "cjs",
Expand Down
46 changes: 37 additions & 9 deletions src/writenpmstat.js → src/npmstat.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ const StatPeriod = new Enum(
* @property {string|null} [outDir] - path of the directory where
* the gathered data will be saved into csv files
* @property {StatPeriod} [datePeriod=year] - grouping of the statistics
* @property {boolean} [writePackageName=false] - flag used to write the name of the package into a csv column
* @property {boolean} [mergeStoredData=true] - flag used to merge actual npm statistics with previously stored
*/
class WriteNpmStat {
#packageName;
outDir;

#datePeriod;
#writePackageName;
#mergeStoredData;

/**
Expand All @@ -47,6 +49,7 @@ class WriteNpmStat {
this.outDir = outDir;

this.#datePeriod = StatPeriod.year;
this.#writePackageName = false;
this.#mergeStoredData = true;
}

Expand All @@ -62,6 +65,14 @@ class WriteNpmStat {
this.#datePeriod = StatPeriod.get(datePeriod);
}

get writePackageName() {
return this.#writePackageName;
}

set writePackageName(writePackageName) {
this.#writePackageName = Boolean(writePackageName);
}

get mergeStoredData() {
return this.#mergeStoredData;
}
Expand Down Expand Up @@ -128,7 +139,14 @@ class WriteNpmStat {
}
throw new Error("retryLimit reached");
}
return resolve([res.start, res.downloads]);
const statKey = res.start;
const statValues = [];
statValues.push(res.start);
if (this.writePackageName) {
statValues.push(this.#packageName);
}
statValues.push(res.downloads);
return resolve([statKey, statValues]);
});
});
}
Expand All @@ -155,7 +173,7 @@ class WriteNpmStat {
* <br>&nbsp;&nbsp; - "%Y-%m-%d", for example "2022-12-31", which means to be collected until "2022-12-31"
*
* <br>&nbsp;&nbsp; - undefined, which means to be collected until the actual day
* @param {string|null} [endDate=npmstat] - csv file's postfix
* @param {string|null} [endDate=npmstat] - postfix of the csv file
* @returns {Promise} Promise object represents the npm statistics for a package
*/
writeNpmStat(startDate, endDate, postfix = "npmstat") {
Expand Down Expand Up @@ -246,6 +264,7 @@ class WriteNpmStat {
return resolve(csvData);
}
const csvFilePath = this.outDir + "/" + csvFile;
const writePackageName = this.writePackageName;
fs.stat(csvFilePath, function (err) {
if (err != null) {
return resolve(csvData);
Expand All @@ -255,10 +274,14 @@ class WriteNpmStat {
.on("data", (row) => {
if (firstNewLine) {
if (row.date < firstNewLine[0]) {
csvData[csvFile].push([
row.date,
row.downloads,
]);
const statKey = row.date;
const statValues = [];
statValues.push(row.date);
if (writePackageName) {
statValues.push(row.package);
}
statValues.push(row.downloads);
csvData[csvFile].push([statKey, statValues]);
}
}
})
Expand All @@ -270,7 +293,6 @@ class WriteNpmStat {
}

#writeStats(stats) {
console.log(stats);
if (this.outDir) {
fs.mkdir(this.outDir, { recursive: true }, (err) => {
if (err) {
Expand All @@ -280,9 +302,15 @@ class WriteNpmStat {
const csvFilePath = this.outDir + "/" + key;
const csvWriter = createCsvWriter({
path: csvFilePath,
header: ["date", "downloads"],
header: this.writePackageName
? ["date", "package", "downloads"]
: ["date", "downloads"],
});
const postProcessedStats = [];
value.forEach((stat) => {
postProcessedStats.push(stat[1]);
});
csvWriter.writeRecords(value).catch((err) => {
csvWriter.writeRecords(postProcessedStats).catch((err) => {
throw err;
});
}
Expand Down