Skip to content

Commit 4190338

Browse files
authored
fix: config path + yaml write/read path relative to module execution - fix #107 (#108)
* fix: config path + yaml write/read path relative to module execution - fix #107 * fix: config path + yaml write/read path relative to module execution - fix #107
1 parent 6e5ddda commit 4190338

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/modules/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readYaml, writeYaml } from "@oh/yaml";
1+
import { readYaml, writeYaml } from "./yaml.ts";
22
import type { ConfigProps } from "../types/config.types.ts";
33

44
export const getConfig = async <ConfigTypes extends {}>({
@@ -8,6 +8,7 @@ export const getConfig = async <ConfigTypes extends {}>({
88
}: ConfigProps<ConfigTypes>): Promise<ConfigTypes> => {
99
let config: ConfigTypes = {} as ConfigTypes;
1010
try {
11+
//@ts-ignore
1112
config = await readYaml<ConfigTypes>(`./${fileName}`);
1213
} catch (e) {}
1314

src/modules/yaml.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { parse, stringify } from "@std/yaml";
22
import { createDirectoryIfNotExists } from "../utils/directory.utils.ts";
3+
import { getModulePath } from "../utils/path.utils.ts";
34

45
export type ReadProps = {
56
decode?: boolean;
@@ -16,7 +17,7 @@ export const readYaml = async <T extends unknown>(
1617
filePath: string,
1718
{ decode = false }: ReadProps = { decode: false },
1819
): Promise<T | unknown> => {
19-
let content = await Deno.readTextFile(filePath);
20+
let content = await Deno.readTextFile(getModulePath(filePath));
2021
if (decode)
2122
content = decoder.decode(new Uint8Array(content.split(",").map(Number)));
2223
return parse(content);
@@ -29,11 +30,12 @@ export const writeYaml = async <T extends any>(
2930
encode: false,
3031
},
3132
): Promise<void> => {
32-
await createDirectoryIfNotExists(filePath);
33+
await createDirectoryIfNotExists(getModulePath(filePath));
3334

3435
let content = stringify(data);
3536
//@ts-ignore
3637
if (encode) content = encoder.encode(content);
3738

38-
await Deno.writeTextFile(filePath, content);
39+
console.log(getModulePath(filePath), content);
40+
await Deno.writeTextFile(getModulePath(filePath), content);
3941
};

src/utils/path.utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,15 @@ import * as path from "@std/path";
22

33
export const getExecPath = (): string => Deno.execPath();
44
export const getPath = (): string => path.dirname(getExecPath());
5+
6+
const __dirname = path.dirname(path.fromFileUrl(import.meta.url));
7+
8+
export const getLocalPath = (filePath: string): string => {
9+
if (filePath.startsWith("/") || /^[A-Za-z]:\\/.test(filePath)) {
10+
return filePath; // already absolute
11+
}
12+
return path.join(__dirname, filePath);
13+
};
14+
15+
export const getModulePath = (filePath: string): string =>
16+
path.join(path.dirname(path.fromFileUrl(Deno.mainModule)), filePath);

0 commit comments

Comments
 (0)