Skip to content

Commit

Permalink
✨ [feat/config] #83 add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
JAGFx committed Dec 25, 2021
1 parent b6884bc commit a6f05fc
Show file tree
Hide file tree
Showing 17 changed files with 646 additions and 64 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist/
build/
build/
logs/
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ yarn-error.log*
*.njsproj
*.sln
*.sw?

logs/
12 changes: 12 additions & 0 deletions .run/build.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="build" type="js.build_tools.npm" nameIsGenerated="true">
<package-json value="$PROJECT_DIR$/package.json" />
<command value="run" />
<scripts>
<script value="build" />
</scripts>
<node-interpreter value="project" />
<envs />
<method v="2" />
</configuration>
</component>
12 changes: 12 additions & 0 deletions .run/bundle.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="bundle" type="js.build_tools.npm" nameIsGenerated="true">
<package-json value="$PROJECT_DIR$/package.json" />
<command value="run" />
<scripts>
<script value="bundle" />
</scripts>
<node-interpreter value="project" />
<envs />
<method v="2" />
</configuration>
</component>
12 changes: 12 additions & 0 deletions .run/dashboard_dev.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="dashboard:dev" type="js.build_tools.npm" nameIsGenerated="true">
<package-json value="$PROJECT_DIR$/package.json" />
<command value="run" />
<scripts>
<script value="dashboard:dev" />
</scripts>
<node-interpreter value="project" />
<envs />
<method v="2" />
</configuration>
</component>
12 changes: 12 additions & 0 deletions .run/server_dev.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="server:dev" type="js.build_tools.npm" nameIsGenerated="true">
<package-json value="$PROJECT_DIR$/package.json" />
<command value="run" />
<scripts>
<script value="server:dev" />
</scripts>
<node-interpreter value="project" />
<envs />
<method v="2" />
</configuration>
</component>
75 changes: 62 additions & 13 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@

import { existsSync, readFileSync, writeFileSync } from 'fs';
import path from 'path';
import { ConfigurationException } from './exceptions.js';
import store from './store.js';

const oldConfigPath = (basePath = null) =>
path.resolve(
basePath ?? process.cwd(),
'./config/config.ets2-dashboard-skin.json'
);

export const appConfigPath = (basePath = null) => {
export const initAppConfig = () => {
return new Promise((resolve) => {
store.set('config', {
app: JSON.parse(appConfig()),
game: null
});
resolve();
});
};

export const appConfigPath = () => {
const basePath = store.get('libPath');
const newAppConfigPath = path.resolve(
basePath ?? process.cwd(),
`./config/config.json`
Expand All @@ -26,7 +39,8 @@ export const appConfigPath = (basePath = null) => {
: oldConfigPath(basePath);
};

export const gameConfigPath = (target, basePath = null) => {
export const gameConfigPath = (target) => {
const basePath = store.get('libPath');
const newGameConfigPath = path.resolve(
basePath ?? process.cwd(),
`./config/config.${target}.json`
Expand All @@ -37,19 +51,54 @@ export const gameConfigPath = (target, basePath = null) => {
: oldConfigPath(basePath);
};

export const appConfig = (basePath = null) =>
readFileSync(appConfigPath(basePath), 'UTF-8');
export const appConfig = () => {
const configFileName = appConfigPath();

export const gameConfig = (target, basePath = null) =>
readFileSync(gameConfigPath(target, basePath), 'UTF-8');
try {
return readFileSync(configFileName, 'UTF-8');
} catch (e) {
throw new ConfigurationException(
'Unable to read the configuration application file',
{ configFileName, reason: e.message }
);
}
};

export const saveAppConfig = (data, basePath = null) => {
writeFileSync(appConfigPath(basePath), JSON.stringify(data, null, 2));
export const gameConfig = (target) => {
const configFileName = gameConfigPath(target);

try {
return readFileSync(configFileName, 'UTF-8');
} catch (e) {
throw new ConfigurationException(
'Unable to read the configuration game file',
{ target, configFileName, reason: e.message }
);
}
};

export const saveGameConfig = (target, data, basePath = null) => {
writeFileSync(
gameConfigPath(target, basePath),
JSON.stringify(data, null, 2)
);
export const saveAppConfig = (data) => {
const configFileName = appConfigPath();

try {
writeFileSync(configFileName, JSON.stringify(data, null, 2));
} catch (e) {
throw new ConfigurationException(
'Unable to save the configuration application file',
{ configFileName, reason: e.message }
);
}
};

export const saveGameConfig = (target, data) => {
const configFileName = gameConfigPath(target);

try {
writeFileSync(configFileName, JSON.stringify(data, null, 2));
} catch (e) {
throw new ConfigurationException(
'Unable to save the configuration game file',
{ target, configFileName, reason: e.message }
);
}
};
6 changes: 6 additions & 0 deletions lib/exceptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class ConfigurationException extends Error {
constructor(message, metadata) {
super(message);
this.metadata = metadata;
}
}
47 changes: 47 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import fs from 'fs';
import path from 'path';
import winston from 'winston';
import { logger } from './store.js';

export const initLogger = () => {
return new Promise((resolve) => {
const logFileName = path.resolve(process.cwd(), 'logs/server.log');

if (fs.existsSync(logFileName)) fs.unlinkSync(logFileName);

logger.configure({
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.splat(),
winston.format.colorize(),
winston.format.simple()
),
handleExceptions: true,
handleRejections: true
}),
new winston.transports.File({
filename: logFileName,
format: winston.format.combine(
winston.format.splat(),
winston.format.metadata(),
winston.format.timestamp(),
loggerFormat
),
handleExceptions: true,
handleRejections: true
})
]
});

resolve();
});
};

export const loggerFormat = winston.format.printf(
({ level, message, timestamp, metadata }) => {
return `${timestamp} | ${level.toUpperCase()} | ${message} | ${JSON.stringify(
metadata
)}`;
}
);
Loading

0 comments on commit a6f05fc

Please sign in to comment.