Skip to content
This repository was archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
*: logging ergonomic improvement and style guide improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
zleyyij committed Jun 24, 2023
1 parent 01bf2d8 commit e89624b
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 317 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ COPY Makefile ./Makefile
COPY target/ ./target/

# Run it
CMD ["make", "run"]
CMD ["make", "profile"]
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ format:
lint:
npx gts lint

# Compile the code, and start the bot with a profiler running.
profile: build
# start the bot with a profiler running.
profile:
npx 0x -o --output-dir profile_results.0x ./target/core/main.js

# Build a docker container (requires dev deps)
Expand Down
36 changes: 17 additions & 19 deletions src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import {writeFile, readFile} from 'node:fs/promises';
import {readFileSync} from 'node:fs';
import {parse as parseJSONC, modify, applyEdits, JSONPath} from 'jsonc-parser';
import {EventCategory, eventLogger} from './logger.js';
import {EventCategory, logEvent} from './logger.js';

// TODO: write out a large interface for the config
/**
* Path of the config on the filesystem relative to where node is being run from
*/
Expand All @@ -17,6 +18,7 @@ const CONFIG_LOCATION = './config.jsonc';
* @namespace
*/
// The any type is needed because the json is written to this object at runtime
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const botConfig: any = {
/**
* This function populates `botConfig` with a mirror of `config.jsonc`
Expand Down Expand Up @@ -73,24 +75,23 @@ export const botConfig: any = {
* or filesystem operations fail in any way (bad perms, whatever). Will return silently and
* log an error if `location` does not point to a valid location in the config.
*/
// eslint-ignore-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async editConfigOption(location: JSONPath, newValue: any): Promise<void> {
// iteratively determine whether or not the key that's being edited exists
// eslint-disable-next-line @typescript-eslint/no-this-alias
let currentPosition = this;
for (const i in location) {
// see if the key exists
if (location[i] in currentPosition) {
currentPosition = currentPosition[location[i]];
} else {
eventLogger.logEvent(
{
category: EventCategory.Error,
location: 'core',
description:
"An attempt was made to edit a config value that doesn't exist(" +
location.join('.') +
'), cancelling edit',
},
logEvent(
EventCategory.Error,
'core',
"An attempt was made to edit a config value that doesn't exist( " +
location.join('.') +
'), cancelling edit',

1
);
return;
Expand All @@ -110,14 +111,11 @@ export const botConfig: any = {
);

await writeFile(CONFIG_LOCATION, newConfig);
eventLogger.logEvent(
{
category: EventCategory.Info,
location: 'any',
description:
location.join('.') +
' in `config.jsonc` changed and diff applied in memory',
},
logEvent(
EventCategory.Info,
'core',
location.join('.') +
' in `config.jsonc` changed and diff applied in memory',
2
);
},
Expand Down
Loading

0 comments on commit e89624b

Please sign in to comment.