Fix file paths and error handling in server.js and

build.js
This commit is contained in:
Gabriel Pastori 2023-11-14 12:00:59 -03:00
parent b6f447bb0a
commit b44edc8631
3 changed files with 20 additions and 10 deletions

View File

@ -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';

View File

@ -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)

View File

@ -1,6 +1,6 @@
{
"name": "evolution-manager",
"version": "0.2.0",
"version": "0.2.1",
"main": "dist",
"scripts": {
"dev": "vite",