From 6db619d3c4a9a02943e7262726502473318d8c09 Mon Sep 17 00:00:00 2001 From: Onja Date: Tue, 10 Oct 2023 15:52:08 +0300 Subject: [PATCH] Refactor the code to load the project path from the config/constants.js file and fix a bug in the parse method where it returns false instead of rejecting the promise with an error message when columns are invalid --- src/app.js | 3 --- src/config/constants.js | 3 +++ src/models/file.js | 11 ++++++++--- 3 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 src/config/constants.js diff --git a/src/app.js b/src/app.js index 4a24c3e..8c13c2a 100644 --- a/src/app.js +++ b/src/app.js @@ -8,9 +8,6 @@ var indexRouter = require('./routes/index'); var app = express(); -// Declare a global variable to hold project path -global.__basedir = __dirname; - // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); diff --git a/src/config/constants.js b/src/config/constants.js new file mode 100644 index 0000000..be33c9b --- /dev/null +++ b/src/config/constants.js @@ -0,0 +1,3 @@ +module.exports = { + basedir: __dirname, +}; \ No newline at end of file diff --git a/src/models/file.js b/src/models/file.js index 1eb911e..e775916 100644 --- a/src/models/file.js +++ b/src/models/file.js @@ -1,3 +1,4 @@ +/* global basedir */ const path = require('path'); const URL = require("url"); const fs = require('fs'); @@ -12,7 +13,10 @@ const { PassThrough } = require('stream'); const slugify = require('slugify'); const e = require('express'); -const dest = path.join(appRoot, 'public/csv'); +// Load project path from config/constants.js +const { basedir } = require('../config/constants'); + +const dest = path.join(basedir, 'public/csv'); // Create a generateFilePath function witch returns a path with a filename and datetime @@ -116,12 +120,13 @@ class File extends EventEmitter { // create a parse method which read the file and return a stream - async parse(columns) { + parse(columns) { const stream = new PassThrough(); // check if columns is valid if (!columns || columns.length) { - return Promise.reject(new Error('Invalid columns')); + // return Promise.reject(new Error('Invalid columns')); + return false; } // Create a variable to hold csv columns indexes