astro-accelerator-utils 0.2.33 → 0.2.35

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.
@@ -11,7 +11,7 @@
11
11
  export function sortByPubDate (a, b) {
12
12
  const dateA = a.frontmatter.pubDate || '1970-01-01';
13
13
  const dateB = b.frontmatter.pubDate || '1970-01-01';
14
- return dateB.localeCompare(dateA);
14
+ return dateA.localeCompare(dateB);
15
15
  }
16
16
 
17
17
  /**
@@ -23,7 +23,7 @@ export function sortByPubDate (a, b) {
23
23
  export function sortByPubDateDesc (a, b) {
24
24
  const dateA = a.frontmatter.pubDate || '1970-01-01';
25
25
  const dateB = b.frontmatter.pubDate || '1970-01-01';
26
- return dateA.localeCompare(dateB);
26
+ return dateB.localeCompare(dateA);
27
27
  }
28
28
 
29
29
  /**
@@ -7,6 +7,7 @@ import { Paging } from './paging.mjs';
7
7
  import { Posts } from './posts.mjs';
8
8
  import { Taxonomy } from './taxonomy.mjs';
9
9
  import { UrlFormatter } from './urls.mjs';
10
+ import { Statistics } from './statistics.mjs';
10
11
 
11
12
  /**
12
13
  * @typedef { import("../../types/Site").Site } Site
@@ -22,6 +23,7 @@ export class Accelerator {
22
23
  this.siteUrl = site.url;
23
24
  this.subfolder = site.subfolder;
24
25
  this.useTrailingUrlSlash = site.useTrailingUrlSlash;
26
+ this.captureStatistics = site.captureStatistics;
25
27
  }
26
28
 
27
29
  get authors() {
@@ -57,6 +59,10 @@ export class Accelerator {
57
59
  }
58
60
 
59
61
  get urlFormatter() {
60
- return new UrlFormatter(this.siteUrl, this.subfolder, this.useTrailingUrlSlash)
62
+ return new UrlFormatter(this.siteUrl, this.subfolder, this.useTrailingUrlSlash);
63
+ }
64
+
65
+ get statistics() {
66
+ return Statistics;
61
67
  }
62
68
  }
package/lib/v1/cache.mjs CHANGED
@@ -78,23 +78,23 @@ export class Cache {
78
78
  }
79
79
  }
80
80
 
81
- /**
81
+ /**
82
82
  * Get's the path of the cache files
83
83
  * @returns {string}
84
84
  */
85
- getCachePath () {
86
- const cachePath = path.join(process.cwd(), '.cache/');
87
- fs.mkdirSync(cachePath, { recursive: true })
88
- return cachePath;
89
- }
90
-
91
- /**
92
- * Gets the file path for a cache item
93
- * @param {string} key
94
- * @returns {string}
95
- */
96
- getItemPath (key) {
97
- const cachePath = this.getCachePath();
98
- return path.join(cachePath, key + '.json');
99
- }
85
+ getCachePath () {
86
+ const cachePath = path.join(process.cwd(), '.cache/');
87
+ fs.mkdirSync(cachePath, { recursive: true })
88
+ return cachePath;
89
+ }
90
+
91
+ /**
92
+ * Gets the file path for a cache item
93
+ * @param {string} key
94
+ * @returns {string}
95
+ */
96
+ getItemPath (key) {
97
+ const cachePath = this.getCachePath();
98
+ return path.join(cachePath, key + '.json');
99
+ }
100
100
  }
@@ -0,0 +1,74 @@
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
+ fs.appendFileSync(path, `"${this.operation}",${console.timeStamp},${this.duration}${EOL}`);
54
+ }
55
+
56
+ /**
57
+ * Get's the path of the log files
58
+ * @returns {string}
59
+ */
60
+ static getLogPath () {
61
+ const logPath = path.join(process.cwd(), '.log/');
62
+ fs.mkdirSync(logPath, { recursive: true })
63
+ return logPath;
64
+ }
65
+
66
+ /**
67
+ * Gets the file path for a log item
68
+ * @returns {string}
69
+ */
70
+ static getItemPath () {
71
+ const cachePath = this.getLogPath();
72
+ return path.join(cachePath, 'statistics.csv');
73
+ }
74
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-accelerator-utils",
3
- "version": "0.2.33",
3
+ "version": "0.2.35",
4
4
  "description": "Astro utilities for Astro Accelerator.",
5
5
  "main": "index.mjs",
6
6
  "type": "module",