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