UNPKG

1.39 kBJavaScriptView Raw
1const fs = require('fs-extra');
2const jsonfile = require('jsonfile');
3const os = require('os');
4const path = require('path');
5const uuid = require('uuid/v4');
6const { FILE_PATH, CONFIGURATION, METRICS } = require('@src/utils/constants');
7
8const askFolderPath = path.join(os.homedir(), FILE_PATH.ASK.HIDDEN_FOLDER);
9const defaultMetricFilePath = path.join(askFolderPath, FILE_PATH.ASK.METRIC_FILE);
10
11class MetricConfig {
12 /**
13 * Constructor for MetricConfig class
14 * @param {string} filePath
15 */
16 constructor(filePath = defaultMetricFilePath) {
17 // making file path if not exists
18 if (!fs.existsSync(filePath)) {
19 fs.ensureDirSync(askFolderPath);
20 jsonfile.writeFileSync(filePath, { machineId: uuid(), createdAt: new Date() }, { spaces: CONFIGURATION.JSON_DISPLAY_INDENT });
21 }
22 this.data = JSON.parse(fs.readFileSync(filePath));
23 }
24
25 /**
26 * Gets machineId property
27 * @returns {string}
28 */
29 get machineId() {
30 return this.data.machineId;
31 }
32
33 /**
34 * Returns boolean indicating if user is new
35 * @returns {boolean}
36 */
37 isNewUser() {
38 const { createdAt } = this.data;
39 const daysDiff = (new Date().getTime() - new Date(createdAt).getTime()) / (1000 * 3600 * 24);
40 return daysDiff <= METRICS.NEW_USER_LENGTH_DAYS;
41 }
42}
43
44module.exports = MetricConfig;