Skip to content

Commit cc3efa6

Browse files
authored
Merge pull request #31 from veghdev/docs
docs: create docs site
2 parents 6920bb6 + 7575ecd commit cc3efa6

File tree

3 files changed

+125
-13
lines changed

3 files changed

+125
-13
lines changed

.github/workflows/doc.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Publish documentation
2+
3+
on:
4+
workflow_dispatch:
5+
release:
6+
types: [published]
7+
push:
8+
branches: [ main ]
9+
pull_request:
10+
branches: [ main ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v2
19+
20+
- name: Configure git
21+
run: |
22+
git config --global user.mail [email protected]
23+
git config --global user.name docs
24+
25+
- name: Switch to gh-pages branch
26+
run: |
27+
git branch -f gh-pages
28+
git switch gh-pages
29+
30+
- name: Initialize development environment
31+
run: npm install
32+
33+
- name: Build documentation
34+
run: npm run docs
35+
36+
- name: Push to gh-pages branch
37+
run: |
38+
git add docs
39+
if ! git diff-index --quiet HEAD; then
40+
git commit -am 'docs: update'
41+
git push -f origin gh-pages
42+
fi
43+
if: ( github.event_name == 'release' && github.event.action == 'published' ) || github.event_name == 'workflow_dispatch'

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"check-prettier": "prettier -c ./*js ./src/*js",
1212
"check-eslint": "eslint ./*js ./src/*js",
1313
"check": "npm-run-all check-prettier check-eslint",
14+
"docs": "npx jsdoc -d docs --readme README.md ./src/*js",
1415
"rollup": "rollup -c",
1516
"pack": "mkdir -p build && npm pack --pack-destination build && tar -ztvf build/*.tgz"
1617
},
@@ -47,6 +48,7 @@
4748
"rollup": "^2.70.2",
4849
"@rollup/plugin-commonjs": "^22.0.0",
4950
"rollup-plugin-auto-external": "^2.0.0",
50-
"rollup-plugin-terser": "^7.0.2"
51+
"rollup-plugin-terser": "^7.0.2",
52+
"jsdoc": "^3.6.10"
5153
}
5254
}

src/writenpmstat.js

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,37 @@ const createCsvWriter = require("csv-writer").createArrayCsvWriter;
77

88
const StatDate = require("./statdate.js");
99

10+
/**
11+
* Enum for statistics grouping (year, month, day, null)
12+
* @readonly
13+
* @enum {string|null}
14+
*/
1015
const StatPeriod = new Enum(
1116
["year", "month", "day", null],
1217
{ ignoreCase: true },
1318
{ freeze: true }
1419
);
1520

21+
/**
22+
* Class to collect, filter and save npm statistics to csv files
23+
* @property {string|null} [outDir] - path of the directory where
24+
* the gathered data will be saved into csv files
25+
* @property {StatPeriod} [datePeriod=year] - grouping of the statistics
26+
* @property {boolean} [mergeStoredData=true] - flag used to merge actual npm statistics with previously stored
27+
*/
1628
class WriteNpmStat {
1729
#packageName;
1830
outDir;
1931

2032
#datePeriod;
2133
#mergeStoredData;
2234

35+
/**
36+
* Initialize WriteNpmStat class
37+
* @param {string} packageName - name of the target npm package
38+
* @param {string|null} [outDir] - path of the directory where
39+
* the gathered data will be saved into csv files
40+
*/
2341
constructor(packageName, outDir) {
2442
if (!packageName) {
2543
throw new Error("packageName is a required argument");
@@ -52,8 +70,32 @@ class WriteNpmStat {
5270
this.#mergeStoredData = Boolean(mergeStoredData);
5371
}
5472

55-
getNpmStat(startDay, endDay) {
56-
const statDate = new StatDate(startDay, endDay);
73+
/**
74+
* Returns npm statistics for a package
75+
* @param {string|null} [startDate] - start date of the statistics
76+
* should be in one of the following formats:
77+
*
78+
* <br>&nbsp;&nbsp; - "%Y", for example "2022", which means to be collected from "2022-01-01"
79+
*
80+
* <br>&nbsp;&nbsp; - "%Y-%m", for example "2022-01", which means to be collected from "2022-01-01"
81+
*
82+
* <br>&nbsp;&nbsp; - "%Y-%m-%d", for example "2022-01-01", which means to be collected from "2022-01-01"
83+
*
84+
* <br>&nbsp;&nbsp; - undefined, which means to be collected from the last half year
85+
* @param {string|null} [endDate] - end date of the statistics
86+
* should be in one of the following formats:
87+
*
88+
* <br>&nbsp;&nbsp; - "%Y", for example "2022", which means to be collected until "2022-12-31"
89+
*
90+
* <br>&nbsp;&nbsp; - "%Y-%m", for example "2022-12", which means to be collected until "2022-12-31"
91+
*
92+
* <br>&nbsp;&nbsp; - "%Y-%m-%d", for example "2022-12-31", which means to be collected until "2022-12-31"
93+
*
94+
* <br>&nbsp;&nbsp; - undefined, which means to be collected until the actual day
95+
* @returns {Promise} Promise object represents the npm statistics for a package
96+
*/
97+
getNpmStat(startDate, endDate) {
98+
const statDate = new StatDate(startDate, endDate);
5799
const days = WriteNpmStat.getDays(statDate.start, statDate.end);
58100
return new Promise((resolve) => {
59101
const stats = [];
@@ -66,10 +108,10 @@ class WriteNpmStat {
66108
});
67109
}
68110

69-
static getDays(startDay, endDay) {
111+
static getDays(startDate, endDate) {
70112
const arr = [];
71-
const dt = new Date(startDay);
72-
for (dt; dt <= new Date(endDay); dt.setDate(dt.getDate() + 1)) {
113+
const dt = new Date(startDate);
114+
for (dt; dt <= new Date(endDate); dt.setDate(dt.getDate() + 1)) {
73115
arr.push(StatDate.formatDate(new Date(dt)));
74116
}
75117
return arr;
@@ -91,26 +133,51 @@ class WriteNpmStat {
91133
});
92134
}
93135

94-
writeNpmStat(startDay, endDay, postfix = "npmstat") {
136+
/**
137+
* Writes npm statistics for a package
138+
* @param {string|null} [startDate] - start date of the statistics
139+
* should be in one of the following formats:
140+
*
141+
* <br>&nbsp;&nbsp; - "%Y", for example "2022", which means to be collected from "2022-01-01"
142+
*
143+
* <br>&nbsp;&nbsp; - "%Y-%m", for example "2022-01", which means to be collected from "2022-01-01"
144+
*
145+
* <br>&nbsp;&nbsp; - "%Y-%m-%d", for example "2022-01-01", which means to be collected from "2022-01-01"
146+
*
147+
* <br>&nbsp;&nbsp; - undefined, which means to be collected from the last half year
148+
* @param {string|null} [endDate] - end date of the statistics
149+
* should be in one of the following formats:
150+
*
151+
* <br>&nbsp;&nbsp; - "%Y", for example "2022", which means to be collected until "2022-12-31"
152+
*
153+
* <br>&nbsp;&nbsp; - "%Y-%m", for example "2022-12", which means to be collected until "2022-12-31"
154+
*
155+
* <br>&nbsp;&nbsp; - "%Y-%m-%d", for example "2022-12-31", which means to be collected until "2022-12-31"
156+
*
157+
* <br>&nbsp;&nbsp; - undefined, which means to be collected until the actual day
158+
* @param {string|null} [endDate=npmstat] - csv file's postfix
159+
* @returns {Promise} Promise object represents the npm statistics for a package
160+
*/
161+
writeNpmStat(startDate, endDate, postfix = "npmstat") {
95162
return new Promise((resolve) => {
96-
const stats = this.getNpmStat(startDay, endDay);
163+
const stats = this.getNpmStat(startDate, endDate);
97164
stats.then((stats) => {
98165
const grouped = this.#groupStats(
99166
stats,
100-
startDay,
101-
endDay,
167+
startDate,
168+
endDate,
102169
postfix
103170
);
104171
this.#mergeStats(grouped).then((merged) => {
105172
this.#writeStats(merged);
173+
return resolve(merged);
106174
});
107-
return resolve();
108175
});
109176
});
110177
}
111178

112-
#groupStats(stats, startDay, endDay, postfix) {
113-
const statDate = new StatDate(startDay, endDay);
179+
#groupStats(stats, startDate, endDate, postfix) {
180+
const statDate = new StatDate(startDate, endDate);
114181
const days = WriteNpmStat.getDays(statDate.start, statDate.end);
115182
const grouped = {};
116183
if (this.datePeriod) {

0 commit comments

Comments
 (0)