-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: start building config importer
- Loading branch information
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
{} | ||
{ | ||
"imports": { | ||
"@std/fs": "jsr:@std/fs@^1.0.6", | ||
"@std/path": "jsr:@std/path@^1.0.8", | ||
"@std/yaml": "jsr:@std/yaml@^1.0.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
application_name: Karr Auth | ||
|
||
database: | ||
name: karr_auth | ||
user: karr_auth | ||
password: karr_auth | ||
host: localhost | ||
port: 5432 | ||
|
||
foo: bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"application_name": "Karr", | ||
"database": { | ||
"host": "localhost", | ||
"port": 5432, | ||
"username": "karr", | ||
"password": "karr", | ||
"database": "karr", | ||
"user_name": "karr" | ||
}, | ||
"admin_email": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { parse } from "@std/yaml" | ||
import { dirname, fromFileUrl, join } from "@std/path" | ||
import { existsSync } from "@std/fs" | ||
import { logger, toCamelCase } from "@util" | ||
|
||
import defaultConfig from "./default_config.json" with { type: "json" } | ||
|
||
type Config = typeof defaultConfig | ||
|
||
function resolvePath(file: string) { | ||
return join(dirname(fromFileUrl(import.meta.url)), file) | ||
} | ||
|
||
function parseFile(file: string = "../karr_config.yaml") { | ||
const filetype = file.split(".").pop() | ||
if (!filetype) { | ||
throw new Error("Invalid file type") | ||
} | ||
|
||
switch (filetype) { | ||
case "yaml": | ||
case "yml": | ||
return parse(Deno.readTextFileSync(file)) | ||
case "json": | ||
return JSON.parse(Deno.readTextFileSync(file)) | ||
default: | ||
throw new Error("Invalid file type") | ||
} | ||
} | ||
|
||
function camelCaseify(obj: Config) { | ||
return Object.keys(obj).reduce((acc: unknown, key: string) => { | ||
acc[toCamelCase(key)] = obj[key] | ||
return acc | ||
}, {} as Config) | ||
} | ||
|
||
export function readConfig() { | ||
const acceptedExtensions = ["yaml", "yml", "json"] | ||
|
||
for (const ext of acceptedExtensions) { | ||
const path = resolvePath(`../karr_config.${ext}`) | ||
|
||
if (existsSync(path)) { | ||
const userConfig = parseFile(path) | ||
|
||
const filteredUserConfig = | ||
(Object.keys(userConfig) as Array<keyof typeof defaultConfig>) | ||
.filter((key) => key in defaultConfig) | ||
.reduce((obj, key) => { | ||
obj[key] = userConfig[key] | ||
return obj | ||
}, {} as typeof defaultConfig) | ||
|
||
return camelCaseify(Object.assign(defaultConfig, filteredUserConfig)) | ||
} | ||
} | ||
|
||
logger.error("No configuration file found") | ||
return camelCaseify(defaultConfig) | ||
} | ||
|
||
if (import.meta.main) { | ||
logger.info("Read config", readConfig()) | ||
} |