Skip to content

Commit 4d6cc72

Browse files
authored
Merge pull request #50 from veghdev/packagename
npmstat.js: WriteNpmStat add new property, write package name into csv
2 parents 17a4fa7 + ed7275c commit 4d6cc72

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const path = require("path");
66

77
export default [
88
{
9-
input: path.resolve(__dirname, "./src/writenpmstat.js"),
9+
input: path.resolve(__dirname, "./src/npmstat.js"),
1010
output: {
1111
file: path.resolve(__dirname, "./dist/index.min.js"),
1212
format: "cjs",
Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ const StatPeriod = new Enum(
2323
* @property {string|null} [outDir] - path of the directory where
2424
* the gathered data will be saved into csv files
2525
* @property {StatPeriod} [datePeriod=year] - grouping of the statistics
26+
* @property {boolean} [writePackageName=false] - flag used to write the name of the package into a csv column
2627
* @property {boolean} [mergeStoredData=true] - flag used to merge actual npm statistics with previously stored
2728
*/
2829
class WriteNpmStat {
2930
#packageName;
3031
outDir;
3132

3233
#datePeriod;
34+
#writePackageName;
3335
#mergeStoredData;
3436

3537
/**
@@ -47,6 +49,7 @@ class WriteNpmStat {
4749
this.outDir = outDir;
4850

4951
this.#datePeriod = StatPeriod.year;
52+
this.#writePackageName = false;
5053
this.#mergeStoredData = true;
5154
}
5255

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

68+
get writePackageName() {
69+
return this.#writePackageName;
70+
}
71+
72+
set writePackageName(writePackageName) {
73+
this.#writePackageName = Boolean(writePackageName);
74+
}
75+
6576
get mergeStoredData() {
6677
return this.#mergeStoredData;
6778
}
@@ -128,7 +139,14 @@ class WriteNpmStat {
128139
}
129140
throw new Error("retryLimit reached");
130141
}
131-
return resolve([res.start, res.downloads]);
142+
const statKey = res.start;
143+
const statValues = [];
144+
statValues.push(res.start);
145+
if (this.writePackageName) {
146+
statValues.push(this.#packageName);
147+
}
148+
statValues.push(res.downloads);
149+
return resolve([statKey, statValues]);
132150
});
133151
});
134152
}
@@ -155,7 +173,7 @@ class WriteNpmStat {
155173
* <br>&nbsp;&nbsp; - "%Y-%m-%d", for example "2022-12-31", which means to be collected until "2022-12-31"
156174
*
157175
* <br>&nbsp;&nbsp; - undefined, which means to be collected until the actual day
158-
* @param {string|null} [endDate=npmstat] - csv file's postfix
176+
* @param {string|null} [endDate=npmstat] - postfix of the csv file
159177
* @returns {Promise} Promise object represents the npm statistics for a package
160178
*/
161179
writeNpmStat(startDate, endDate, postfix = "npmstat") {
@@ -246,6 +264,7 @@ class WriteNpmStat {
246264
return resolve(csvData);
247265
}
248266
const csvFilePath = this.outDir + "/" + csvFile;
267+
const writePackageName = this.writePackageName;
249268
fs.stat(csvFilePath, function (err) {
250269
if (err != null) {
251270
return resolve(csvData);
@@ -255,10 +274,14 @@ class WriteNpmStat {
255274
.on("data", (row) => {
256275
if (firstNewLine) {
257276
if (row.date < firstNewLine[0]) {
258-
csvData[csvFile].push([
259-
row.date,
260-
row.downloads,
261-
]);
277+
const statKey = row.date;
278+
const statValues = [];
279+
statValues.push(row.date);
280+
if (writePackageName) {
281+
statValues.push(row.package);
282+
}
283+
statValues.push(row.downloads);
284+
csvData[csvFile].push([statKey, statValues]);
262285
}
263286
}
264287
})
@@ -270,7 +293,6 @@ class WriteNpmStat {
270293
}
271294

272295
#writeStats(stats) {
273-
console.log(stats);
274296
if (this.outDir) {
275297
fs.mkdir(this.outDir, { recursive: true }, (err) => {
276298
if (err) {
@@ -280,9 +302,15 @@ class WriteNpmStat {
280302
const csvFilePath = this.outDir + "/" + key;
281303
const csvWriter = createCsvWriter({
282304
path: csvFilePath,
283-
header: ["date", "downloads"],
305+
header: this.writePackageName
306+
? ["date", "package", "downloads"]
307+
: ["date", "downloads"],
308+
});
309+
const postProcessedStats = [];
310+
value.forEach((stat) => {
311+
postProcessedStats.push(stat[1]);
284312
});
285-
csvWriter.writeRecords(value).catch((err) => {
313+
csvWriter.writeRecords(postProcessedStats).catch((err) => {
286314
throw err;
287315
});
288316
}

0 commit comments

Comments
 (0)