From b44edc8631759b96f8866ff04faaa893c500d8c3 Mon Sep 17 00:00:00 2001 From: Gabriel Pastori <58153955+gabrielpastori1@users.noreply.github.com> Date: Tue, 14 Nov 2023 12:00:59 -0300 Subject: [PATCH] Fix file paths and error handling in server.js and build.js --- lib/server.js | 22 +++++++++++++++------- lib/utils/build.js | 6 ++++-- package.json | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/server.js b/lib/server.js index 92c5117..fcf2022 100644 --- a/lib/server.js +++ b/lib/server.js @@ -11,20 +11,28 @@ const functions = { } module.exports = async (argv) => { - if (argv._.length === 1) throw new Error('No operation specified') + try{ - const operation = argv._[1] - if (!functions[operation]) throw new Error(`Unknown operation: ${operation}`) - - await functions[operation](argv) + if (argv._.length === 1) throw new Error('No operation specified') + + const operation = argv._[1] + if (!functions[operation]) throw new Error(`Unknown operation: ${operation}`) + + await functions[operation](argv) + } catch (e) { + console.error(e.message || e) + process.exit(1) + } } +const path = require('path'); + function startServer(argv) { const { port = 9615 } = argv || {} - const index = fs.readFileSync(process.cwd() + '/dist/index.html'); + const index = fs.readFileSync(path.join(__dirname, '..', 'dist', 'index.html')); http.createServer(function (req, res) { try { @@ -32,7 +40,7 @@ function startServer(argv) { // verify if url is a file in dist folder if (parsedUrl.pathname === '/') throw {} - let filePath = process.cwd() + '/dist' + parsedUrl.pathname; + let filePath = path.join(__dirname, '..', 'dist', parsedUrl.pathname); if (fs.existsSync(filePath)) { const contentType = mime.lookup(filePath) || 'text/plain'; diff --git a/lib/utils/build.js b/lib/utils/build.js index 0fc3030..6708216 100644 --- a/lib/utils/build.js +++ b/lib/utils/build.js @@ -1,17 +1,19 @@ const { exec } = require('child_process') const fs = require('fs') +const path = require('path') module.exports = () => { return new Promise((resolve, reject) => { console.log('📦 Build start') console.time('📦 Build complete') - const distFolder = process.cwd() + '/dist' + const distFolder = path.join(__dirname, '..', '..', 'dist') if (fs.existsSync(distFolder)) { console.time('📦 Remove dist folder') fs.rmSync(distFolder, { recursive: true, force: true }) console.timeEnd('📦 Remove dist folder') } - exec('npm run build', (err, stdout) => { + + exec('npm run build', { cwd: path.join(__dirname, '..', '..') }, (err, stdout) => { if (err) { console.error(err) reject(err) diff --git a/package.json b/package.json index 5fd4a30..1822791 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "evolution-manager", - "version": "0.2.0", + "version": "0.2.1", "main": "dist", "scripts": { "dev": "vite",