astro-accelerator-utils 0.3.7 → 0.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/README.md +19 -19
- package/index.d.mts +4 -4
- package/index.mjs +9 -9
- package/lib/postFiltering.d.mts +48 -48
- package/lib/postFiltering.mjs +116 -116
- package/lib/postOrdering.d.mts +32 -32
- package/lib/postOrdering.mjs +51 -51
- package/lib/v1/accelerator.d.mts +36 -36
- package/lib/v1/accelerator.mjs +70 -70
- package/lib/v1/authors.d.mts +28 -28
- package/lib/v1/authors.mjs +71 -71
- package/lib/v1/cache.d.mts +43 -43
- package/lib/v1/cache.mjs +99 -99
- package/lib/v1/dates.d.mts +19 -19
- package/lib/v1/dates.mjs +26 -26
- package/lib/v1/markdown.d.mts +20 -20
- package/lib/v1/markdown.mjs +45 -45
- package/lib/v1/navigation.d.mts +111 -111
- package/lib/v1/navigation.mjs +446 -427
- package/lib/v1/paging.d.mts +15 -15
- package/lib/v1/paging.mjs +77 -77
- package/lib/v1/posts.d.mts +26 -26
- package/lib/v1/posts.mjs +47 -47
- package/lib/v1/statistics-stub.d.mts +5 -5
- package/lib/v1/statistics-stub.mjs +9 -9
- package/lib/v1/statistics.d.mts +39 -39
- package/lib/v1/statistics.mjs +78 -78
- package/lib/v1/taxonomy.d.mts +62 -62
- package/lib/v1/taxonomy.mjs +144 -144
- package/lib/v1/urls.d.mts +40 -40
- package/lib/v1/urls.mjs +104 -104
- package/package.json +3 -3
- package/types/Astro.d.ts +19 -19
- package/types/AuthorList.d.ts +8 -8
- package/types/BannerImage.d.ts +4 -4
- package/types/Frontmatter.d.ts +30 -30
- package/types/Link.d.ts +6 -6
- package/types/NavPage.d.ts +22 -22
- package/types/PagePredicate.d.ts +2 -2
- package/types/Site.d.ts +43 -43
- package/types/Taxonomy.d.ts +15 -15
package/lib/v1/paging.d.mts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef { import("../../types/Link").Link } Link
|
|
3
|
-
*/
|
|
4
|
-
export class Paging {
|
|
5
|
-
/**
|
|
6
|
-
* Provides a list of paging links, 1 ... 3 4 5 ... 7
|
|
7
|
-
* @param {number} limit
|
|
8
|
-
* @param {number} numberOfPages
|
|
9
|
-
* @param {number} currentPage
|
|
10
|
-
* @param {string} url
|
|
11
|
-
* @returns {Link[]}
|
|
12
|
-
*/
|
|
13
|
-
links(limit: number, numberOfPages: number, currentPage: number, url: string): Link[];
|
|
14
|
-
}
|
|
15
|
-
export type Link = import("../../types/Link").Link;
|
|
1
|
+
/**
|
|
2
|
+
* @typedef { import("../../types/Link").Link } Link
|
|
3
|
+
*/
|
|
4
|
+
export class Paging {
|
|
5
|
+
/**
|
|
6
|
+
* Provides a list of paging links, 1 ... 3 4 5 ... 7
|
|
7
|
+
* @param {number} limit
|
|
8
|
+
* @param {number} numberOfPages
|
|
9
|
+
* @param {number} currentPage
|
|
10
|
+
* @param {string} url
|
|
11
|
+
* @returns {Link[]}
|
|
12
|
+
*/
|
|
13
|
+
links(limit: number, numberOfPages: number, currentPage: number, url: string): Link[];
|
|
14
|
+
}
|
|
15
|
+
export type Link = import("../../types/Link").Link;
|
package/lib/v1/paging.mjs
CHANGED
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef { import("../../types/Link").Link } Link
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export class Paging {
|
|
6
|
-
/**
|
|
7
|
-
* Provides a list of paging links, 1 ... 3 4 5 ... 7
|
|
8
|
-
* @param {number} limit
|
|
9
|
-
* @param {number} numberOfPages
|
|
10
|
-
* @param {number} currentPage
|
|
11
|
-
* @param {string} url
|
|
12
|
-
* @returns {Link[]}
|
|
13
|
-
*/
|
|
14
|
-
links (limit, numberOfPages, currentPage, url) {
|
|
15
|
-
/** @type {Link[]} */
|
|
16
|
-
const pageLinks = [];
|
|
17
|
-
|
|
18
|
-
let start = 0;
|
|
19
|
-
let end = numberOfPages;
|
|
20
|
-
|
|
21
|
-
if (numberOfPages > limit) {
|
|
22
|
-
start = (currentPage - (limit - 1) / 2) - 1;
|
|
23
|
-
if (start < 0) {
|
|
24
|
-
start = 0;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
end = (start + limit);
|
|
28
|
-
if (end > numberOfPages) {
|
|
29
|
-
end = numberOfPages;
|
|
30
|
-
start = numberOfPages - limit;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (start == 1) {
|
|
35
|
-
pageLinks.push({
|
|
36
|
-
title: '1',
|
|
37
|
-
url: url.replace('/' + currentPage, '/' + 1),
|
|
38
|
-
ariaCurrent: false,
|
|
39
|
-
class: ''
|
|
40
|
-
});
|
|
41
|
-
} else if (start > 1) {
|
|
42
|
-
pageLinks.push({
|
|
43
|
-
title: '1',
|
|
44
|
-
url: url.replace('/' + currentPage, '/' + 1),
|
|
45
|
-
ariaCurrent: false,
|
|
46
|
-
class: 'paging-collapse-after'
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
for (let i = start; i < end; i++) {
|
|
51
|
-
const userPage = i + 1;
|
|
52
|
-
pageLinks.push({
|
|
53
|
-
title: userPage.toString(),
|
|
54
|
-
url: url.replace('/' + currentPage, '/' + userPage),
|
|
55
|
-
ariaCurrent: userPage == currentPage ? 'page' : false,
|
|
56
|
-
class: ''
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (end < (numberOfPages - 1)) {
|
|
61
|
-
pageLinks.push({
|
|
62
|
-
title: numberOfPages.toString(),
|
|
63
|
-
url: url.replace('/' + currentPage, '/' + numberOfPages),
|
|
64
|
-
ariaCurrent: false,
|
|
65
|
-
class: 'paging-collapse-before'
|
|
66
|
-
});
|
|
67
|
-
} else if (end < numberOfPages) {
|
|
68
|
-
pageLinks.push({
|
|
69
|
-
title: numberOfPages.toString(),
|
|
70
|
-
url: url.replace('/' + currentPage, '/' + numberOfPages),
|
|
71
|
-
ariaCurrent: false,
|
|
72
|
-
class: ''
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return pageLinks;
|
|
77
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @typedef { import("../../types/Link").Link } Link
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export class Paging {
|
|
6
|
+
/**
|
|
7
|
+
* Provides a list of paging links, 1 ... 3 4 5 ... 7
|
|
8
|
+
* @param {number} limit
|
|
9
|
+
* @param {number} numberOfPages
|
|
10
|
+
* @param {number} currentPage
|
|
11
|
+
* @param {string} url
|
|
12
|
+
* @returns {Link[]}
|
|
13
|
+
*/
|
|
14
|
+
links (limit, numberOfPages, currentPage, url) {
|
|
15
|
+
/** @type {Link[]} */
|
|
16
|
+
const pageLinks = [];
|
|
17
|
+
|
|
18
|
+
let start = 0;
|
|
19
|
+
let end = numberOfPages;
|
|
20
|
+
|
|
21
|
+
if (numberOfPages > limit) {
|
|
22
|
+
start = (currentPage - (limit - 1) / 2) - 1;
|
|
23
|
+
if (start < 0) {
|
|
24
|
+
start = 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
end = (start + limit);
|
|
28
|
+
if (end > numberOfPages) {
|
|
29
|
+
end = numberOfPages;
|
|
30
|
+
start = numberOfPages - limit;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (start == 1) {
|
|
35
|
+
pageLinks.push({
|
|
36
|
+
title: '1',
|
|
37
|
+
url: url.replace('/' + currentPage, '/' + 1),
|
|
38
|
+
ariaCurrent: false,
|
|
39
|
+
class: ''
|
|
40
|
+
});
|
|
41
|
+
} else if (start > 1) {
|
|
42
|
+
pageLinks.push({
|
|
43
|
+
title: '1',
|
|
44
|
+
url: url.replace('/' + currentPage, '/' + 1),
|
|
45
|
+
ariaCurrent: false,
|
|
46
|
+
class: 'paging-collapse-after'
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
for (let i = start; i < end; i++) {
|
|
51
|
+
const userPage = i + 1;
|
|
52
|
+
pageLinks.push({
|
|
53
|
+
title: userPage.toString(),
|
|
54
|
+
url: url.replace('/' + currentPage, '/' + userPage),
|
|
55
|
+
ariaCurrent: userPage == currentPage ? 'page' : false,
|
|
56
|
+
class: ''
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (end < (numberOfPages - 1)) {
|
|
61
|
+
pageLinks.push({
|
|
62
|
+
title: numberOfPages.toString(),
|
|
63
|
+
url: url.replace('/' + currentPage, '/' + numberOfPages),
|
|
64
|
+
ariaCurrent: false,
|
|
65
|
+
class: 'paging-collapse-before'
|
|
66
|
+
});
|
|
67
|
+
} else if (end < numberOfPages) {
|
|
68
|
+
pageLinks.push({
|
|
69
|
+
title: numberOfPages.toString(),
|
|
70
|
+
url: url.replace('/' + currentPage, '/' + numberOfPages),
|
|
71
|
+
ariaCurrent: false,
|
|
72
|
+
class: ''
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return pageLinks;
|
|
77
|
+
}
|
|
78
78
|
}
|
package/lib/v1/posts.d.mts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef { import("../../types/Astro").MarkdownInstance } MarkdownInstance
|
|
3
|
-
* @typedef { import("./cache.mjs").Cache } Cache
|
|
4
|
-
*/
|
|
5
|
-
export class Posts {
|
|
6
|
-
/**
|
|
7
|
-
* Constructor
|
|
8
|
-
* @param {Cache} cache
|
|
9
|
-
* @param {() => MarkdownInstance[]} [fetchAll]
|
|
10
|
-
*/
|
|
11
|
-
constructor(cache: Cache, fetchAll?: () => MarkdownInstance[]);
|
|
12
|
-
cache: import("./cache.mjs").Cache;
|
|
13
|
-
fetchAll: () => any;
|
|
14
|
-
/**
|
|
15
|
-
* Gets all markdown posts in the site
|
|
16
|
-
* @returns {MarkdownInstance[]}
|
|
17
|
-
*/
|
|
18
|
-
all(): MarkdownInstance[];
|
|
19
|
-
/**
|
|
20
|
-
* Gets top-level markdown posts in the site
|
|
21
|
-
* @returns {MarkdownInstance[]}
|
|
22
|
-
*/
|
|
23
|
-
root(subfolder: any): MarkdownInstance[];
|
|
24
|
-
}
|
|
25
|
-
export type MarkdownInstance = import("../../types/Astro").MarkdownInstance;
|
|
26
|
-
export type Cache = import("./cache.mjs").Cache;
|
|
1
|
+
/**
|
|
2
|
+
* @typedef { import("../../types/Astro").MarkdownInstance } MarkdownInstance
|
|
3
|
+
* @typedef { import("./cache.mjs").Cache } Cache
|
|
4
|
+
*/
|
|
5
|
+
export class Posts {
|
|
6
|
+
/**
|
|
7
|
+
* Constructor
|
|
8
|
+
* @param {Cache} cache
|
|
9
|
+
* @param {() => MarkdownInstance[]} [fetchAll]
|
|
10
|
+
*/
|
|
11
|
+
constructor(cache: Cache, fetchAll?: () => MarkdownInstance[]);
|
|
12
|
+
cache: import("./cache.mjs").Cache;
|
|
13
|
+
fetchAll: () => any;
|
|
14
|
+
/**
|
|
15
|
+
* Gets all markdown posts in the site
|
|
16
|
+
* @returns {MarkdownInstance[]}
|
|
17
|
+
*/
|
|
18
|
+
all(): MarkdownInstance[];
|
|
19
|
+
/**
|
|
20
|
+
* Gets top-level markdown posts in the site
|
|
21
|
+
* @returns {MarkdownInstance[]}
|
|
22
|
+
*/
|
|
23
|
+
root(subfolder: any): MarkdownInstance[];
|
|
24
|
+
}
|
|
25
|
+
export type MarkdownInstance = import("../../types/Astro").MarkdownInstance;
|
|
26
|
+
export type Cache = import("./cache.mjs").Cache;
|
package/lib/v1/posts.mjs
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef { import("../../types/Astro").MarkdownInstance } MarkdownInstance
|
|
3
|
-
* @typedef { import("./cache.mjs").Cache } Cache
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export class Posts {
|
|
7
|
-
/**
|
|
8
|
-
* Constructor
|
|
9
|
-
* @param {Cache} cache
|
|
10
|
-
* @param {() => MarkdownInstance[]} [fetchAll]
|
|
11
|
-
*/
|
|
12
|
-
constructor(cache, fetchAll) {
|
|
13
|
-
this.cache = cache;
|
|
14
|
-
/* istanbul ignore next */
|
|
15
|
-
this.fetchAll = fetchAll ?? function() {
|
|
16
|
-
return import.meta.glob(['/src/pages/**/*.md', '/src/pages/**/*.mdx'], { eager: true });
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Gets all markdown posts in the site
|
|
22
|
-
* @returns {MarkdownInstance[]}
|
|
23
|
-
*/
|
|
24
|
-
all () {
|
|
25
|
-
const key = 'v1_posts.all';
|
|
26
|
-
|
|
27
|
-
return this.cache.get(key, () => {
|
|
28
|
-
const pageImportResult = this.fetchAll();
|
|
29
|
-
return Object.values(pageImportResult);
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Gets top-level markdown posts in the site
|
|
35
|
-
* @returns {MarkdownInstance[]}
|
|
36
|
-
*/
|
|
37
|
-
root (subfolder) {
|
|
38
|
-
const isRoot = subfolder.length == 0;
|
|
39
|
-
const expectedDepth = isRoot ? 1 : 2;
|
|
40
|
-
|
|
41
|
-
return this.all().filter(p => {
|
|
42
|
-
const url = p.url ?? '/';
|
|
43
|
-
const depth = (url).split('/').length - 1;
|
|
44
|
-
return depth == expectedDepth
|
|
45
|
-
|| (depth == (expectedDepth - 1) && p.file.includes(subfolder.toLowerCase() + '.md'));
|
|
46
|
-
})
|
|
47
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @typedef { import("../../types/Astro").MarkdownInstance } MarkdownInstance
|
|
3
|
+
* @typedef { import("./cache.mjs").Cache } Cache
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export class Posts {
|
|
7
|
+
/**
|
|
8
|
+
* Constructor
|
|
9
|
+
* @param {Cache} cache
|
|
10
|
+
* @param {() => MarkdownInstance[]} [fetchAll]
|
|
11
|
+
*/
|
|
12
|
+
constructor(cache, fetchAll) {
|
|
13
|
+
this.cache = cache;
|
|
14
|
+
/* istanbul ignore next */
|
|
15
|
+
this.fetchAll = fetchAll ?? function() {
|
|
16
|
+
return import.meta.glob(['/src/pages/**/*.md', '/src/pages/**/*.mdx'], { eager: true });
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Gets all markdown posts in the site
|
|
22
|
+
* @returns {MarkdownInstance[]}
|
|
23
|
+
*/
|
|
24
|
+
all () {
|
|
25
|
+
const key = 'v1_posts.all';
|
|
26
|
+
|
|
27
|
+
return this.cache.get(key, () => {
|
|
28
|
+
const pageImportResult = this.fetchAll();
|
|
29
|
+
return Object.values(pageImportResult);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Gets top-level markdown posts in the site
|
|
35
|
+
* @returns {MarkdownInstance[]}
|
|
36
|
+
*/
|
|
37
|
+
root (subfolder) {
|
|
38
|
+
const isRoot = subfolder.length == 0;
|
|
39
|
+
const expectedDepth = isRoot ? 1 : 2;
|
|
40
|
+
|
|
41
|
+
return this.all().filter(p => {
|
|
42
|
+
const url = p.url ?? '/';
|
|
43
|
+
const depth = (url).split('/').length - 1;
|
|
44
|
+
return depth == expectedDepth
|
|
45
|
+
|| (depth == (expectedDepth - 1) && p.file.includes(subfolder.toLowerCase() + '.md'));
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
48
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export class StatisticsStub {
|
|
2
|
-
constructor(operation: any);
|
|
3
|
-
start(): void;
|
|
4
|
-
stop(): void;
|
|
5
|
-
}
|
|
1
|
+
export class StatisticsStub {
|
|
2
|
+
constructor(operation: any);
|
|
3
|
+
start(): void;
|
|
4
|
+
stop(): void;
|
|
5
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export class StatisticsStub {
|
|
2
|
-
constructor(operation) {
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
start() {
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
stop() {
|
|
9
|
-
}
|
|
1
|
+
export class StatisticsStub {
|
|
2
|
+
constructor(operation) {
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
start() {
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
stop() {
|
|
9
|
+
}
|
|
10
10
|
}
|
package/lib/v1/statistics.d.mts
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
export class Statistics {
|
|
2
|
-
/**
|
|
3
|
-
* Clears the stats file
|
|
4
|
-
*/
|
|
5
|
-
static purge(): void;
|
|
6
|
-
/**
|
|
7
|
-
* Get's the path of the log files
|
|
8
|
-
* @returns {string}
|
|
9
|
-
*/
|
|
10
|
-
static getLogPath(): string;
|
|
11
|
-
/**
|
|
12
|
-
* Gets the file path for a log item
|
|
13
|
-
* @returns {string}
|
|
14
|
-
*/
|
|
15
|
-
static getItemPath(): string;
|
|
16
|
-
/**
|
|
17
|
-
* Creates a new stats instance - all instances append to the stats file
|
|
18
|
-
* Use Statistics.purge() to reset the file
|
|
19
|
-
* After creating a timer, use start() and stop() to record an event
|
|
20
|
-
* @param {string} operation
|
|
21
|
-
*/
|
|
22
|
-
constructor(operation: string);
|
|
23
|
-
operation: string;
|
|
24
|
-
/**
|
|
25
|
-
* Gets high resolution time in ms
|
|
26
|
-
* @returns number
|
|
27
|
-
*/
|
|
28
|
-
getMilliseconds(): number;
|
|
29
|
-
/**
|
|
30
|
-
* Start the timer
|
|
31
|
-
*/
|
|
32
|
-
start(): void;
|
|
33
|
-
/**
|
|
34
|
-
* Stop the timer (logs to .logs/statistics.csv)
|
|
35
|
-
*/
|
|
36
|
-
stop(): void;
|
|
37
|
-
end: number;
|
|
38
|
-
duration: number;
|
|
39
|
-
}
|
|
1
|
+
export class Statistics {
|
|
2
|
+
/**
|
|
3
|
+
* Clears the stats file
|
|
4
|
+
*/
|
|
5
|
+
static purge(): void;
|
|
6
|
+
/**
|
|
7
|
+
* Get's the path of the log files
|
|
8
|
+
* @returns {string}
|
|
9
|
+
*/
|
|
10
|
+
static getLogPath(): string;
|
|
11
|
+
/**
|
|
12
|
+
* Gets the file path for a log item
|
|
13
|
+
* @returns {string}
|
|
14
|
+
*/
|
|
15
|
+
static getItemPath(): string;
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new stats instance - all instances append to the stats file
|
|
18
|
+
* Use Statistics.purge() to reset the file
|
|
19
|
+
* After creating a timer, use start() and stop() to record an event
|
|
20
|
+
* @param {string} operation
|
|
21
|
+
*/
|
|
22
|
+
constructor(operation: string);
|
|
23
|
+
operation: string;
|
|
24
|
+
/**
|
|
25
|
+
* Gets high resolution time in ms
|
|
26
|
+
* @returns number
|
|
27
|
+
*/
|
|
28
|
+
getMilliseconds(): number;
|
|
29
|
+
/**
|
|
30
|
+
* Start the timer
|
|
31
|
+
*/
|
|
32
|
+
start(): void;
|
|
33
|
+
/**
|
|
34
|
+
* Stop the timer (logs to .logs/statistics.csv)
|
|
35
|
+
*/
|
|
36
|
+
stop(): void;
|
|
37
|
+
end: number;
|
|
38
|
+
duration: number;
|
|
39
|
+
}
|
package/lib/v1/statistics.mjs
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import process from 'process';
|
|
4
|
-
import { EOL } from 'os';
|
|
5
|
-
|
|
6
|
-
export class Statistics {
|
|
7
|
-
/**
|
|
8
|
-
* Creates a new stats instance - all instances append to the stats file
|
|
9
|
-
* Use Statistics.purge() to reset the file
|
|
10
|
-
* After creating a timer, use start() and stop() to record an event
|
|
11
|
-
* @param {string} operation
|
|
12
|
-
*/
|
|
13
|
-
constructor(operation) {
|
|
14
|
-
this.operation = operation.replace(/"/g, '');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Clears the stats file
|
|
19
|
-
*/
|
|
20
|
-
static purge() {
|
|
21
|
-
const path = this.getItemPath();
|
|
22
|
-
if (fs.existsSync(path)) {
|
|
23
|
-
fs.unlinkSync(path);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
fs.writeFileSync(path, `Operation,Date,Duration${EOL}`);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Gets high resolution time in ms
|
|
31
|
-
* @returns number
|
|
32
|
-
*/
|
|
33
|
-
getMilliseconds() {
|
|
34
|
-
const hrtime = process.hrtime();
|
|
35
|
-
return (hrtime[0] * 1000) + (hrtime[1] / 1000000);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Start the timer
|
|
40
|
-
*/
|
|
41
|
-
start() {
|
|
42
|
-
this.start = this.getMilliseconds();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Stop the timer (logs to .logs/statistics.csv)
|
|
47
|
-
*/
|
|
48
|
-
stop() {
|
|
49
|
-
this.end = this.getMilliseconds();
|
|
50
|
-
this.duration = this.end - this.start;
|
|
51
|
-
|
|
52
|
-
const path = Statistics.getItemPath();
|
|
53
|
-
|
|
54
|
-
if (!fs.existsSync(path)) {
|
|
55
|
-
Statistics.purge();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
fs.appendFileSync(path, `"${this.operation}",${new Date().toJSON()},${this.duration}${EOL}`);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Get's the path of the log files
|
|
63
|
-
* @returns {string}
|
|
64
|
-
*/
|
|
65
|
-
static getLogPath () {
|
|
66
|
-
const logPath = path.join(process.cwd(), '.log/');
|
|
67
|
-
fs.mkdirSync(logPath, { recursive: true })
|
|
68
|
-
return logPath;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Gets the file path for a log item
|
|
73
|
-
* @returns {string}
|
|
74
|
-
*/
|
|
75
|
-
static getItemPath () {
|
|
76
|
-
const cachePath = this.getLogPath();
|
|
77
|
-
return path.join(cachePath, 'statistics.csv');
|
|
78
|
-
}
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import process from 'process';
|
|
4
|
+
import { EOL } from 'os';
|
|
5
|
+
|
|
6
|
+
export class Statistics {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new stats instance - all instances append to the stats file
|
|
9
|
+
* Use Statistics.purge() to reset the file
|
|
10
|
+
* After creating a timer, use start() and stop() to record an event
|
|
11
|
+
* @param {string} operation
|
|
12
|
+
*/
|
|
13
|
+
constructor(operation) {
|
|
14
|
+
this.operation = operation.replace(/"/g, '');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Clears the stats file
|
|
19
|
+
*/
|
|
20
|
+
static purge() {
|
|
21
|
+
const path = this.getItemPath();
|
|
22
|
+
if (fs.existsSync(path)) {
|
|
23
|
+
fs.unlinkSync(path);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
fs.writeFileSync(path, `Operation,Date,Duration${EOL}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Gets high resolution time in ms
|
|
31
|
+
* @returns number
|
|
32
|
+
*/
|
|
33
|
+
getMilliseconds() {
|
|
34
|
+
const hrtime = process.hrtime();
|
|
35
|
+
return (hrtime[0] * 1000) + (hrtime[1] / 1000000);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Start the timer
|
|
40
|
+
*/
|
|
41
|
+
start() {
|
|
42
|
+
this.start = this.getMilliseconds();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Stop the timer (logs to .logs/statistics.csv)
|
|
47
|
+
*/
|
|
48
|
+
stop() {
|
|
49
|
+
this.end = this.getMilliseconds();
|
|
50
|
+
this.duration = this.end - this.start;
|
|
51
|
+
|
|
52
|
+
const path = Statistics.getItemPath();
|
|
53
|
+
|
|
54
|
+
if (!fs.existsSync(path)) {
|
|
55
|
+
Statistics.purge();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
fs.appendFileSync(path, `"${this.operation}",${new Date().toJSON()},${this.duration}${EOL}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Get's the path of the log files
|
|
63
|
+
* @returns {string}
|
|
64
|
+
*/
|
|
65
|
+
static getLogPath () {
|
|
66
|
+
const logPath = path.join(process.cwd(), '.log/');
|
|
67
|
+
fs.mkdirSync(logPath, { recursive: true })
|
|
68
|
+
return logPath;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Gets the file path for a log item
|
|
73
|
+
* @returns {string}
|
|
74
|
+
*/
|
|
75
|
+
static getItemPath () {
|
|
76
|
+
const cachePath = this.getLogPath();
|
|
77
|
+
return path.join(cachePath, 'statistics.csv');
|
|
78
|
+
}
|
|
79
79
|
}
|