From f1838bf69a55453eee3c23816c10bec0acc943c5 Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Tue, 4 Mar 2025 16:13:47 -0500 Subject: [PATCH] fix: fix invalid path to node with sh execution (#1213) Hoping this will resolve: https://forums.unraid.net/topic/187498-unable-to-install-my-server-plugin/ ## Summary by CodeRabbit - **New Features** - Improved command-line execution behavior for enhanced compatibility across different environments. - Enhanced the version retrieval process by allowing the system to locate configuration details from multiple potential sources. - Introduced a build enhancement that conditionally injects a startup script into bundled outputs, improving script portability and user experience. - Specified the Node.js interpreter path in the application configuration for better execution control. - Added a symbolic link for the Node.js binary to enhance accessibility. - **Bug Fixes** - Enhanced error handling for locating the `package.json` file, providing feedback when no valid file is found. - **Chores** - Removed unnecessary shell script that modified the system's `PATH` environment variable. --- api/ecosystem.config.json | 1 + api/src/cli.ts | 2 -- api/src/environment.ts | 28 ++++++++++++++----- api/vite.config.ts | 6 ++++ plugin/plugins/dynamix.unraid.net.plg | 2 ++ .../dynamix.unraid.net/etc/profile.d/node.sh | 6 ---- 6 files changed, 30 insertions(+), 15 deletions(-) delete mode 100755 plugin/source/dynamix.unraid.net/etc/profile.d/node.sh diff --git a/api/ecosystem.config.json b/api/ecosystem.config.json index 09412913f..6a78d511a 100644 --- a/api/ecosystem.config.json +++ b/api/ecosystem.config.json @@ -11,6 +11,7 @@ "max_restarts": 10, "min_uptime": 10000, "watch": false, + "interpreter": "/usr/local/node/bin/node", "ignore_watch": ["node_modules", "src", ".env.*", "myservers.cfg"], "log_file": "/var/log/graphql-api.log", "kill_timeout": 10000 diff --git a/api/src/cli.ts b/api/src/cli.ts index 0833faa11..b4165e0eb 100644 --- a/api/src/cli.ts +++ b/api/src/cli.ts @@ -1,5 +1,3 @@ -#!/usr/bin/env node - import '@app/dotenv.js'; import { execa } from 'execa'; diff --git a/api/src/environment.ts b/api/src/environment.ts index 9b4b8a9b9..53794a717 100644 --- a/api/src/environment.ts +++ b/api/src/environment.ts @@ -1,16 +1,30 @@ import { readFileSync } from 'node:fs'; import { homedir } from 'node:os'; -import { dirname, join } from 'node:path'; +import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; const getPackageJsonVersion = () => { try { - // Use import.meta.resolve to get the URL of package.json - const packageJsonUrl = import.meta.resolve('../package.json'); - const packageJsonPath = fileURLToPath(packageJsonUrl); - const packageJson = readFileSync(packageJsonPath, 'utf-8'); - const packageJsonObject = JSON.parse(packageJson); - return packageJsonObject.version; + // Try different possible locations for package.json + const possibleLocations = ['../package.json', '../../package.json']; + + for (const location of possibleLocations) { + try { + const packageJsonUrl = import.meta.resolve(location); + const packageJsonPath = fileURLToPath(packageJsonUrl); + const packageJson = readFileSync(packageJsonPath, 'utf-8'); + const packageJsonObject = JSON.parse(packageJson); + if (packageJsonObject.version) { + return packageJsonObject.version; + } + } catch { + // Continue to next location if this one fails + } + } + + // If we get here, we couldn't find a valid package.json in any location + console.error('Could not find package.json in any of the expected locations'); + return undefined; } catch (error) { console.error('Failed to load package.json:', error); return undefined; diff --git a/api/vite.config.ts b/api/vite.config.ts index 600468346..f279d86f3 100644 --- a/api/vite.config.ts +++ b/api/vite.config.ts @@ -90,6 +90,12 @@ export default defineConfig(({ mode }): ViteUserConfig => { entryFileNames: '[name].js', format: 'es', interop: 'auto', + banner: (chunk) => { + if (chunk.fileName === 'main.js' || chunk.fileName === 'cli.js') { + return '#!/usr/local/node/bin/node\n'; + } + return ''; + }, }, preserveEntrySignatures: 'strict', external: [ diff --git a/plugin/plugins/dynamix.unraid.net.plg b/plugin/plugins/dynamix.unraid.net.plg index 21bdfbff2..f20269ece 100755 --- a/plugin/plugins/dynamix.unraid.net.plg +++ b/plugin/plugins/dynamix.unraid.net.plg @@ -907,9 +907,11 @@ fi # Create symlink to unraid-api binary (to allow usage elsewhere) +ln -sf /usr/local/node/bin/node /usr/local/bin/node ln -sf ${unraid_binary_path} /usr/local/sbin/unraid-api ln -sf ${unraid_binary_path} /usr/bin/unraid-api + logger "Starting flash backup (if enabled)" echo "/etc/rc.d/rc.flash_backup start" | at -M now &>/dev/null . /root/.bashrc diff --git a/plugin/source/dynamix.unraid.net/etc/profile.d/node.sh b/plugin/source/dynamix.unraid.net/etc/profile.d/node.sh deleted file mode 100755 index ec790bbe3..000000000 --- a/plugin/source/dynamix.unraid.net/etc/profile.d/node.sh +++ /dev/null @@ -1,6 +0,0 @@ -#! /bin/bash - -# Add Node.js binary path to PATH if not already present -if [[ ":$PATH:" != *":/usr/local/node/bin:"* ]]; then - export PATH="/usr/local/node/bin:$PATH" -fi