-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
130 lines (110 loc) · 3.49 KB
/
mod.ts
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { basename, extname, join } from 'path/mod.ts';
import { BenoSetTypes, BenoTypes } from './src/parsers/types.d.ts';
import { benoMagicReader, benoOneFile } from './src/parsers/reader.ts';
import { Validate } from './src/validator.ts';
// Methods for Parsers
interface BenoCfgFunctions {
config(path?: string, env?: string): void;
get(key: string): unknown | undefined;
content(): Record<string, unknown>[] | undefined;
set(filename: string, object: BenoSetTypes): boolean;
has(filename: string, key: string): boolean;
}
export class Beno implements BenoCfgFunctions {
version = '0.1.0';
constructor(private props: BenoTypes) {
this.props.path = join(Deno.cwd(), 'config');
this.props.envPath = join(Deno.cwd(), '.env');
}
config(path?: string | undefined, env?: string | undefined): void {
/** Make a validation */
// Helpers for the Params
let cfg, envPath;
// Add the
if (path == undefined) {
cfg = join(Deno.cwd(), 'config');
} else {
cfg = path;
}
if (env == undefined) {
envPath = join(Deno.cwd(), '.env');
} else {
envPath = env;
}
this.props.path = cfg!;
this.props.envPath = envPath!;
}
content(): Record<string, unknown>[] | undefined {
const path: string[] = [];
for (const e of Deno.readDirSync(this.props.path!)) {
if (e.isFile && extname(e.name).substring(1) == this.props.encoder) {
path.push(join(this.props.path!, e.name));
}
}
return benoMagicReader(path, this.props.encoder);
}
get(key: string): Validate {
const content = this.content();
if (content == undefined) {
throw new Error(
'Beno ERROR: Not defined the content not found a valid file in the directory or not found the directory',
);
}
const target = content.find((i) => key in i);
return new Validate((target ?? {})[key]);
}
set(filename: string, object: BenoSetTypes): boolean {
const content = this.content();
if (content == undefined) {
throw new Error(
'Beno ERROR: Not defined the content not found a valid file in the directory or not found the directory',
);
}
content.map((e) => {
if (extname(filename) != '') {
filename = filename.replace(`.${this.props.encoder}`, '');
}
if (typeof e.BENO_INTERNALS_FILEPATH == 'string') {
if (
filename ==
basename(e.BENO_INTERNALS_FILEPATH).replace(
`.${this.props.encoder}`,
'',
)
) {
const obj = benoOneFile(
e.BENO_INTERNALS_FILEPATH,
this.props.encoder,
);
obj[object.key] = object.val;
try {
Deno.writeTextFileSync(
e.BENO_INTERNALS_FILEPATH,
JSON.stringify(obj, null, 2),
);
} catch (e) {
throw new Error(
`Beno ERROR: Is not possible write the file error.\n${e}`,
);
}
return true;
}
} else {
throw new Error(
`Beno ERROR: Not valid internal path please report this on github maybe a Bug`,
);
}
});
return false;
}
has(key: string): boolean {
const content = this.content();
if (content == undefined) {
throw new Error(
'Beno ERROR: Not defined the content not found a valid file in the directory or not found the directory',
);
}
if (content.find((i) => key in i) != undefined) return true;
return false;
}
}