fixed electron build + implemented dynamic settings + custom user data folder
This commit is contained in:
1 parent
8958ead210
commit
b872cd0733
14 files changed
+281
-76
No files matched your search
@@ -1,8 +1,14 @@
|
||||
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
||||
|
||||
# ! me
|
||||
settings.json
|
||||
sequelize.log
|
||||
showy.log
|
||||
|
||||
# Compiled output
|
||||
/dist
|
||||
/dist_exe
|
||||
/dist_electron
|
||||
/uploads
|
||||
/tmp
|
||||
/out-tsc
|
||||
|
||||
+8
-2
@@ -13,7 +13,7 @@
|
||||
"build": {
|
||||
"builder": "@angular-devkit/build-angular:application",
|
||||
"options": {
|
||||
"outputPath": "dist/show-z",
|
||||
"outputPath": "dist/showy",
|
||||
"index": "frontend/src/index.html",
|
||||
"browser": "frontend/src/main.ts",
|
||||
"polyfills": ["zone.js"],
|
||||
@@ -28,6 +28,7 @@
|
||||
"frontend/src/assets/css/bootstrap.min.css",
|
||||
"frontend/src/assets/css/bootstrap-extended.css",
|
||||
|
||||
"frontend/src/assets/plugins/bs-stepper/css/bs-stepper.css",
|
||||
"frontend/src/assets/plugins/metismenu/metisMenu.min.css",
|
||||
"frontend/src/assets/plugins/metismenu/mm-vertical.css",
|
||||
"frontend/src/assets/plugins/simplebar/css/simplebar.css",
|
||||
@@ -37,7 +38,6 @@
|
||||
"frontend/src/assets/sass/semi-dark.css",
|
||||
"frontend/src/assets/sass/dark-theme.css",
|
||||
|
||||
|
||||
"frontend/src/assets/sass/bordered-theme.css",
|
||||
|
||||
"node_modules/ngx-toastr/toastr.css",
|
||||
@@ -67,6 +67,12 @@
|
||||
},
|
||||
"configurations": {
|
||||
"production": {
|
||||
"fileReplacements": [
|
||||
{
|
||||
"replace": "frontend/src/environments/environment.ts",
|
||||
"with": "frontend/src/environments/environment.prod.ts"
|
||||
}
|
||||
],
|
||||
"budgets": [
|
||||
{
|
||||
"type": "initial",
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
|
||||
const CustomError = require('../errors/CustomError');
|
||||
const { logToFile } = require('../services/logService');
|
||||
|
||||
module.exports = (err, req, res, next) => {
|
||||
if (err instanceof CustomError) {
|
||||
res.status(err.statusCode()).json({ message: err.message });
|
||||
logToFile(`Error (${err.statusCode()}) ${err.message}`)
|
||||
} else {
|
||||
console.log(`📌 Error: `, err)
|
||||
logToFile(`Error (500) ${err}`)
|
||||
res.status(500).json({ message: 'Internal server error' });
|
||||
}
|
||||
};
|
||||
@@ -6,6 +6,7 @@ const entryRouter = require('./entry.routes');
|
||||
const entryRatingsRouter = require('./entry-ratings.routes');
|
||||
const seasonRouter = require('./season.routes');
|
||||
const episodeSeenRouter = require('./episode-seen.routes');
|
||||
const settingsRouter = require('./settings.routes');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -18,4 +19,6 @@ router.use('/entry-ratings', entryRatingsRouter);
|
||||
router.use('/seasons', seasonRouter);
|
||||
router.use('/episode-seen', episodeSeenRouter);
|
||||
|
||||
router.use('/settings', settingsRouter);
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,33 @@
|
||||
const express = require('express');
|
||||
const { updateSettings, getSettings } = require('../services/settingsService');
|
||||
const settingsRouter = express.Router();
|
||||
|
||||
const settingsFileFormat = {
|
||||
"dataFolderPath": "",
|
||||
"tmdbApiUrl": "",
|
||||
"tmdbApiKey": "",
|
||||
"frontPort": "",
|
||||
"backPort": "",
|
||||
"portableMode": "",
|
||||
};
|
||||
|
||||
// ! /settings
|
||||
settingsRouter.put('/', async (req, res) => {
|
||||
const settings = req.body;
|
||||
|
||||
updateSettings(settings);
|
||||
|
||||
res.status(200).send(getSettings());
|
||||
});
|
||||
|
||||
settingsRouter.get('/', (req, res) => {
|
||||
try {
|
||||
const settings = getSettings();
|
||||
|
||||
res.status(200).json(settings);
|
||||
} catch (err) {
|
||||
res.status(500).send('Error retrieving settings');
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = settingsRouter;
|
||||
+41
-20
@@ -1,22 +1,30 @@
|
||||
// server.js
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const routes = require('./routes/routes.js');
|
||||
const sequelize = require('./db/database.js');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 4000;
|
||||
|
||||
const relations = require('./db/relations.js');
|
||||
const sequelizeHooks = require('./db/sequelizeHooks.js');
|
||||
const { initializeGenres, initializeData } = require('./db/dataInitializer.js');
|
||||
const { initializeData } = require('./db/dataInitializer.js');
|
||||
const killerRouter = require('./routes/killer.routes.js');
|
||||
const errorHandler = require('./middleware/errorHandler');
|
||||
const { getSettings } = require('./services/settingsService.js');
|
||||
const { logToFile } = require('./services/logService.js');
|
||||
|
||||
// Middleware
|
||||
// ? variables
|
||||
let settings = getSettings()
|
||||
const PORT = settings["backPort"] || 4000;
|
||||
const parrentProcess = JSON.parse(process.env.PARENT_PROCESS || '{}');
|
||||
|
||||
// ? Middleware
|
||||
app.use(bodyParser.json()); // for parsing application/json
|
||||
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
|
||||
|
||||
// CORS Middleware to allow requests from any origin
|
||||
// * CORS Middleware to allow requests from any origin
|
||||
app.use((req, res, next) => {
|
||||
res.header('Access-Control-Allow-Origin', '*');
|
||||
res.header(
|
||||
@@ -30,17 +38,29 @@ app.use((req, res, next) => {
|
||||
next();
|
||||
});
|
||||
|
||||
// ? Routes
|
||||
// * media access route
|
||||
app.use('/api/v1/media/:category/:type', (req, res, next) => {
|
||||
// ? Routes : media access routes
|
||||
app.use('/api/v1/media/:category/:type/:file', (req, res, next) => {
|
||||
const category = req.params.category;
|
||||
const type = req.params.type;
|
||||
const file = req.params.file;
|
||||
|
||||
if (!['entries', 'collections', 'franchises'].includes(category)) {
|
||||
return res.status(404).send('Not Found');
|
||||
}
|
||||
|
||||
if (!['backdrops', 'posters', 'others'].includes(type)) {
|
||||
if (!['backdrops', 'posters', 'logos', 'others'].includes(type)) {
|
||||
return res.status(404).send('Not Found');
|
||||
}
|
||||
|
||||
const settings = getSettings();
|
||||
const dataPath = settings.userDataPath;
|
||||
|
||||
const mediaPath = path.join(dataPath, 'images', category, type);
|
||||
// const mediaPath = path.join(__dirname, '..', `data/images/${category}/${type}`);
|
||||
const filePath = path.join(mediaPath, file);
|
||||
logToFile(filePath)
|
||||
fs.access(filePath, fs.constants.F_OK, (err) => {
|
||||
if (err) {
|
||||
return res.status(404).send('Not Found');
|
||||
}
|
||||
|
||||
@@ -48,34 +68,35 @@ app.use('/api/v1/media/:category/:type', (req, res, next) => {
|
||||
fallthrough: false, // ! Important: don't call next middleware if file isn't found
|
||||
};
|
||||
|
||||
express.static(path.join(__dirname, '..', `uploads/${category}/${type}`), options)(req, res, next);
|
||||
// express.static(filePath, options)(req, res, next);
|
||||
// Serve the file using fs.createReadStream
|
||||
const stream = fs.createReadStream(filePath);
|
||||
stream.pipe(res);
|
||||
});
|
||||
});
|
||||
|
||||
// * other api routes
|
||||
// ? other api routes
|
||||
app.use('/api/v1', routes);
|
||||
|
||||
|
||||
// ! Error handling middleware
|
||||
const errorHandler = require('./middleware/errorHandler');
|
||||
// ? Error handling middleware
|
||||
app.use(errorHandler);
|
||||
|
||||
// ! KILLER ROUTE: used to crash the backend
|
||||
// ? KILLER ROUTE: used to crash the backend
|
||||
app.use('/killer', killerRouter);
|
||||
|
||||
// Start the server
|
||||
// * Start the server
|
||||
const doUpdate = false;
|
||||
const doForce = false;
|
||||
console.log(`Syncing database schema...`);
|
||||
sequelize
|
||||
// .sync({force: true}) // will drop all tables and recreate them.
|
||||
.sync({ alter: doUpdate }) // will attempt to automatically alter tables to fit the model definitions.
|
||||
// .sync()
|
||||
.then((result) => {
|
||||
console.log(`Database schema synced...`);
|
||||
console.log(`Database schema synced.`);
|
||||
initializeData();
|
||||
|
||||
app.listen(PORT, () => {
|
||||
//const Entry = require("./models/Entry.js")
|
||||
console.log(`Backend server running on http://localhost:${PORT}`);
|
||||
console.log(`Backend server running on http://localhost:${PORT}/`);
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
// logService.js
|
||||
const fs = require('fs');
|
||||
const util = require('util');
|
||||
|
||||
// Convert fs.writeFile to a promise-based function
|
||||
const writeFile = util.promisify(fs.writeFile);
|
||||
|
||||
// Ensure the log file exists and is accessible
|
||||
const logFilePath = 'showy.log';
|
||||
fs.open(logFilePath, 'a', (err, fd) => {
|
||||
if (err) {
|
||||
console.error("Failed to open log file:", err);
|
||||
} else {
|
||||
fs.close(fd, (err) => {
|
||||
if (err) console.error("Failed to close log file:", err);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Asynchronous function to append a message to the log file
|
||||
async function logToFile(message) {
|
||||
try {
|
||||
await writeFile(logFilePath, `${message}\n`, { flag: 'a' });
|
||||
} catch (err) {
|
||||
console.error("Failed to write to log file:", err);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
logToFile,
|
||||
};
|
||||
@@ -0,0 +1,61 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const parrentProcess = JSON.parse(process.env.PARENT_PROCESS || '{}');
|
||||
|
||||
let userDataPath = '';
|
||||
const defaultSettings = {
|
||||
userDataPath: '',
|
||||
tmdbApiUrl: '',
|
||||
tmdbApiKey: '',
|
||||
frontPort: '',
|
||||
backPort: '',
|
||||
portableMode: '',
|
||||
};
|
||||
|
||||
function getUserDataPath() {
|
||||
// return parrentProcess.electron_path || path.join(__dirname, '..', '..', 'data');
|
||||
return path.join(__dirname, '..', '..');
|
||||
}
|
||||
|
||||
function getSettingsFilePath() {
|
||||
return path.join(getUserDataPath(), 'settings.json');
|
||||
}
|
||||
|
||||
function getSettings() {
|
||||
const settingsFilePath = getSettingsFilePath();
|
||||
|
||||
try {
|
||||
const settingsData = fs.readFileSync(settingsFilePath);
|
||||
// console.log(`Loaded settings file.`);
|
||||
return JSON.parse(settingsData);
|
||||
} catch (err) {
|
||||
console.error('Error reading settings file:', err);
|
||||
console.log('Creating default settings file...');
|
||||
|
||||
let newSettingsFile = { ...defaultSettings };
|
||||
|
||||
let defaultDataPath = path.join(getUserDataPath(), 'data')
|
||||
|
||||
newSettingsFile.userDataPath = defaultDataPath;
|
||||
newSettingsFile.backPort = 4000;
|
||||
|
||||
fs.mkdirSync(defaultDataPath, { recursive: true });
|
||||
fs.writeFileSync(settingsFilePath, JSON.stringify(newSettingsFile, null, 2));
|
||||
console.log('Default settings file created.');
|
||||
|
||||
return defaultSettings;
|
||||
}
|
||||
}
|
||||
|
||||
function updateSettings(newSettings) {
|
||||
const settingsFilePath = getSettingsFilePath();
|
||||
|
||||
let updatedSettings = { ...userDataPath, ...newSettings };
|
||||
fs.mkdirSync(newSettings.userDataPath, { recursive: true }); // making sure the newly set data path is created before saving the new config
|
||||
fs.writeFileSync(settingsFilePath, JSON.stringify(updatedSettings, null, 2));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getSettings,
|
||||
updateSettings
|
||||
};
|
||||
@@ -10,7 +10,7 @@ import PerfectScrollbar from 'perfect-scrollbar';
|
||||
styleUrl: './app.component.css',
|
||||
})
|
||||
export class AppComponent implements AfterViewInit {
|
||||
title = 'ShowZ';
|
||||
title = 'Showy';
|
||||
|
||||
@ViewChild('scrollContainer', { static: true }) scrollContainer!: ElementRef;
|
||||
private ps!: PerfectScrollbar;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>ShowZ</title>
|
||||
<title>Showy</title>
|
||||
<base href="/">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico">
|
||||
|
||||
@@ -3,42 +3,17 @@ const { app, BrowserWindow } = require('electron');
|
||||
const path = require('path');
|
||||
const url = require('url');
|
||||
|
||||
let expressProcess;
|
||||
let win;
|
||||
|
||||
function createWindow() {
|
||||
win = new BrowserWindow({
|
||||
width: 1200,
|
||||
height: 720,
|
||||
webPreferences: {
|
||||
nodeIntegration: true
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
win.loadURL(
|
||||
url.format({
|
||||
pathname: path.join(__dirname, 'dist\\show-z\\browser\\index.html'),
|
||||
protocol: 'file:',
|
||||
slashes: true
|
||||
})
|
||||
);
|
||||
|
||||
win.on('closed', () => {
|
||||
win = null;
|
||||
});
|
||||
}
|
||||
|
||||
// app.on('ready', createWindow);
|
||||
|
||||
app.on('ready', () => {
|
||||
// Start the Express server
|
||||
const expressProcess = spawn('node', ['backend/server.js']);
|
||||
expressProcess.stdout.on('data', (data) => {
|
||||
console.log(`Express stdout: ${data}`);
|
||||
});
|
||||
expressProcess.stderr.on('data', (data) => {
|
||||
console.error(`Express stderr: ${data}`);
|
||||
});
|
||||
// ! Start the Express server loop
|
||||
startExpressServer();
|
||||
createMediaFolders();
|
||||
|
||||
// Start the Electron app
|
||||
createWindow();
|
||||
@@ -55,3 +30,60 @@ app.on('activate', () => {
|
||||
createWindow();
|
||||
}
|
||||
});
|
||||
|
||||
const { dialog, ipcMain } = require('electron');
|
||||
const { createMediaFolders } = require('./backend/services/mediaService');
|
||||
|
||||
ipcMain.on('select-dirs', async (event, arg) => {
|
||||
const result = await dialog.showOpenDialog(mainWindow, {
|
||||
properties: ['openDirectory']
|
||||
})
|
||||
console.log('directories selected', result.filePaths)
|
||||
})
|
||||
|
||||
function startExpressServer() {
|
||||
let parrentProcess = {
|
||||
"electron_path": app.getPath('userData')
|
||||
}
|
||||
// Start the Express server
|
||||
expressProcess = spawn('node', ['backend/server.js'], { env: { PARENT_PROCESS: JSON.stringify(parrentProcess) } });
|
||||
expressProcess.stdout.on('data', (data) => {
|
||||
console.log(`Express stdout: ${data}`);
|
||||
});
|
||||
expressProcess.stderr.on('data', (data) => {
|
||||
console.error(`Express stderr: ${data}`);
|
||||
});
|
||||
|
||||
// Restart the server if it crashes
|
||||
expressProcess.on('exit', (code, signal) => {
|
||||
console.log(`Express process exited with code ${code} and signal ${signal}. Restarting...`);
|
||||
startExpressServer();
|
||||
});
|
||||
}
|
||||
|
||||
function createWindow() {
|
||||
win = new BrowserWindow({
|
||||
width: 1200,
|
||||
height: 720,
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, 'preload.js'),
|
||||
nodeIntegration: true,
|
||||
enableRemoteModule: false,
|
||||
contextIsolation: true,
|
||||
sandbox: true
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
win.loadURL(
|
||||
url.format({
|
||||
pathname: path.join(__dirname, 'dist', 'showy', 'browser', 'index.html'),
|
||||
protocol: 'file:',
|
||||
slashes: true
|
||||
})
|
||||
);
|
||||
|
||||
win.on('closed', () => {
|
||||
win = null;
|
||||
});
|
||||
}
|
||||
Generated
+2
-2
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"name": "show-z",
|
||||
"name": "showy",
|
||||
"version": "0.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "show-z",
|
||||
"name": "showy",
|
||||
"version": "0.0.0",
|
||||
"dependencies": {
|
||||
"@angular/animations": "^17.0.0",
|
||||
|
||||
+21
-21
@@ -1,23 +1,34 @@
|
||||
{
|
||||
"name": "show-z",
|
||||
"name": "showy",
|
||||
"author": "X4Lo",
|
||||
"description": "Movies & Tv-Series Tracking library",
|
||||
"version": "0.0.0",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"ng": "ng",
|
||||
"start:backend": "nodemon backend/server.js",
|
||||
"start:frontend": "ng serve",
|
||||
"electron build": "ng build --configuration=production --base-href ./ && copyfiles backend/**/* dist/show-z && electron .",
|
||||
"build": "ng build",
|
||||
"Backend": "nodemon backend/server.js",
|
||||
"Frontend": "ng serve",
|
||||
"build-and-copy": "ng build --configuration production --base-href ./ && copyfiles backend/**/* dist/showy",
|
||||
"watch": "ng build --watch --configuration development",
|
||||
"test": "ng test",
|
||||
"electron-build": "npm-run-all -s electron-build:*",
|
||||
"electron-build:all": "ng build --configuration=production --base-href ./ && copyfiles backend/**/* dist/showy && electron . && electron-builder",
|
||||
"electron-build:ng": "ng build --configuration=production --base-href ./",
|
||||
"electron-build:copy": "copyfiles backend/**/* dist/show-z",
|
||||
"electron-build:copy": "copyfiles backend/**/* dist/showy",
|
||||
"electron-build:electron": "electron .",
|
||||
"electron-build:exe": "electron-builder"
|
||||
"electron-build:exe": "electron-builder",
|
||||
"electron-copy-be": "copyfiles backend/**/* dist_electron/win-unpacked"
|
||||
},
|
||||
"build": {
|
||||
"appId": "xalo.showy",
|
||||
"productName": "Showy",
|
||||
"win": {
|
||||
"target": "nsis",
|
||||
"icon": "frontend/to/icon.ico"
|
||||
},
|
||||
"directories": {
|
||||
"output": "dist_electron"
|
||||
}
|
||||
},
|
||||
"portable": true,
|
||||
"singleInstance": false,
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@angular/animations": "^17.0.0",
|
||||
@@ -64,16 +75,5 @@
|
||||
"karma-jasmine-html-reporter": "~2.1.0",
|
||||
"nodemon": "^3.1.0",
|
||||
"typescript": "~5.2.2"
|
||||
},
|
||||
"build": {
|
||||
"appId": "xalo.showy",
|
||||
"productName": "Showy",
|
||||
"win": {
|
||||
"target": "nsis",
|
||||
"icon": "frontend/to/icon.ico"
|
||||
},
|
||||
"directories": {
|
||||
"output": "dist_exe"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
const { ipcRenderer } = require('electron')
|
||||
|
||||
process.once('loaded', () => {
|
||||
window.addEventListener('message', evt => {
|
||||
if (evt.data.type === 'select-dirs') {
|
||||
ipcRenderer.send('select-dirs')
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user