1 | const fs = require('fs-extra');
|
2 | const jsonfile = require('jsonfile');
|
3 | const os = require('os');
|
4 | const path = require('path');
|
5 | const uuid = require('uuid/v4');
|
6 | const { FILE_PATH, CONFIGURATION, METRICS } = require('@src/utils/constants');
|
7 |
|
8 | const askFolderPath = path.join(os.homedir(), FILE_PATH.ASK.HIDDEN_FOLDER);
|
9 | const defaultMetricFilePath = path.join(askFolderPath, FILE_PATH.ASK.METRIC_FILE);
|
10 |
|
11 | class MetricConfig {
|
12 | |
13 |
|
14 |
|
15 |
|
16 | constructor(filePath = defaultMetricFilePath) {
|
17 |
|
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 |
|
27 |
|
28 |
|
29 | get machineId() {
|
30 | return this.data.machineId;
|
31 | }
|
32 |
|
33 | |
34 |
|
35 |
|
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 |
|
44 | module.exports = MetricConfig;
|