From 56dd278df53f23a5c338bd7e9b4a93edf9c56a84 Mon Sep 17 00:00:00 2001 From: Yevhen Badorov Date: Wed, 1 Mar 2023 21:30:31 +0200 Subject: [PATCH 1/4] Read app path from applications file --- main.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/main.js b/main.js index b194a82..e970d92 100644 --- a/main.js +++ b/main.js @@ -1,25 +1,38 @@ 'use strict'; -const fastify = require('fastify'); +const { resolve } = require('node:path'); +const { readFile } = require('node:fs/promises'); -const path = require('node:path'); +const fastify = require('fastify'); const { Logger, StreamForLogger } = require('./src/logger.js'); const http = require('./src/http.js'); const ws = require('./src/ws.js'); const { loadApplication } = require('./src/loader.js'); -const APPLICATION_PATH = path.join(process.cwd(), '../NodeJS-Application'); +const APPLICATIONS_FILE_PATH = '.applications'; const LOG_FOLDER_PATH = './log'; +const getAppPaths = async (appFilePath) => { + const appsFile = await readFile(resolve(appFilePath), { + encoding: 'utf8', + }); + + return appsFile.split('\n').filter((path) => path.trim() !== ''); +}; + (async () => { const streamForLogger = new StreamForLogger(LOG_FOLDER_PATH); - const server = fastify({ logger: { level: 'info', stream: streamForLogger } }); + const server = fastify({ + logger: { level: 'info', stream: streamForLogger }, + }); const logger = new Logger(server.log); - const app = await loadApplication(APPLICATION_PATH, logger); + + const appPath = (await getAppPaths(APPLICATIONS_FILE_PATH))[0]; + const app = await loadApplication(appPath, logger); http.init(server, app.api); - http.initStatic(server, APPLICATION_PATH); + http.initStatic(server, appPath); ws.init(server, app.api); http.start(server, { port: app.config.server.ports[0] }); })(); From 49a027ba664adee4e7b9c5e62a69f2c3bef39f3a Mon Sep 17 00:00:00 2001 From: Yevhen Badorov Date: Wed, 1 Mar 2023 21:34:07 +0200 Subject: [PATCH 2/4] fix typo --- src/http.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/http.js b/src/http.js index b9dc4f1..99bb4b0 100644 --- a/src/http.js +++ b/src/http.js @@ -2,7 +2,7 @@ const path = require('node:path'); -const fastigyStatic = require('@fastify/static'); +const fastifyStatic = require('@fastify/static'); function init(server, routes) { /* TODO: session support */ @@ -24,7 +24,7 @@ function init(server, routes) { function initStatic(server, appPath) { const staticPath = path.join(appPath, 'static'); - server.register(fastigyStatic, { + server.register(fastifyStatic, { root: staticPath, wildcard: true, }); From 072817c8c90eaa3eb3c36915ad04468a70e4cefa Mon Sep 17 00:00:00 2001 From: Yevhen Badorov Date: Wed, 1 Mar 2023 21:36:11 +0200 Subject: [PATCH 3/4] change ECMAScript version in eslint config Change ECMAScript version in eslint config to pass linting. ECMAScript 2020 doesn't include private class fields that this project is using --- .eslintrc.json | 2 +- src/logger.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 69eb1e4..2e2caf9 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -6,7 +6,7 @@ }, "extends": "eslint:recommended", "parserOptions": { - "ecmaVersion": 2020 + "ecmaVersion": 2022 }, "globals": { "BigInt": true diff --git a/src/logger.js b/src/logger.js index 1fd761b..09922eb 100644 --- a/src/logger.js +++ b/src/logger.js @@ -47,7 +47,7 @@ class StreamForLogger { constructor(folderPath) { this.folderPath = folderPath; - this.date = new Date().toISOString().substring(0, 10); + this.date = new Date().toISOString().substring(0, 10); this.#createFileStream(); } @@ -69,7 +69,7 @@ class StreamForLogger { this.#logFileStream = fs.createWriteStream(filePath, { flags: 'a' }); return this.#logFileStream; } -}; +} module.exports = { Logger, From f1220b9ff9738cb2ed30e8f2ceaa51a21c734e0d Mon Sep 17 00:00:00 2001 From: Yevhen Badorov Date: Wed, 1 Mar 2023 23:06:09 +0200 Subject: [PATCH 4/4] Address code review comments --- .eslintrc.json | 2 +- main.js | 19 +++++++------------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 2e2caf9..d164636 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -6,7 +6,7 @@ }, "extends": "eslint:recommended", "parserOptions": { - "ecmaVersion": 2022 + "ecmaVersion": "latest" }, "globals": { "BigInt": true diff --git a/main.js b/main.js index e970d92..4308a24 100644 --- a/main.js +++ b/main.js @@ -1,7 +1,6 @@ 'use strict'; -const { resolve } = require('node:path'); -const { readFile } = require('node:fs/promises'); +const fsp = require('node:fs/promises'); const fastify = require('fastify'); @@ -10,25 +9,21 @@ const http = require('./src/http.js'); const ws = require('./src/ws.js'); const { loadApplication } = require('./src/loader.js'); -const APPLICATIONS_FILE_PATH = '.applications'; const LOG_FOLDER_PATH = './log'; -const getAppPaths = async (appFilePath) => { - const appsFile = await readFile(resolve(appFilePath), { - encoding: 'utf8', - }); - - return appsFile.split('\n').filter((path) => path.trim() !== ''); +const getAppPaths = async () => { + const apps = await fsp.readFile('.applications', 'utf8'); + return apps.split(/[\r\n\s]+/).filter((s) => s.length !== 0); }; (async () => { - const streamForLogger = new StreamForLogger(LOG_FOLDER_PATH); + const stream = new StreamForLogger(LOG_FOLDER_PATH); const server = fastify({ - logger: { level: 'info', stream: streamForLogger }, + logger: { level: 'info', stream }, }); const logger = new Logger(server.log); - const appPath = (await getAppPaths(APPLICATIONS_FILE_PATH))[0]; + const appPath = (await getAppPaths())[0]; const app = await loadApplication(appPath, logger); http.init(server, app.api);