Skip to content

Commit

Permalink
feat: improve the set function
Browse files Browse the repository at this point in the history
  • Loading branch information
TeoDev1611 committed Aug 20, 2022
1 parent 27139b6 commit c5a8762
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 24 deletions.
11 changes: 6 additions & 5 deletions examples.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Beno } from './mod.ts';

const beno = new Beno({ encoder: 'json' });
console.log(beno.get('example').string());
/* console.log(beno.get('example').string());
console.log(beno.get('to').number());
console.log(beno.get('yes').boolean());
beno.set('xd', 'xd');
console.log(beno.get('yes').boolean()); */
beno.set('other', { key: 'helloo', val: 'benooooooooo' });
beno.set('hello.json', { key: 'bye', val: 'chauuuuuuuu' });

const JSONC = new Beno({encoder: 'jsonc'})
/* const JSONC = new Beno({encoder: 'jsonc'})
console.log(JSONC.content())
console.log(JSONC.get('hello').string())
console.log(JSONC.get('hello').string()) */
44 changes: 39 additions & 5 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { parse as parseJSONC } from 'encoding/jsonc.ts';
import { basename, extname, join } from 'path/mod.ts';
import { BenoTypes } from './src/parsers/types.d.ts';
import { benoMagicReader } from './src/parsers/reader.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, value: unknown): void;
set(filename: string, object: BenoSetTypes): void;
}

export class Beno implements BenoCfgFunctions {
Expand Down Expand Up @@ -63,16 +63,50 @@ export class Beno implements BenoCfgFunctions {
return new Validate((target ?? {})[key]);
}

set(filename: string, value: unknown): void {
set(filename: string, object: BenoSetTypes): void {
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) => {
console.log(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}`,
);
}
}
} else {
throw new Error(
`Beno ERROR: Not valid internal path please report this on github maybe a Bug`,
);
}
});
}
}
36 changes: 22 additions & 14 deletions src/parsers/reader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import * as JSONC from 'encoding/jsonc.ts';

export function benoOneFile(
file: string,
encoder: 'json' | 'jsonc',
) {
try {
if (encoder == 'json') {
return JSON.parse(Deno.readTextFileSync(file));
} else {
return JSONC.parse(Deno.readTextFileSync(file)) as Record<
string,
unknown
>;
}
} catch (e) {
if (e instanceof Deno.errors.NotFound) return;
throw new Error(`Beno ERROR: ${e}`);
}
}

// @desc Read a file if exists and if return none not exists
export function benoMagicReader(
files: string[],
Expand All @@ -8,20 +27,9 @@ export function benoMagicReader(
try {
const content: Record<string, unknown>[] = [];
for (const i of files) {
if (encoder == 'json') {
const objCont = JSON.parse(Deno.readTextFileSync(i));
// Append a key for a path!
objCont['BENO_INTERNALS_FILEPATH'] = i;
content.push(objCont);
} else {
const objCont = JSONC.parse(Deno.readTextFileSync(i)) as Record<
string,
unknown
>;
// Append a key for a path!
objCont['BENO_INTERNALS_FILEPATH'] = i;
content.push(objCont);
}
const obj = benoOneFile(i, encoder);
obj['BENO_INTERNALS_FILEPATH'] = i;
content.push(obj);
}
return content;
} catch (e) {
Expand Down
5 changes: 5 additions & 0 deletions src/parsers/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ export interface BenoTypes {
path?: string;
envPath?: string;
}

export interface BenoSetTypes {
key: string;
val: string;
}

0 comments on commit c5a8762

Please sign in to comment.