-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.utils.ts
More file actions
72 lines (60 loc) · 1.89 KB
/
app.utils.ts
File metadata and controls
72 lines (60 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import * as fs from 'node:fs';
import * as path from 'node:path';
import { PACKAGE_JSON_PATH, USER_HOME_DIRECTORY } from '@/constants';
import { AppUtils as AppUtilsInterface } from '@/interfaces';
export class AppUtils implements AppUtilsInterface {
public readonly projectConfigDirectory = path.resolve(
USER_HOME_DIRECTORY,
`.${this.name}`,
);
public readonly envFilePath = path.resolve(
this.projectConfigDirectory,
'.env',
);
public readonly ignoreFilePath = path.resolve(
this.projectConfigDirectory,
'.commitfyignore',
);
public readonly logger: AppUtilsInterface['logger'] = {
error: (message, ...params) =>
console.error(`${this.name}:`, message, ...params),
warn: (message, ...params) =>
console.warn(`${this.name}:`, message, ...params),
log: (message, ...params) =>
console.log(`${this.name}:`, message, ...params),
message: (message, ...params) => console.log(message, ...params),
};
constructor() {
if (!fs.existsSync(this.projectConfigDirectory)) {
fs.mkdirSync(this.projectConfigDirectory);
}
if (!fs.existsSync(this.ignoreFilePath)) {
fs.writeFileSync(
this.ignoreFilePath,
['package-lock.json', 'yarn.lock', 'node_modules'].join('\n'),
'utf-8',
);
}
}
public get name() {
return this.packageJson.name;
}
public get version() {
return this.packageJson.version;
}
public get ignoreFiles(): string[] {
if (fs.existsSync('.commitfyignore')) {
return fs
.readFileSync('.commitfyignore', 'utf-8')
.split('\n')
.filter((line) => line.trim() !== '');
}
return fs
.readFileSync(this.ignoreFilePath, 'utf-8')
.split('\n')
.filter((line) => line.trim() !== '');
}
private get packageJson(): { version: string; name: string } {
return JSON.parse(fs.readFileSync(PACKAGE_JSON_PATH, 'utf-8'));
}
}