Compare commits

..

18 Commits

Author SHA1 Message Date
github-actions[bot]
20d5e70d65 chore: add VERSION v0.4.9 2025-10-24 20:39:06 +00:00
Michel Roegl-Brunner
68b6fc81bd Merge pull request #247 from community-scripts/fix/script-card-click-issues
Fix: Autosync disable functionality and multiple instance issues
2025-10-24 22:36:37 +02:00
Michel Roegl-Brunner
16947cedf5 Remove debug logs from autosync service
- Remove excessive debug logging from scheduleAutoSync and stopAutoSync
- Keep essential logging for troubleshooting
- Clean up console output for production use
2025-10-24 22:30:26 +02:00
Michel Roegl-Brunner
eca0cb57f9 Fix TypeScript linting errors in autoSyncService
- Add JSDoc type definitions for settings object
- Fix appriseUrls undefined checks
- Fix null assignment for lastAutoSyncError
- Add proper type annotations for error fields

All TypeScript linting errors are now resolved.
2025-10-24 22:30:15 +02:00
Michel Roegl-Brunner
011cbd23b2 Fix autosync continuing to run after being disabled
- Add destroy() method to properly stop cron jobs
- Add additional safety checks in cron job execution
- Add debugging logs to track cron job lifecycle
- Ensure isRunning is set to false even when no cron job exists
- Add null check for cronJob before execution

This should prevent autosync from continuing to run after being disabled.
2025-10-24 22:29:37 +02:00
Michel Roegl-Brunner
19e18b4ebf Fix multiple autosync instances running simultaneously
- Add global lock to prevent multiple autosync instances from running
- Add initialization check to prevent multiple service creation
- Add global lock checks in cron job execution
- Prevent multiple notifications from being sent
- Fix TypeScript errors with error field types

This fixes the issue where 6+ autosync instances were running simultaneously,
causing multiple notifications and rate limit issues.
2025-10-24 22:27:40 +02:00
Michel Roegl-Brunner
ffef6313d4 Add error reporting for GitHub rate limit errors
- Add specific error handling for GitHub API rate limit (403) errors
- Create RateLimitError with proper error name for identification
- Store last sync error and error time in settings for UI display
- Add error status display in autosync settings modal
- Show user-friendly error messages with GitHub token suggestion
- Clear error status on successful sync
- Update both GitHubJsonService and GitHubService with rate limit error handling

This provides better user feedback when GitHub API rate limits are exceeded
and guides users to set up a GitHub token for higher rate limits.
2025-10-24 22:23:59 +02:00
Michel Roegl-Brunner
7b4daf8754 Fix autosync continuing to run after being disabled
- Add defensive check in cron job execution to stop if autosync is disabled
- Ensure isRunning flag is set to false when stopping autosync
- Add logging to show when autosync is disabled and not scheduling
- Fix both API route and TRPC router to properly stop service
- Prevent multiple cron jobs from running simultaneously

This fixes the issue where autosync would continue running even after
being disabled in the GUI, causing rate limit errors and unwanted syncs.
2025-10-24 22:20:13 +02:00
Michel Roegl-Brunner
fdeda6c77a Add GitHub token authentication to sync services
- Add GitHub token authentication to GitHubJsonService for API calls
- Add GitHub token authentication to GitHubService for API calls
- Update fetchFromGitHub methods to use GITHUB_TOKEN from .env
- Update downloadJsonFile methods to use GitHub token for raw file downloads
- Add proper error handling for rate limit exceeded (403) errors
- Add console logging to show when token is/isn't being used
- Improve error messages to suggest setting GITHUB_TOKEN for higher rate limits

This ensures that when a GitHub token is specified in .env, it will be used
for all GitHub API calls during sync operations, providing higher rate limits
and better reliability.
2025-10-24 22:16:31 +02:00
Michel Roegl-Brunner
5acaf144fb Fix autosync toggle disable functionality
- Fix service instance management to use global instance for stopping autosync
- Add automatic saving when toggle is changed (no manual save required)
- Fix validation issue where custom sync type without cron expression caused 400 error
- Add comprehensive debugging and error handling
- Ensure .env file is properly updated with AUTO_SYNC_ENABLED value
- Improve service lifecycle management with proper state cleanup
- Add fallback logic for invalid sync interval configurations

Resolves issue where disabling autosync in GUI didn't update .env file or stop service
2025-10-24 22:15:24 +02:00
Michel Roegl-Brunner
926032e83b Fix auto-sync service not stopping when disabled
- Use global auto-sync service instance instead of creating new instances
- This ensures stopAutoSync() is called on the actual running service
- Fix .env file parsing to handle comments and empty lines properly
- Update both TRPC and REST API endpoints to use global instance
- Auto-sync service will now properly stop when disabled in settings
2025-10-24 22:09:16 +02:00
Michel Roegl-Brunner
8fc9b27f55 Fix missing access import in scriptDownloader.js
- Add missing 'access' import from 'fs/promises'
- This was causing checkScriptExists method to fail with 'access is not defined' error
- Now properly detects when script files exist locally
- Fixes issue where UI shows 'Load Script' instead of 'Install' button for downloaded scripts
2025-10-24 22:07:46 +02:00
Michel Roegl-Brunner
9a8cff3227 Add missing checkScriptExists method to scriptDownloader.js
- Add checkScriptExists method that was missing from JavaScript implementation
- This method is required by the checkScriptFiles API endpoint
- Method checks if CT script and install script files exist locally
- Returns ctExists, installExists, and files array
- Fixes issue where UI shows 'Load Script' instead of 'Install' button for downloaded scripts
2025-10-24 22:06:34 +02:00
Michel Roegl-Brunner
e40bd1f6a3 Fix CT script source line replacement
- Implement modifyScriptContent method to replace GitHub source line with local source
- Replace 'source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/build.func)'
  with 'SCRIPT_DIR="." \nsource "/../core/build.func"'
- This ensures CT scripts use local build.func instead of downloading from GitHub
- Applied to all CT scripts during download process
- Tested with 2fauth script - replacement works correctly
2025-10-24 22:04:23 +02:00
Michel Roegl-Brunner
6c982050da Fix notification to show script names grouped by category
- Store full script objects instead of just names in results.newScripts and results.updatedScripts
- This allows groupScriptsByCategory to properly group scripts by their categories
- Notification will now show actual script names grouped by category instead of 'undefined synced, undefined up to date'
- Script objects contain name, slug, and categories fields needed for proper grouping
2025-10-24 22:02:56 +02:00
Michel Roegl-Brunner
b40f5b788c Fix script downloader placeholder files issue
- Delete stub scriptDownloader.js that contained placeholder implementation
- Implement real JavaScript script downloader with GitHub fetch functionality
- Fix incremental JSON sync to only process newly synced files
- Add proper error handling and file structure management
- Support all script types (ct/, tools/, vm/, vw/) with directory preservation
- Download install scripts for CT scripts
- Re-enable auto-sync service to use real implementation

Scripts now download real content from GitHub instead of placeholders.
2025-10-24 21:58:35 +02:00
Michel Roegl-Brunner
2c3fdf5544 Fix script card click issues and missing Install button
- Fix type signature mismatch in handleCardClick functions
  - Updated ScriptsGrid.tsx and DownloadedScriptsTab.tsx to accept ScriptCardType instead of { slug: string }
  - This resolves the modal not opening when clicking script cards

- Fix server-side import issues
  - Updated autoSyncService.js to import from githubJsonService.ts instead of .js
  - Fixed path aliases in githubJsonService.ts to use relative imports
  - Updated scripts.ts to import from TypeScript service files directly

- Fix missing Install button
  - Resolved scriptDownloaderService.checkScriptExists method not being available
  - Install button now appears when script files exist locally

- Remove debug logging
  - Cleaned up temporary console.log statements and debug UI elements

All script card interactions now work properly:
- Cards open detail modal when clicked
- Install button appears when appropriate
- Server-side API calls work correctly
2025-10-24 21:22:46 +02:00
github-actions[bot]
74dd29b87b chore: add VERSION v0.4.8 (#239)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-10-24 11:00:54 +00:00
36 changed files with 2394 additions and 2054 deletions

View File

@@ -27,3 +27,12 @@ AUTH_ENABLED=false
AUTH_SETUP_COMPLETED=false
JWT_SECRET=
DATABASE_URL="file:/opt/ProxmoxVE-Local/data/settings.db"
AUTO_SYNC_ENABLED=false
SYNC_INTERVAL_TYPE=
SYNC_INTERVAL_PREDEFINED=
AUTO_DOWNLOAD_NEW=
AUTO_UPDATE_EXISTING=
NOTIFICATION_ENABLED=
APPRISE_URLS=
LAST_AUTO_SYNC=
SYNC_INTERVAL_CRON=

View File

@@ -1 +1 @@
0.4.8
0.4.9

View File

@@ -12,7 +12,7 @@
"documentation": "https://docs.bunkerweb.io/latest/",
"website": "https://www.bunkerweb.io/",
"logo": "https://cdn.jsdelivr.net/gh/selfhst/icons/webp/bunkerweb.webp",
"config_path": "/opt/bunkerweb/variables.env",
"config_path": "/etc/bunkerweb/variables.env",
"description": "BunkerWeb is a security-focused web server that enhances web application protection. It guards against common web vulnerabilities like SQL injection, XSS, and CSRF. It features simple setup and configuration using a YAML file, customizable security rules, and provides detailed logs for traffic monitoring and threat detection.",
"install_methods": [
{

48
scripts/json/execute.json Normal file
View File

@@ -0,0 +1,48 @@
{
"name": "PVE LXC Execute Command",
"slug": "lxc-execute",
"categories": [
1
],
"date_created": "2025-09-18",
"type": "pve",
"updateable": false,
"privileged": false,
"interface_port": null,
"documentation": null,
"website": null,
"logo": "https://cdn.jsdelivr.net/gh/selfhst/icons/webp/proxmox.webp",
"config_path": "",
"description": "This script allows administrators to execute a custom command inside one or multiple LXC containers on a Proxmox VE node. Containers can be selectively excluded via an interactive checklist. If a container is stopped, the script will automatically start it, run the command, and then shut it down again. Only Debian and Ubuntu based containers are supported.",
"install_methods": [
{
"type": "default",
"script": "tools/pve/execute.sh",
"resources": {
"cpu": null,
"ram": null,
"hdd": null,
"os": null,
"version": null
}
}
],
"default_credentials": {
"username": null,
"password": null
},
"notes": [
{
"text": "Execute within the Proxmox shell.",
"type": "info"
},
{
"text": "Non-Debian/Ubuntu containers will be skipped automatically.",
"type": "info"
},
{
"text": "Stopped containers will be started temporarily to run the command, then shut down again.",
"type": "warning"
}
]
}

View File

@@ -23,7 +23,7 @@
"ram": 2048,
"hdd": 10,
"os": "debian",
"version": "12"
"version": "13"
}
}
],

View File

@@ -12,7 +12,7 @@
"documentation": "https://github.com/HydroshieldMKII/Guardian/blob/main/README.md",
"config_path": "/opt/guardian/.env",
"website": "https://github.com/HydroshieldMKII/Guardian",
"logo": null,
"logo": "https://cdn.jsdelivr.net/gh/selfhst/icons/webp/guardian-plex.webp",
"description": "Guardian is a lightweight companion app for Plex that lets you monitor, approve or block devices in real time. It helps you enforce per-user or global policies, stop unwanted sessions automatically and grant temporary access - all through a simple web interface.",
"install_methods": [
{

View File

@@ -21,7 +21,7 @@
"resources": {
"cpu": 2,
"ram": 2048,
"hdd": 8,
"hdd": 16,
"os": "ubuntu",
"version": "24.04"
}

40
scripts/json/jotty.json Normal file
View File

@@ -0,0 +1,40 @@
{
"name": "jotty",
"slug": "jotty",
"categories": [
12
],
"date_created": "2025-10-21",
"type": "ct",
"updateable": true,
"privileged": false,
"interface_port": 3000,
"documentation": "https://github.com/fccview/jotty/blob/main/README.md",
"website": "https://github.com/fccview/jotty",
"logo": "https://cdn.jsdelivr.net/gh/selfhst/icons/webp/jotty.webp",
"config_path": "/opt/jotty/.env",
"description": "A simple, self-hosted app for your checklists and notes. Tired of bloated, cloud-based to-do apps? jotty is a lightweight alternative for managing your personal checklists and notes. It's built with Next.js 14, is easy to deploy, and keeps all your data on your own server.",
"install_methods": [
{
"type": "default",
"script": "ct/jotty.sh",
"resources": {
"cpu": 2,
"ram": 3072,
"hdd": 6,
"os": "debian",
"version": "13"
}
}
],
"default_credentials": {
"username": null,
"password": null
},
"notes": [
{
"text": "jotty was previously named rwMarkable",
"type": "info"
}
]
}

View File

@@ -23,7 +23,7 @@
"ram": 2048,
"hdd": 8,
"os": "debian",
"version": "12"
"version": "13"
}
}
],

View File

@@ -1,186 +1,186 @@
{
"categories": [
{
"name": "Proxmox & Virtualization",
"id": 1,
"sort_order": 1.0,
"description": "Tools and scripts to manage Proxmox VE and virtualization platforms effectively.",
"icon": "server"
},
{
"name": "Operating Systems",
"id": 2,
"sort_order": 2.0,
"description": "Scripts for deploying and managing various operating systems.",
"icon": "monitor"
},
{
"name": "Containers & Docker",
"id": 3,
"sort_order": 3.0,
"description": "Solutions for containerization using Docker and related technologies.",
"icon": "box"
},
{
"name": "Network & Firewall",
"id": 4,
"sort_order": 4.0,
"description": "Enhance network security and configure firewalls with ease.",
"icon": "shield"
},
{
"name": "Adblock & DNS",
"id": 5,
"sort_order": 5.0,
"description": "Optimize your network with DNS and ad-blocking solutions.",
"icon": "ban"
},
{
"name": "Authentication & Security",
"id": 6,
"sort_order": 6.0,
"description": "Secure your infrastructure with authentication and security tools.",
"icon": "lock"
},
{
"name": "Backup & Recovery",
"id": 7,
"sort_order": 7.0,
"description": "Reliable backup and recovery scripts to protect your data.",
"icon": "archive"
},
{
"name": "Databases",
"id": 8,
"sort_order": 8.0,
"description": "Deploy and manage robust database systems with ease.",
"icon": "database"
},
{
"name": "Monitoring & Analytics",
"id": 9,
"sort_order": 9.0,
"description": "Monitor system performance and analyze data seamlessly.",
"icon": "bar-chart"
},
{
"name": "Dashboards & Frontends",
"id": 10,
"sort_order": 10.0,
"description": "Create interactive dashboards and user-friendly frontends.",
"icon": "layout"
},
{
"name": "Files & Downloads",
"id": 11,
"sort_order": 11.0,
"description": "Manage file sharing and downloading solutions efficiently.",
"icon": "download"
},
{
"name": "Documents & Notes",
"id": 12,
"sort_order": 12.0,
"description": "Organize and manage documents and note-taking tools.",
"icon": "file-text"
},
{
"name": "Media & Streaming",
"id": 13,
"sort_order": 13.0,
"description": "Stream and manage media effortlessly across devices.",
"icon": "play"
},
{
"name": "*Arr Suite",
"id": 14,
"sort_order": 14.0,
"description": "Automated media management with the popular *Arr suite tools.",
"icon": "tv"
},
{
"name": "NVR & Cameras",
"id": 15,
"sort_order": 15.0,
"description": "Manage network video recorders and camera setups.",
"icon": "camera"
},
{
"name": "IoT & Smart Home",
"id": 16,
"sort_order": 16.0,
"description": "Control and automate IoT devices and smart home systems.",
"icon": "home"
},
{
"name": "ZigBee, Z-Wave & Matter",
"id": 17,
"sort_order": 17.0,
"description": "Solutions for ZigBee, Z-Wave, and Matter-based device management.",
"icon": "radio"
},
{
"name": "MQTT & Messaging",
"id": 18,
"sort_order": 18.0,
"description": "Set up reliable messaging and MQTT-based communication systems.",
"icon": "message-circle"
},
{
"name": "Automation & Scheduling",
"id": 19,
"sort_order": 19.0,
"description": "Automate tasks and manage scheduling with powerful tools.",
"icon": "clock"
},
{
"name": "AI / Coding & Dev-Tools",
"id": 20,
"sort_order": 20.0,
"description": "Leverage AI and developer tools for smarter coding workflows.",
"icon": "code"
},
{
"name": "Webservers & Proxies",
"id": 21,
"sort_order": 21.0,
"description": "Deploy and configure web servers and proxy solutions.",
"icon": "globe"
},
{
"name": "Bots & ChatOps",
"id": 22,
"sort_order": 22.0,
"description": "Enhance collaboration with bots and ChatOps integrations.",
"icon": "bot"
},
{
"name": "Finance & Budgeting",
"id": 23,
"sort_order": 23.0,
"description": "Track expenses and manage budgets efficiently.",
"icon": "dollar-sign"
},
{
"name": "Gaming & Leisure",
"id": 24,
"sort_order": 24.0,
"description": "Scripts for gaming servers and leisure-related tools.",
"icon": "gamepad-2"
},
{
"name": "Business & ERP",
"id": 25,
"sort_order": 25.0,
"description": "Streamline business operations with ERP and management tools.",
"icon": "building"
},
{
"name": "Miscellaneous",
"id": 0,
"sort_order": 99.0,
"description": "General scripts and tools that don't fit into other categories.",
"icon": "more-horizontal"
}
]
}
"categories": [
{
"name": "Proxmox & Virtualization",
"id": 1,
"sort_order": 1.0,
"description": "Tools and scripts to manage Proxmox VE and virtualization platforms effectively.",
"icon": "server"
},
{
"name": "Operating Systems",
"id": 2,
"sort_order": 2.0,
"description": "Scripts for deploying and managing various operating systems.",
"icon": "monitor"
},
{
"name": "Containers & Docker",
"id": 3,
"sort_order": 3.0,
"description": "Solutions for containerization using Docker and related technologies.",
"icon": "box"
},
{
"name": "Network & Firewall",
"id": 4,
"sort_order": 4.0,
"description": "Enhance network security and configure firewalls with ease.",
"icon": "shield"
},
{
"name": "Adblock & DNS",
"id": 5,
"sort_order": 5.0,
"description": "Optimize your network with DNS and ad-blocking solutions.",
"icon": "ban"
},
{
"name": "Authentication & Security",
"id": 6,
"sort_order": 6.0,
"description": "Secure your infrastructure with authentication and security tools.",
"icon": "lock"
},
{
"name": "Backup & Recovery",
"id": 7,
"sort_order": 7.0,
"description": "Reliable backup and recovery scripts to protect your data.",
"icon": "archive"
},
{
"name": "Databases",
"id": 8,
"sort_order": 8.0,
"description": "Deploy and manage robust database systems with ease.",
"icon": "database"
},
{
"name": "Monitoring & Analytics",
"id": 9,
"sort_order": 9.0,
"description": "Monitor system performance and analyze data seamlessly.",
"icon": "bar-chart"
},
{
"name": "Dashboards & Frontends",
"id": 10,
"sort_order": 10.0,
"description": "Create interactive dashboards and user-friendly frontends.",
"icon": "layout"
},
{
"name": "Files & Downloads",
"id": 11,
"sort_order": 11.0,
"description": "Manage file sharing and downloading solutions efficiently.",
"icon": "download"
},
{
"name": "Documents & Notes",
"id": 12,
"sort_order": 12.0,
"description": "Organize and manage documents and note-taking tools.",
"icon": "file-text"
},
{
"name": "Media & Streaming",
"id": 13,
"sort_order": 13.0,
"description": "Stream and manage media effortlessly across devices.",
"icon": "play"
},
{
"name": "*Arr Suite",
"id": 14,
"sort_order": 14.0,
"description": "Automated media management with the popular *Arr suite tools.",
"icon": "tv"
},
{
"name": "NVR & Cameras",
"id": 15,
"sort_order": 15.0,
"description": "Manage network video recorders and camera setups.",
"icon": "camera"
},
{
"name": "IoT & Smart Home",
"id": 16,
"sort_order": 16.0,
"description": "Control and automate IoT devices and smart home systems.",
"icon": "home"
},
{
"name": "ZigBee, Z-Wave & Matter",
"id": 17,
"sort_order": 17.0,
"description": "Solutions for ZigBee, Z-Wave, and Matter-based device management.",
"icon": "radio"
},
{
"name": "MQTT & Messaging",
"id": 18,
"sort_order": 18.0,
"description": "Set up reliable messaging and MQTT-based communication systems.",
"icon": "message-circle"
},
{
"name": "Automation & Scheduling",
"id": 19,
"sort_order": 19.0,
"description": "Automate tasks and manage scheduling with powerful tools.",
"icon": "clock"
},
{
"name": "AI / Coding & Dev-Tools",
"id": 20,
"sort_order": 20.0,
"description": "Leverage AI and developer tools for smarter coding workflows.",
"icon": "code"
},
{
"name": "Webservers & Proxies",
"id": 21,
"sort_order": 21.0,
"description": "Deploy and configure web servers and proxy solutions.",
"icon": "globe"
},
{
"name": "Bots & ChatOps",
"id": 22,
"sort_order": 22.0,
"description": "Enhance collaboration with bots and ChatOps integrations.",
"icon": "bot"
},
{
"name": "Finance & Budgeting",
"id": 23,
"sort_order": 23.0,
"description": "Track expenses and manage budgets efficiently.",
"icon": "dollar-sign"
},
{
"name": "Gaming & Leisure",
"id": 24,
"sort_order": 24.0,
"description": "Scripts for gaming servers and leisure-related tools.",
"icon": "gamepad-2"
},
{
"name": "Business & ERP",
"id": 25,
"sort_order": 25.0,
"description": "Streamline business operations with ERP and management tools.",
"icon": "building"
},
{
"name": "Miscellaneous",
"id": 0,
"sort_order": 99.0,
"description": "General scripts and tools that don't fit into other categories.",
"icon": "more-horizontal"
}
]
}

View File

@@ -23,7 +23,7 @@
"ram": 1024,
"hdd": 4,
"os": "debian",
"version": "13"
"version": "12"
}
}
],

View File

@@ -23,7 +23,7 @@
"ram": 2048,
"hdd": 8,
"os": "debian",
"version": "13"
"version": "12"
}
},
{

View File

@@ -23,7 +23,7 @@
"ram": 2048,
"hdd": 6,
"os": "debian",
"version": "13"
"version": "12"
}
}
],

View File

@@ -23,7 +23,7 @@
"ram": 1024,
"hdd": 4,
"os": "debian",
"version": "13"
"version": "12"
}
}
],

View File

@@ -0,0 +1,40 @@
{
"name": "Open-Archiver",
"slug": "open-archiver",
"categories": [
7
],
"date_created": "2025-10-18",
"type": "ct",
"updateable": true,
"privileged": false,
"interface_port": 3000,
"documentation": "https://docs.openarchiver.com/",
"config_path": "/opt/openarchiver/.env",
"website": "https://openarchiver.com/",
"logo": "https://cdn.jsdelivr.net/gh/selfhst/icons/webp/open-archiver.webp",
"description": "Open Archiver is a secure, self-hosted email archiving solution, and it's completely open source. Get an email archiver that enables full-text search across email and attachments. Create a permanent, searchable, and compliant mail archive from Google Workspace, Microsoft 35, and any IMAP server.",
"install_methods": [
{
"type": "default",
"script": "ct/open-archiver.sh",
"resources": {
"cpu": 2,
"ram": 3072,
"hdd": 8,
"os": "debian",
"version": "13"
}
}
],
"default_credentials": {
"username": null,
"password": null
},
"notes": [
{
"text": "Data directory is: `/opt/openarchiver-data`. If you have a lot of email, you might consider mounting external storage to this directory.",
"type": "info"
}
]
}

View File

@@ -23,7 +23,7 @@
"ram": 8192,
"hdd": 25,
"os": "debian",
"version": "13"
"version": "12"
}
}
],

View File

@@ -19,8 +19,8 @@
"type": "default",
"script": "ct/paperless-ai.sh",
"resources": {
"cpu": 2,
"ram": 2048,
"cpu": 4,
"ram": 4096,
"hdd": 20,
"os": "debian",
"version": "13"

View File

@@ -23,7 +23,7 @@
"ram": 1024,
"hdd": 4,
"os": "debian",
"version": "13"
"version": "12"
}
}
],

View File

@@ -23,7 +23,7 @@
"ram": 512,
"hdd": 2,
"os": "debian",
"version": "13"
"version": "12"
}
}
],

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -23,7 +23,7 @@
"ram": 2048,
"hdd": 5,
"os": "Debian",
"version": "12"
"version": "13"
}
}
],

View File

@@ -356,7 +356,7 @@ export function DownloadedScriptsTab({ onInstallScript }: DownloadedScriptsTabPr
}
}, [selectedCategory]);
const handleCardClick = (scriptCard: { slug: string }) => {
const handleCardClick = (scriptCard: ScriptCardType) => {
// All scripts are GitHub scripts, open modal
setSelectedSlug(scriptCard.slug);
setIsModalOpen(true);

View File

@@ -46,6 +46,8 @@ export function GeneralSettingsModal({ isOpen, onClose }: GeneralSettingsModalPr
const [appriseUrls, setAppriseUrls] = useState<string[]>([]);
const [appriseUrlsText, setAppriseUrlsText] = useState('');
const [lastAutoSync, setLastAutoSync] = useState('');
const [lastAutoSyncError, setLastAutoSyncError] = useState<string | null>(null);
const [lastAutoSyncErrorTime, setLastAutoSyncErrorTime] = useState<string | null>(null);
const [cronValidationError, setCronValidationError] = useState('');
// Load existing settings when modal opens
@@ -311,6 +313,8 @@ export function GeneralSettingsModal({ isOpen, onClose }: GeneralSettingsModalPr
setAppriseUrls(settings.appriseUrls ?? []);
setAppriseUrlsText((settings.appriseUrls ?? []).join('\n'));
setLastAutoSync(settings.lastAutoSync ?? '');
setLastAutoSyncError(settings.lastAutoSyncError ?? null);
setLastAutoSyncErrorTime(settings.lastAutoSyncErrorTime ?? null);
}
}
} catch (error) {
@@ -323,30 +327,6 @@ export function GeneralSettingsModal({ isOpen, onClose }: GeneralSettingsModalPr
setMessage(null);
try {
// Validate cron expression if custom
if (syncIntervalType === 'custom' && syncIntervalCron) {
const response = await fetch('/api/settings/auto-sync', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
autoSyncEnabled,
syncIntervalType,
syncIntervalPredefined,
syncIntervalCron,
autoDownloadNew,
autoUpdateExisting,
notificationEnabled,
appriseUrls: appriseUrls
})
});
if (!response.ok) {
const errorData = await response.json();
setMessage({ type: 'error', text: errorData.error ?? 'Failed to save auto-sync settings' });
return;
}
}
const response = await fetch('/api/settings/auto-sync', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
@@ -361,7 +341,7 @@ export function GeneralSettingsModal({ isOpen, onClose }: GeneralSettingsModalPr
appriseUrls: appriseUrls
})
});
if (response.ok) {
setMessage({ type: 'success', text: 'Auto-sync settings saved successfully!' });
setTimeout(() => setMessage(null), 3000);
@@ -824,7 +804,41 @@ export function GeneralSettingsModal({ isOpen, onClose }: GeneralSettingsModalPr
</div>
<Toggle
checked={autoSyncEnabled}
onCheckedChange={setAutoSyncEnabled}
onCheckedChange={async (checked) => {
setAutoSyncEnabled(checked);
// Auto-save when toggle changes
try {
// If syncIntervalType is custom but no cron expression, fallback to predefined
const effectiveSyncIntervalType = (syncIntervalType === 'custom' && !syncIntervalCron)
? 'predefined'
: syncIntervalType;
const response = await fetch('/api/settings/auto-sync', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
autoSyncEnabled: checked,
syncIntervalType: effectiveSyncIntervalType,
syncIntervalPredefined: effectiveSyncIntervalType === 'predefined' ? syncIntervalPredefined : undefined,
syncIntervalCron: effectiveSyncIntervalType === 'custom' ? syncIntervalCron : undefined,
autoDownloadNew,
autoUpdateExisting,
notificationEnabled,
appriseUrls: appriseUrls
})
});
if (response.ok) {
// Update local state to reflect the effective sync interval type
if (effectiveSyncIntervalType !== syncIntervalType) {
setSyncIntervalType(effectiveSyncIntervalType);
}
}
} catch (error) {
console.error('Error saving auto-sync toggle:', error);
}
}}
disabled={isSaving}
/>
</div>
@@ -1016,6 +1030,25 @@ export function GeneralSettingsModal({ isOpen, onClose }: GeneralSettingsModalPr
</div>
)}
{lastAutoSyncError && (
<div className="p-3 bg-error/10 text-error-foreground border border-error/20 rounded-md">
<div className="flex items-start gap-2">
<svg className="w-4 h-4 mt-0.5 flex-shrink-0" fill="currentColor" viewBox="0 0 20 20">
<path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clipRule="evenodd" />
</svg>
<div>
<p className="text-sm font-medium">Last sync error:</p>
<p className="text-sm mt-1">{lastAutoSyncError}</p>
{lastAutoSyncErrorTime && (
<p className="text-xs mt-1 opacity-75">
{new Date(lastAutoSyncErrorTime).toLocaleString()}
</p>
)}
</div>
</div>
</div>
)}
<div className="flex gap-2">
<Button
onClick={triggerManualSync}

View File

@@ -574,7 +574,7 @@ export function ScriptsGrid({ onInstallScript }: ScriptsGridProps) {
}, []);
const handleCardClick = (scriptCard: { slug: string }) => {
const handleCardClick = (scriptCard: ScriptCardType) => {
// All scripts are GitHub scripts, open modal
setSelectedSlug(scriptCard.slug);
setIsModalOpen(true);

View File

@@ -64,14 +64,12 @@ export async function POST(request: NextRequest) {
// Validate custom cron expression
if (settings.syncIntervalType === 'custom') {
if (!settings.syncIntervalCron || typeof settings.syncIntervalCron !== 'string') {
return NextResponse.json(
{ error: 'Custom cron expression is required when syncIntervalType is "custom"' },
{ status: 400 }
);
}
if (!isValidCron(settings.syncIntervalCron, { seconds: false })) {
if (!settings.syncIntervalCron || typeof settings.syncIntervalCron !== 'string' || settings.syncIntervalCron.trim() === '') {
// Fallback to predefined if custom is selected but no cron expression
settings.syncIntervalType = 'predefined';
settings.syncIntervalPredefined = settings.syncIntervalPredefined || '1hour';
settings.syncIntervalCron = '';
} else if (!isValidCron(settings.syncIntervalCron, { seconds: false })) {
return NextResponse.json(
{ error: 'Invalid cron expression' },
{ status: 400 }
@@ -138,7 +136,9 @@ export async function POST(request: NextRequest) {
'AUTO_UPDATE_EXISTING': settings.autoUpdateExisting ? 'true' : 'false',
'NOTIFICATION_ENABLED': settings.notificationEnabled ? 'true' : 'false',
'APPRISE_URLS': Array.isArray(settings.appriseUrls) ? JSON.stringify(settings.appriseUrls) : (settings.appriseUrls || '[]'),
'LAST_AUTO_SYNC': settings.lastAutoSync || ''
'LAST_AUTO_SYNC': settings.lastAutoSync || '',
'LAST_AUTO_SYNC_ERROR': settings.lastAutoSyncError || '',
'LAST_AUTO_SYNC_ERROR_TIME': settings.lastAutoSyncErrorTime || ''
};
// Update or add each setting
@@ -160,15 +160,28 @@ export async function POST(request: NextRequest) {
// Reschedule auto-sync service with new settings
try {
const { AutoSyncService } = await import('../../../../server/services/autoSyncService.js');
const autoSyncService = new AutoSyncService();
const { getAutoSyncService, setAutoSyncService } = await import('../../../../server/lib/autoSyncInit.js');
let autoSyncService = getAutoSyncService();
// If no global instance exists, create one
if (!autoSyncService) {
const { AutoSyncService } = await import('../../../../server/services/autoSyncService.js');
autoSyncService = new AutoSyncService();
setAutoSyncService(autoSyncService);
}
// Update the global service instance with new settings
autoSyncService.saveSettings(settings);
if (settings.autoSyncEnabled) {
autoSyncService.scheduleAutoSync();
console.log('Auto-sync rescheduled with new settings');
} else {
autoSyncService.stopAutoSync();
console.log('Auto-sync stopped');
// Ensure the service is completely stopped and won't restart
autoSyncService.isRunning = false;
// Also stop the global service instance if it exists
const { stopAutoSync: stopGlobalAutoSync } = await import('../../../../server/lib/autoSyncInit.js');
stopGlobalAutoSync();
}
} catch (error) {
console.error('Error rescheduling auto-sync service:', error);
@@ -204,7 +217,9 @@ export async function GET() {
autoUpdateExisting: false,
notificationEnabled: false,
appriseUrls: [],
lastAutoSync: ''
lastAutoSync: '',
lastAutoSyncError: null,
lastAutoSyncErrorTime: null
}
});
}
@@ -228,7 +243,9 @@ export async function GET() {
return [];
}
})(),
lastAutoSync: getEnvValue(envContent, 'LAST_AUTO_SYNC') || ''
lastAutoSync: getEnvValue(envContent, 'LAST_AUTO_SYNC') || '',
lastAutoSyncError: getEnvValue(envContent, 'LAST_AUTO_SYNC_ERROR') || null,
lastAutoSyncErrorTime: getEnvValue(envContent, 'LAST_AUTO_SYNC_ERROR_TIME') || null
};
return NextResponse.json({ settings });

View File

@@ -115,6 +115,18 @@ export const scriptsRouter = createTRPCRouter({
.input(z.object({ slug: z.string() }))
.query(async ({ input }) => {
try {
console.log('getScriptBySlug called with slug:', input.slug);
console.log('githubJsonService methods:', Object.getOwnPropertyNames(Object.getPrototypeOf(githubJsonService)));
console.log('githubJsonService.getScriptBySlug type:', typeof githubJsonService.getScriptBySlug);
if (typeof githubJsonService.getScriptBySlug !== 'function') {
return {
success: false,
error: 'getScriptBySlug method is not available on githubJsonService',
script: null
};
}
const script = await githubJsonService.getScriptBySlug(input.slug);
if (!script) {
return {
@@ -125,6 +137,7 @@ export const scriptsRouter = createTRPCRouter({
}
return { success: true, script };
} catch (error) {
console.error('Error in getScriptBySlug:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to fetch script',
@@ -490,14 +503,29 @@ export const scriptsRouter = createTRPCRouter({
}))
.mutation(async ({ input }) => {
try {
const autoSyncService = new AutoSyncService();
// Use the global auto-sync service instance
const { getAutoSyncService, setAutoSyncService } = await import('~/server/lib/autoSyncInit');
let autoSyncService = getAutoSyncService();
// If no global instance exists, create one
if (!autoSyncService) {
const { AutoSyncService } = await import('~/server/services/autoSyncService');
autoSyncService = new AutoSyncService();
setAutoSyncService(autoSyncService);
}
// Save settings to both .env file and service instance
autoSyncService.saveSettings(input);
// Reschedule auto-sync if enabled
if (input.autoSyncEnabled) {
autoSyncService.scheduleAutoSync();
console.log('Auto-sync rescheduled with new settings');
} else {
autoSyncService.stopAutoSync();
// Ensure the service is completely stopped and won't restart
autoSyncService.isRunning = false;
console.log('Auto-sync stopped');
}
return { success: true, message: 'Auto-sync settings saved successfully' };

View File

@@ -1,21 +1,31 @@
import { AutoSyncService } from '../services/autoSyncService.js';
let autoSyncService = null;
let isInitialized = false;
/**
* Initialize auto-sync service and schedule cron job if enabled
*/
export function initializeAutoSync() {
if (isInitialized) {
console.log('Auto-sync service already initialized, skipping...');
return;
}
try {
console.log('Initializing auto-sync service...');
autoSyncService = new AutoSyncService();
isInitialized = true;
console.log('AutoSyncService instance created');
// Load settings and schedule if enabled
const settings = autoSyncService.loadSettings();
console.log('Settings loaded:', settings);
if (settings.autoSyncEnabled) {
console.log('Auto-sync is enabled, scheduling cron job...');
autoSyncService.scheduleAutoSync();
console.log('Cron job scheduled');
} else {
console.log('Auto-sync is disabled');
}
@@ -23,6 +33,7 @@ export function initializeAutoSync() {
console.log('Auto-sync service initialized successfully');
} catch (error) {
console.error('Failed to initialize auto-sync service:', error);
console.error('Error stack:', error.stack);
}
}
@@ -35,6 +46,7 @@ export function stopAutoSync() {
console.log('Stopping auto-sync service...');
autoSyncService.stopAutoSync();
autoSyncService = null;
isInitialized = false;
console.log('Auto-sync service stopped');
}
} catch (error) {
@@ -49,6 +61,13 @@ export function getAutoSyncService() {
return autoSyncService;
}
/**
* Set the auto-sync service instance (for external management)
*/
export function setAutoSyncService(service) {
autoSyncService = service;
}
/**
* Graceful shutdown handler
*/

View File

@@ -49,6 +49,13 @@ export function getAutoSyncService(): AutoSyncService | null {
return autoSyncService;
}
/**
* Set the auto-sync service instance (for external management)
*/
export function setAutoSyncService(service: AutoSyncService | null): void {
autoSyncService = service;
}
/**
* Graceful shutdown handler
*/

View File

@@ -25,6 +25,25 @@ export class ScriptManager {
// Initialize lazily to avoid accessing env vars during module load
}
/**
* Safely handle file modification time, providing fallback for invalid dates
* @param mtime - The file modification time from fs.stat
* @returns Date - Valid date or current date as fallback
*/
private safeMtime(mtime: Date): Date {
try {
// Check if the date is valid
if (!mtime || isNaN(mtime.getTime())) {
console.warn('Invalid mtime detected, using current time as fallback');
return new Date();
}
return mtime;
} catch (error) {
console.warn('Error processing mtime:', error);
return new Date();
}
}
private initializeConfig() {
if (this.scriptsDir === null) {
// Handle both absolute and relative paths for testing
@@ -63,7 +82,7 @@ export class ScriptManager {
path: filePath,
extension,
size: stats.size,
lastModified: stats.mtime,
lastModified: this.safeMtime(stats.mtime),
executable
});
}
@@ -125,7 +144,7 @@ export class ScriptManager {
path: filePath,
extension,
size: stats.size,
lastModified: stats.mtime,
lastModified: this.safeMtime(stats.mtime),
executable,
logo,
slug
@@ -212,7 +231,7 @@ export class ScriptManager {
path: filePath,
extension,
size: stats.size,
lastModified: stats.mtime,
lastModified: this.safeMtime(stats.mtime),
executable,
logo,
slug

View File

@@ -6,12 +6,34 @@ import { readFile, writeFile, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import cronValidator from 'cron-validator';
// Global lock to prevent multiple autosync instances from running simultaneously
let globalAutoSyncLock = false;
export class AutoSyncService {
constructor() {
this.cronJob = null;
this.isRunning = false;
}
/**
* Safely convert a date to ISO string, handling invalid dates
* @param {Date} date - The date to convert
* @returns {string} - ISO string or fallback timestamp
*/
safeToISOString(date) {
try {
// Check if the date is valid
if (!date || isNaN(date.getTime())) {
console.warn('Invalid date provided to safeToISOString, using current time as fallback');
return new Date().toISOString();
}
return date.toISOString();
} catch (error) {
console.warn('Error converting date to ISO string:', error instanceof Error ? error.message : String(error));
return new Date().toISOString();
}
}
/**
* Load auto-sync settings from .env file
*/
@@ -20,6 +42,19 @@ export class AutoSyncService {
const envPath = join(process.cwd(), '.env');
const envContent = readFileSync(envPath, 'utf8');
/** @type {{
* autoSyncEnabled: boolean;
* syncIntervalType: string;
* syncIntervalPredefined?: string;
* syncIntervalCron?: string;
* autoDownloadNew: boolean;
* autoUpdateExisting: boolean;
* notificationEnabled: boolean;
* appriseUrls?: string[];
* lastAutoSync?: string;
* lastAutoSyncError?: string;
* lastAutoSyncErrorTime?: string;
* }} */
const settings = {
autoSyncEnabled: false,
syncIntervalType: 'predefined',
@@ -29,7 +64,9 @@ export class AutoSyncService {
autoUpdateExisting: false,
notificationEnabled: false,
appriseUrls: [],
lastAutoSync: ''
lastAutoSync: '',
lastAutoSyncError: '',
lastAutoSyncErrorTime: ''
};
const lines = envContent.split('\n');
@@ -74,6 +111,12 @@ export class AutoSyncService {
case 'LAST_AUTO_SYNC':
settings.lastAutoSync = value;
break;
case 'LAST_AUTO_SYNC_ERROR':
settings.lastAutoSyncError = value;
break;
case 'LAST_AUTO_SYNC_ERROR_TIME':
settings.lastAutoSyncErrorTime = value;
break;
}
}
}
@@ -90,7 +133,9 @@ export class AutoSyncService {
autoUpdateExisting: false,
notificationEnabled: false,
appriseUrls: [],
lastAutoSync: ''
lastAutoSync: '',
lastAutoSyncError: '',
lastAutoSyncErrorTime: ''
};
}
}
@@ -107,6 +152,8 @@ export class AutoSyncService {
* @param {boolean} settings.notificationEnabled
* @param {Array<string>} [settings.appriseUrls]
* @param {string} [settings.lastAutoSync]
* @param {string} [settings.lastAutoSyncError]
* @param {string} [settings.lastAutoSyncErrorTime]
*/
saveSettings(settings) {
try {
@@ -130,19 +177,37 @@ export class AutoSyncService {
'AUTO_UPDATE_EXISTING': settings.autoUpdateExisting.toString(),
'NOTIFICATION_ENABLED': settings.notificationEnabled.toString(),
'APPRISE_URLS': JSON.stringify(settings.appriseUrls || []),
'LAST_AUTO_SYNC': settings.lastAutoSync || ''
'LAST_AUTO_SYNC': settings.lastAutoSync || '',
'LAST_AUTO_SYNC_ERROR': settings.lastAutoSyncError || '',
'LAST_AUTO_SYNC_ERROR_TIME': settings.lastAutoSyncErrorTime || ''
};
const existingKeys = new Set();
for (const line of lines) {
const [key] = line.split('=');
const trimmedKey = key?.trim();
if (trimmedKey && trimmedKey in settingsMap) {
// @ts-ignore - Dynamic key access is safe here
newLines.push(`${trimmedKey}=${settingsMap[trimmedKey]}`);
existingKeys.add(trimmedKey);
} else if (trimmedKey && !(trimmedKey in settingsMap)) {
const trimmedLine = line.trim();
// Skip empty lines and comments
if (!trimmedLine || trimmedLine.startsWith('#')) {
newLines.push(line);
continue;
}
const equalIndex = trimmedLine.indexOf('=');
if (equalIndex === -1) {
// Line doesn't contain '=', keep as is
newLines.push(line);
continue;
}
const key = trimmedLine.substring(0, equalIndex).trim();
if (key && key in settingsMap) {
// Replace existing setting
// @ts-ignore - Dynamic property access is safe here
newLines.push(`${key}=${settingsMap[key]}`);
existingKeys.add(key);
} else {
// Keep other settings as is
newLines.push(line);
}
}
@@ -170,6 +235,14 @@ export class AutoSyncService {
const settings = this.loadSettings();
if (!settings.autoSyncEnabled) {
console.log('Auto-sync is disabled, not scheduling cron job');
this.isRunning = false; // Ensure we're completely stopped
return;
}
// Check if there's already a global autosync running
if (globalAutoSyncLock) {
console.log('Auto-sync is already running globally, not scheduling new cron job');
return;
}
@@ -200,8 +273,28 @@ export class AutoSyncService {
console.log(`Scheduling auto-sync with cron expression: ${cronExpression}`);
this.cronJob = cron.schedule(cronExpression, async () => {
// Check global lock first
if (globalAutoSyncLock) {
console.log('Auto-sync already running globally, skipping cron execution...');
return;
}
if (this.isRunning) {
console.log('Auto-sync already running, skipping...');
console.log('Auto-sync already running locally, skipping...');
return;
}
// Double-check that autosync is still enabled before executing
const currentSettings = this.loadSettings();
if (!currentSettings.autoSyncEnabled) {
console.log('Auto-sync has been disabled, stopping and destroying cron job');
this.stopAutoSync();
return;
}
// Additional check: if cronJob is null, it means it was stopped
if (!this.cronJob) {
console.log('Cron job was stopped, skipping execution');
return;
}
@@ -221,8 +314,13 @@ export class AutoSyncService {
stopAutoSync() {
if (this.cronJob) {
this.cronJob.stop();
this.cronJob.destroy();
this.cronJob = null;
console.log('Auto-sync cron job stopped');
this.isRunning = false;
console.log('Auto-sync cron job stopped and destroyed');
} else {
console.log('No active cron job to stop');
this.isRunning = false; // Ensure isRunning is false even if no cron job
}
}
@@ -230,11 +328,19 @@ export class AutoSyncService {
* Execute auto-sync process
*/
async executeAutoSync() {
if (this.isRunning) {
console.log('Auto-sync already running, skipping...');
return { success: false, message: 'Auto-sync already running' };
// Check global lock first
if (globalAutoSyncLock) {
console.log('Auto-sync already running globally, skipping...');
return { success: false, message: 'Auto-sync already running globally' };
}
if (this.isRunning) {
console.log('Auto-sync already running locally, skipping...');
return { success: false, message: 'Auto-sync already running locally' };
}
// Set global lock
globalAutoSyncLock = true;
this.isRunning = true;
const startTime = new Date();
@@ -251,56 +357,120 @@ export class AutoSyncService {
const results = {
jsonSync: syncResult,
newScripts: [],
updatedScripts: [],
errors: []
newScripts: /** @type {any[]} */ ([]),
updatedScripts: /** @type {any[]} */ ([]),
errors: /** @type {string[]} */ ([])
};
// Step 2: Auto-download/update scripts if enabled
const settings = this.loadSettings();
if (settings.autoDownloadNew || settings.autoUpdateExisting) {
console.log('Processing synced JSON files for script downloads...');
// Only process scripts for files that were actually synced
// @ts-ignore - syncedFiles exists in the JavaScript version
if (syncResult.syncedFiles && syncResult.syncedFiles.length > 0) {
// @ts-ignore - syncedFiles exists in the JavaScript version
console.log(`Processing ${syncResult.syncedFiles.length} synced JSON files for new scripts...`);
console.log(`Processing ${syncResult.syncedFiles.length} synced JSON files for script downloads...`);
// Get all scripts from synced files
// @ts-ignore - syncedFiles exists in the JavaScript version
const allSyncedScripts = await githubJsonService.getScriptsForFiles(syncResult.syncedFiles);
// Get scripts only for the synced files
const localScriptsService = await import('./localScripts.js');
const syncedScripts = [];
// Initialize script downloader service
// @ts-ignore - initializeConfig is public in the JS version
scriptDownloaderService.initializeConfig();
// Filter to only truly NEW scripts (not previously downloaded)
const newScripts = [];
for (const script of allSyncedScripts) {
const isDownloaded = await scriptDownloaderService.isScriptDownloaded(script);
if (!isDownloaded) {
newScripts.push(script);
for (const filename of syncResult.syncedFiles) {
try {
// Extract slug from filename (remove .json extension)
const slug = filename.replace('.json', '');
const script = await localScriptsService.localScriptsService.getScriptBySlug(slug);
if (script) {
syncedScripts.push(script);
}
} catch (error) {
console.warn(`Error loading script from ${filename}:`, error);
}
}
console.log(`Found ${newScripts.length} new scripts out of ${allSyncedScripts.length} total scripts`);
console.log(`Found ${syncedScripts.length} scripts from synced JSON files`);
if (settings.autoDownloadNew && newScripts.length > 0) {
console.log(`Auto-downloading ${newScripts.length} new scripts...`);
const downloadResult = await scriptDownloaderService.autoDownloadNewScripts(newScripts);
// @ts-ignore - Type assertion needed for dynamic assignment
results.newScripts = downloadResult.downloaded;
// @ts-ignore - Type assertion needed for dynamic assignment
results.errors.push(...downloadResult.errors);
// Filter to only truly NEW scripts (not previously downloaded)
const newScripts = [];
const existingScripts = [];
for (const script of syncedScripts) {
try {
// Validate script object
if (!script || !script.slug) {
console.warn('Invalid script object found, skipping:', script);
continue;
}
const isDownloaded = await scriptDownloaderService.isScriptDownloaded(script);
if (!isDownloaded) {
newScripts.push(script);
} else {
existingScripts.push(script);
}
} catch (error) {
console.warn(`Error checking script ${script?.slug || 'unknown'}:`, error);
// Treat as new script if we can't check
if (script && script.slug) {
newScripts.push(script);
}
}
}
if (settings.autoUpdateExisting) {
console.log('Auto-updating existing scripts from synced files...');
const updateResult = await scriptDownloaderService.autoUpdateExistingScripts(allSyncedScripts);
// @ts-ignore - Type assertion needed for dynamic assignment
results.updatedScripts = updateResult.updated;
// @ts-ignore - Type assertion needed for dynamic assignment
results.errors.push(...updateResult.errors);
console.log(`Found ${newScripts.length} new scripts and ${existingScripts.length} existing scripts from synced files`);
// Download new scripts
if (settings.autoDownloadNew && newScripts.length > 0) {
console.log(`Auto-downloading ${newScripts.length} new scripts...`);
const downloaded = [];
const errors = [];
for (const script of newScripts) {
try {
const result = await scriptDownloaderService.loadScript(script);
if (result.success) {
downloaded.push(script); // Store full script object for category grouping
console.log(`Downloaded script: ${script.name || script.slug}`);
} else {
errors.push(`${script.name || script.slug}: ${result.message}`);
}
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
errors.push(`${script.name || script.slug}: ${errorMsg}`);
console.error(`Failed to download script ${script.slug}:`, error);
}
}
results.newScripts = downloaded;
results.errors.push(...errors);
}
// Update existing scripts
if (settings.autoUpdateExisting && existingScripts.length > 0) {
console.log(`Auto-updating ${existingScripts.length} existing scripts...`);
const updated = [];
const errors = [];
for (const script of existingScripts) {
try {
// Always update existing scripts when auto-update is enabled
const result = await scriptDownloaderService.loadScript(script);
if (result.success) {
updated.push(script); // Store full script object for category grouping
console.log(`Updated script: ${script.name || script.slug}`);
} else {
errors.push(`${script.name || script.slug}: ${result.message}`);
}
} catch (error) {
const errorMsg = error instanceof Error ? error.message : String(error);
errors.push(`${script.name || script.slug}: ${errorMsg}`);
console.error(`Failed to update script ${script.slug}:`, error);
}
}
results.updatedScripts = updated;
results.errors.push(...errors);
}
} else {
console.log('No JSON files were synced, skipping script download/update');
@@ -310,14 +480,19 @@ export class AutoSyncService {
}
// Step 3: Send notifications if enabled
if (settings.notificationEnabled && settings.appriseUrls?.length > 0) {
console.log('Sending notifications...');
if (settings.notificationEnabled && settings.appriseUrls && settings.appriseUrls.length > 0) {
console.log('Sending success notifications...');
await this.sendSyncNotification(results);
console.log('Success notifications sent');
}
// Step 4: Update last sync time
const lastSyncTime = new Date().toISOString();
const updatedSettings = { ...settings, lastAutoSync: lastSyncTime };
// Step 4: Update last sync time and clear any previous errors
const lastSyncTime = this.safeToISOString(new Date());
const updatedSettings = {
...settings,
lastAutoSync: lastSyncTime,
lastAutoSyncError: '' // Clear any previous errors on successful sync
};
this.saveSettings(updatedSettings);
const duration = new Date().getTime() - startTime.getTime();
@@ -333,27 +508,51 @@ export class AutoSyncService {
} catch (error) {
console.error('Auto-sync execution failed:', error);
// Check if it's a rate limit error
const isRateLimitError = error instanceof Error && error.name === 'RateLimitError';
const errorMessage = error instanceof Error ? error.message : String(error);
// Send error notification if enabled
const settings = this.loadSettings();
if (settings.notificationEnabled && settings.appriseUrls?.length > 0) {
if (settings.notificationEnabled && settings.appriseUrls && settings.appriseUrls.length > 0) {
try {
const notificationTitle = isRateLimitError ? 'Auto-Sync Rate Limited' : 'Auto-Sync Failed';
const notificationMessage = isRateLimitError
? `GitHub API rate limit exceeded. Please set a GITHUB_TOKEN in your .env file for higher rate limits. Error: ${errorMessage}`
: `Auto-sync failed with error: ${errorMessage}`;
await appriseService.sendNotification(
'Auto-Sync Failed',
`Auto-sync failed with error: ${error instanceof Error ? error.message : String(error)}`,
settings.appriseUrls
notificationTitle,
notificationMessage,
settings.appriseUrls || []
);
} catch (notifError) {
console.error('Failed to send error notification:', notifError);
}
}
// Store the error in settings for UI display
const errorSettings = this.loadSettings();
const errorToStore = isRateLimitError
? `GitHub API rate limit exceeded. Please set a GITHUB_TOKEN in your .env file for higher rate limits.`
: errorMessage;
const updatedErrorSettings = {
...errorSettings,
lastAutoSyncError: errorToStore,
lastAutoSyncErrorTime: this.safeToISOString(new Date())
};
this.saveSettings(updatedErrorSettings);
return {
success: false,
message: error instanceof Error ? error.message : String(error),
error: error instanceof Error ? error.message : String(error)
message: errorToStore,
error: errorMessage,
isRateLimitError
};
} finally {
this.isRunning = false;
globalAutoSyncLock = false; // Release global lock
}
}
@@ -384,6 +583,12 @@ export class AutoSyncService {
const grouped = new Map();
scripts.forEach(script => {
// Validate script object
if (!script || !script.name) {
console.warn('Invalid script object in groupScriptsByCategory, skipping:', script);
return;
}
const scriptCategories = script.categories || [0]; // Default to Miscellaneous (id: 0)
scriptCategories.forEach((/** @type {number} */ catId) => {
const categoryName = categoryMap.get(catId) || 'Miscellaneous';
@@ -415,7 +620,18 @@ export class AutoSyncService {
// @ts-ignore - Dynamic property access
if (results.jsonSync) {
// @ts-ignore - Dynamic property access
body += `JSON Files: ${results.jsonSync.syncedCount} synced, ${results.jsonSync.skippedCount} up-to-date\n`;
const syncedCount = results.jsonSync.count || 0;
// @ts-ignore - Dynamic property access
const syncedFiles = results.jsonSync.syncedFiles || [];
// Calculate up-to-date count (total files - synced files)
// We can't easily get total file count from the sync result, so just show synced count
if (syncedCount > 0) {
body += `JSON Files: ${syncedCount} synced\n`;
} else {
body += `JSON Files: All up-to-date\n`;
}
// @ts-ignore - Dynamic property access
if (results.jsonSync.errors?.length > 0) {
// @ts-ignore - Dynamic property access

View File

@@ -29,14 +29,24 @@ export class GitHubService {
}
private async fetchFromGitHub<T>(endpoint: string): Promise<T> {
const response = await fetch(`${this.baseUrl}${endpoint}`, {
headers: {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'PVEScripts-Local/1.0',
},
});
const headers: HeadersInit = {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'PVEScripts-Local/1.0',
};
// Add GitHub token authentication if available
if (env.GITHUB_TOKEN) {
headers.Authorization = `token ${env.GITHUB_TOKEN}`;
}
const response = await fetch(`${this.baseUrl}${endpoint}`, { headers });
if (!response.ok) {
if (response.status === 403) {
const error = new Error(`GitHub API rate limit exceeded. Consider setting GITHUB_TOKEN for higher limits. Status: ${response.status} ${response.statusText}`);
error.name = 'RateLimitError';
throw error;
}
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`);
}

View File

@@ -1,276 +1,6 @@
import { writeFile, mkdir } from 'fs/promises';
import { readFileSync, readdirSync, statSync, utimesSync } from 'fs';
import { join } from 'path';
import { Buffer } from 'buffer';
// JavaScript wrapper for githubJsonService.ts
// This allows the JavaScript autoSyncService.js to import the TypeScript service
export class GitHubJsonService {
constructor() {
this.baseUrl = null;
this.repoUrl = null;
this.branch = null;
this.jsonFolder = null;
this.localJsonDirectory = null;
this.scriptCache = new Map();
}
import { githubJsonService } from './githubJsonService.ts';
initializeConfig() {
if (this.repoUrl === null) {
// Get environment variables
this.repoUrl = process.env.REPO_URL || "";
this.branch = process.env.REPO_BRANCH || "main";
this.jsonFolder = process.env.JSON_FOLDER || "scripts";
this.localJsonDirectory = join(process.cwd(), 'scripts', 'json');
// Only validate GitHub URL if it's provided
if (this.repoUrl) {
// Extract owner and repo from the URL
const urlMatch = /github\.com\/([^\/]+)\/([^\/]+)/.exec(this.repoUrl);
if (!urlMatch) {
throw new Error(`Invalid GitHub repository URL: ${this.repoUrl}`);
}
const [, owner, repo] = urlMatch;
this.baseUrl = `https://api.github.com/repos/${owner}/${repo}`;
} else {
// Set a dummy base URL if no REPO_URL is provided
this.baseUrl = "";
}
}
}
async fetchFromGitHub(endpoint) {
this.initializeConfig();
const response = await fetch(`${this.baseUrl}${endpoint}`, {
headers: {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'PVEScripts-Local/1.0',
...(process.env.GITHUB_TOKEN && { 'Authorization': `token ${process.env.GITHUB_TOKEN}` })
},
});
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`);
}
return response.json();
}
async syncJsonFiles() {
try {
this.initializeConfig();
if (!this.baseUrl) {
return {
success: false,
message: 'No GitHub repository configured'
};
}
console.log('Starting fast incremental JSON sync...');
// Ensure local directory exists
await mkdir(this.localJsonDirectory, { recursive: true });
// Step 1: Get file list from GitHub (single API call)
console.log('Fetching file list from GitHub...');
const files = await this.fetchFromGitHub(`/contents/${this.jsonFolder}?ref=${this.branch}`);
if (!Array.isArray(files)) {
throw new Error('Invalid response from GitHub API');
}
const jsonFiles = files.filter(file => file.name.endsWith('.json'));
console.log(`Found ${jsonFiles.length} JSON files in repository`);
// Step 2: Get local file list (fast local operation)
const localFiles = new Map();
try {
console.log(`Looking for local files in: ${this.localJsonDirectory}`);
const localFileList = readdirSync(this.localJsonDirectory);
console.log(`Found ${localFileList.length} files in local directory`);
for (const fileName of localFileList) {
if (fileName.endsWith('.json')) {
const filePath = join(this.localJsonDirectory, fileName);
const stats = statSync(filePath);
localFiles.set(fileName, {
mtime: stats.mtime,
size: stats.size
});
}
}
} catch (error) {
console.log('Error reading local directory:', error.message);
console.log('Directory path:', this.localJsonDirectory);
console.log('No local files found, will download all');
}
console.log(`Found ${localFiles.size} local JSON files`);
// Step 3: Compare and identify files that need syncing
const filesToSync = [];
let skippedCount = 0;
for (const file of jsonFiles) {
const localFile = localFiles.get(file.name);
if (!localFile) {
// File doesn't exist locally
filesToSync.push(file);
console.log(`Missing: ${file.name}`);
} else {
// Compare modification times and sizes
const localMtime = new Date(localFile.mtime);
const remoteMtime = new Date(file.updated_at);
const localSize = localFile.size;
const remoteSize = file.size;
// Sync if remote is newer OR sizes are different (content changed)
if (localMtime < remoteMtime || localSize !== remoteSize) {
filesToSync.push(file);
console.log(`Changed: ${file.name} (${localMtime.toISOString()} -> ${remoteMtime.toISOString()})`);
} else {
skippedCount++;
console.log(`Up-to-date: ${file.name}`);
}
}
}
console.log(`Files to sync: ${filesToSync.length}, Up-to-date: ${skippedCount}`);
// Step 4: Download only the files that need syncing
let syncedCount = 0;
const errors = [];
const syncedFiles = [];
// Process files in batches to avoid overwhelming the API
const batchSize = 10;
for (let i = 0; i < filesToSync.length; i += batchSize) {
const batch = filesToSync.slice(i, i + batchSize);
// Process batch in parallel
const promises = batch.map(async (file) => {
try {
const content = await this.fetchFromGitHub(`/contents/${file.path}?ref=${this.branch}`);
if (content.content) {
// Decode base64 content
const fileContent = Buffer.from(content.content, 'base64').toString('utf-8');
// Write to local file
const localPath = join(this.localJsonDirectory, file.name);
await writeFile(localPath, fileContent, 'utf-8');
// Update file modification time to match remote
const remoteMtime = new Date(file.updated_at);
utimesSync(localPath, remoteMtime, remoteMtime);
syncedCount++;
syncedFiles.push(file.name);
console.log(`Synced: ${file.name}`);
}
} catch (error) {
console.error(`Failed to sync ${file.name}:`, error.message);
errors.push(`${file.name}: ${error.message}`);
}
});
await Promise.all(promises);
// Small delay between batches to be nice to the API
if (i + batchSize < filesToSync.length) {
await new Promise(resolve => setTimeout(resolve, 100));
}
}
console.log(`JSON sync completed. Synced ${syncedCount} files, skipped ${skippedCount} files.`);
return {
success: true,
message: `Successfully synced ${syncedCount} JSON files (${skippedCount} up-to-date)`,
syncedCount,
skippedCount,
syncedFiles,
errors
};
} catch (error) {
console.error('JSON sync failed:', error);
return {
success: false,
message: error.message,
error: error.message
};
}
}
async getAllScripts() {
try {
this.initializeConfig();
if (!this.localJsonDirectory) {
return [];
}
const scripts = [];
// Read all JSON files from local directory
const files = readdirSync(this.localJsonDirectory);
const jsonFiles = files.filter(file => file.endsWith('.json'));
for (const file of jsonFiles) {
try {
const filePath = join(this.localJsonDirectory, file);
const content = readFileSync(filePath, 'utf-8');
const script = JSON.parse(content);
if (script && typeof script === 'object') {
scripts.push(script);
}
} catch (error) {
console.error(`Failed to parse ${file}:`, error.message);
}
}
return scripts;
} catch (error) {
console.error('Failed to get all scripts:', error);
return [];
}
}
/**
* Get scripts only for specific JSON files that were synced
*/
async getScriptsForFiles(syncedFiles) {
try {
this.initializeConfig();
if (!this.localJsonDirectory || !syncedFiles || syncedFiles.length === 0) {
return [];
}
const scripts = [];
for (const fileName of syncedFiles) {
try {
const filePath = join(this.localJsonDirectory, fileName);
const content = readFileSync(filePath, 'utf-8');
const script = JSON.parse(content);
if (script && typeof script === 'object') {
scripts.push(script);
}
} catch (error) {
console.error(`Failed to parse ${fileName}:`, error.message);
}
}
return scripts;
} catch (error) {
console.error('Failed to get scripts for synced files:', error);
return [];
}
}
}
export const githubJsonService = new GitHubJsonService();
export { githubJsonService };

View File

@@ -1,7 +1,7 @@
import { writeFile, mkdir } from 'fs/promises';
import { writeFile, mkdir, readdir } from 'fs/promises';
import { join } from 'path';
import { env } from '~/env.js';
import type { Script, ScriptCard, GitHubFile } from '~/types/script';
import { env } from '../../env.js';
import type { Script, ScriptCard, GitHubFile } from '../../types/script';
export class GitHubJsonService {
private baseUrl: string | null = null;
@@ -41,14 +41,25 @@ export class GitHubJsonService {
private async fetchFromGitHub<T>(endpoint: string): Promise<T> {
this.initializeConfig();
const response = await fetch(`${this.baseUrl!}${endpoint}`, {
headers: {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'PVEScripts-Local/1.0',
},
});
const headers: HeadersInit = {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'PVEScripts-Local/1.0',
};
// Add GitHub token authentication if available
if (env.GITHUB_TOKEN) {
headers.Authorization = `token ${env.GITHUB_TOKEN}`;
}
const response = await fetch(`${this.baseUrl!}${endpoint}`, { headers });
if (!response.ok) {
if (response.status === 403) {
const error = new Error(`GitHub API rate limit exceeded. Consider setting GITHUB_TOKEN for higher limits. Status: ${response.status} ${response.statusText}`);
error.name = 'RateLimitError';
throw error;
}
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`);
}
@@ -59,8 +70,22 @@ export class GitHubJsonService {
this.initializeConfig();
const rawUrl = `https://raw.githubusercontent.com/${this.extractRepoPath()}/${this.branch!}/${filePath}`;
const response = await fetch(rawUrl);
const headers: HeadersInit = {
'User-Agent': 'PVEScripts-Local/1.0',
};
// Add GitHub token authentication if available (for raw files, use token in URL or header)
if (env.GITHUB_TOKEN) {
headers.Authorization = `token ${env.GITHUB_TOKEN}`;
}
const response = await fetch(rawUrl, { headers });
if (!response.ok) {
if (response.status === 403) {
const error = new Error(`GitHub rate limit exceeded while downloading ${filePath}. Consider setting GITHUB_TOKEN for higher limits. Status: ${response.status} ${response.statusText}`);
error.name = 'RateLimitError';
throw error;
}
throw new Error(`Failed to download ${filePath}: ${response.status} ${response.statusText}`);
}
@@ -185,48 +210,90 @@ export class GitHubJsonService {
}
}
async syncJsonFiles(): Promise<{ success: boolean; message: string; count: number }> {
async syncJsonFiles(): Promise<{ success: boolean; message: string; count: number; syncedFiles: string[] }> {
try {
// Get all scripts from GitHub (1 API call + raw downloads)
const scripts = await this.getAllScripts();
console.log('Starting fast incremental JSON sync...');
// Save scripts to local directory
await this.saveScriptsLocally(scripts);
// Get file list from GitHub
console.log('Fetching file list from GitHub...');
const githubFiles = await this.getJsonFiles();
console.log(`Found ${githubFiles.length} JSON files in repository`);
// Get local files
const localFiles = await this.getLocalJsonFiles();
console.log(`Found ${localFiles.length} files in local directory`);
console.log(`Found ${localFiles.filter(f => f.endsWith('.json')).length} local JSON files`);
// Compare and find files that need syncing
const filesToSync = this.findFilesToSync(githubFiles, localFiles);
console.log(`Found ${filesToSync.length} files that need syncing`);
if (filesToSync.length === 0) {
return {
success: true,
message: 'All JSON files are up to date',
count: 0,
syncedFiles: []
};
}
// Download and save only the files that need syncing
const syncedFiles = await this.syncSpecificFiles(filesToSync);
return {
success: true,
message: `Successfully synced ${scripts.length} scripts from GitHub using 1 API call + raw downloads`,
count: scripts.length
message: `Successfully synced ${syncedFiles.length} JSON files from GitHub`,
count: syncedFiles.length,
syncedFiles
};
} catch (error) {
console.error('Error syncing JSON files:', error);
console.error('JSON sync failed:', error);
return {
success: false,
message: `Failed to sync JSON files: ${error instanceof Error ? error.message : 'Unknown error'}`,
count: 0
count: 0,
syncedFiles: []
};
}
}
private async saveScriptsLocally(scripts: Script[]): Promise<void> {
private async getLocalJsonFiles(): Promise<string[]> {
this.initializeConfig();
try {
// Ensure the directory exists
await mkdir(this.localJsonDirectory!, { recursive: true });
// Save each script as a JSON file
for (const script of scripts) {
const filename = `${script.slug}.json`;
const filePath = join(this.localJsonDirectory!, filename);
const content = JSON.stringify(script, null, 2);
await writeFile(filePath, content, 'utf-8');
}
} catch (error) {
console.error('Error saving scripts locally:', error);
throw new Error('Failed to save scripts locally');
const files = await readdir(this.localJsonDirectory!);
return files.filter(f => f.endsWith('.json'));
} catch {
return [];
}
}
private findFilesToSync(githubFiles: GitHubFile[], localFiles: string[]): GitHubFile[] {
const localFileSet = new Set(localFiles);
// Return only files that don't exist locally
return githubFiles.filter(ghFile => !localFileSet.has(ghFile.name));
}
private async syncSpecificFiles(filesToSync: GitHubFile[]): Promise<string[]> {
this.initializeConfig();
const syncedFiles: string[] = [];
await mkdir(this.localJsonDirectory!, { recursive: true });
for (const file of filesToSync) {
try {
const script = await this.downloadJsonFile(file.path);
const filename = `${script.slug}.json`;
const filePath = join(this.localJsonDirectory!, filename);
await writeFile(filePath, JSON.stringify(script, null, 2), 'utf-8');
syncedFiles.push(filename);
} catch (error) {
console.error(`Failed to sync ${file.name}:`, error);
}
}
return syncedFiles;
}
}
// Singleton instance

View File

@@ -0,0 +1,6 @@
// JavaScript wrapper for localScripts.ts
// This allows the JavaScript autoSyncService.js to import the TypeScript service
import { localScriptsService } from './localScripts.ts';
export { localScriptsService };

View File

@@ -1,14 +1,18 @@
import { writeFile, readFile, mkdir } from 'fs/promises';
// Real JavaScript implementation for script downloading
import { join } from 'path';
import { writeFile, mkdir, access } from 'fs/promises';
export class ScriptDownloaderService {
constructor() {
this.scriptsDirectory = null;
this.repoUrl = null;
}
initializeConfig() {
if (this.scriptsDirectory === null) {
this.scriptsDirectory = join(process.cwd(), 'scripts');
// Get REPO_URL from environment or use default
this.repoUrl = process.env.REPO_URL || 'https://github.com/community-scripts/ProxmoxVE';
}
}
@@ -23,19 +27,35 @@ export class ScriptDownloaderService {
}
async downloadFileFromGitHub(filePath) {
// This is a simplified version - in a real implementation,
// you would fetch the file content from GitHub
// For now, we'll return a placeholder
return `#!/bin/bash
# Downloaded script: ${filePath}
# This is a placeholder - implement actual GitHub file download
echo "Script downloaded: ${filePath}"
`;
this.initializeConfig();
if (!this.repoUrl) {
throw new Error('REPO_URL environment variable is not set');
}
// Extract repo path from URL
const match = /github\.com\/([^\/]+)\/([^\/]+)/.exec(this.repoUrl);
if (!match) {
throw new Error('Invalid GitHub repository URL');
}
const [, owner, repo] = match;
const url = `https://raw.githubusercontent.com/${owner}/${repo}/main/${filePath}`;
console.log(`Downloading from GitHub: ${url}`);
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to download ${filePath}: ${response.status} ${response.statusText}`);
}
return response.text();
}
modifyScriptContent(content) {
// Modify script content for CT scripts if needed
return content;
// Replace the build.func source line
const oldPattern = /source <\(curl -fsSL https:\/\/raw\.githubusercontent\.com\/community-scripts\/ProxmoxVE\/main\/misc\/build\.func\)/g;
const newPattern = 'SCRIPT_DIR="$(dirname "$0")" \nsource "$SCRIPT_DIR/../core/build.func"';
return content.replace(oldPattern, newPattern);
}
async loadScript(script) {
@@ -57,6 +77,7 @@ echo "Script downloaded: ${filePath}"
if (fileName) {
// Download from GitHub
console.log(`Downloading script file: ${scriptPath}`);
const content = await this.downloadFileFromGitHub(scriptPath);
// Determine target directory based on script path
@@ -111,6 +132,7 @@ echo "Script downloaded: ${filePath}"
}
files.push(`${finalTargetDir}/${fileName}`);
console.log(`Successfully downloaded: ${finalTargetDir}/${fileName}`);
}
}
}
@@ -121,12 +143,15 @@ echo "Script downloaded: ${filePath}"
if (hasCtScript) {
const installScriptName = `${script.slug}-install.sh`;
try {
console.log(`Downloading install script: install/${installScriptName}`);
const installContent = await this.downloadFileFromGitHub(`install/${installScriptName}`);
const localInstallPath = join(this.scriptsDirectory, 'install', installScriptName);
await writeFile(localInstallPath, installContent, 'utf-8');
files.push(`install/${installScriptName}`);
} catch {
console.log(`Successfully downloaded: install/${installScriptName}`);
} catch (error) {
// Install script might not exist, that's okay
console.log(`Install script not found: install/${installScriptName}`);
}
}
@@ -145,78 +170,6 @@ echo "Script downloaded: ${filePath}"
}
}
/**
* Auto-download new scripts that haven't been downloaded yet
*/
async autoDownloadNewScripts(allScripts) {
this.initializeConfig();
const downloaded = [];
const errors = [];
for (const script of allScripts) {
try {
// Check if script is already downloaded
const isDownloaded = await this.isScriptDownloaded(script);
if (!isDownloaded) {
const result = await this.loadScript(script);
if (result.success) {
downloaded.push(script); // Return full script object instead of just name
console.log(`Auto-downloaded new script: ${script.name || script.slug}`);
} else {
errors.push(`${script.name || script.slug}: ${result.message}`);
}
}
} catch (error) {
const errorMsg = `${script.name || script.slug}: ${error instanceof Error ? error.message : 'Unknown error'}`;
errors.push(errorMsg);
console.error(`Failed to auto-download script ${script.slug}:`, error);
}
}
return { downloaded, errors };
}
/**
* Auto-update existing scripts to newer versions
*/
async autoUpdateExistingScripts(allScripts) {
this.initializeConfig();
const updated = [];
const errors = [];
for (const script of allScripts) {
try {
// Check if script is downloaded
const isDownloaded = await this.isScriptDownloaded(script);
if (isDownloaded) {
// Check if update is needed by comparing content
const needsUpdate = await this.scriptNeedsUpdate(script);
if (needsUpdate) {
const result = await this.loadScript(script);
if (result.success) {
updated.push(script); // Return full script object instead of just name
console.log(`Auto-updated script: ${script.name || script.slug}`);
} else {
errors.push(`${script.name || script.slug}: ${result.message}`);
}
}
}
} catch (error) {
const errorMsg = `${script.name || script.slug}: ${error instanceof Error ? error.message : 'Unknown error'}`;
errors.push(errorMsg);
console.error(`Failed to auto-update script ${script.slug}:`, error);
}
}
return { updated, errors };
}
/**
* Check if a script is already downloaded
*/
async isScriptDownloaded(script) {
if (!script.install_methods?.length) return false;
@@ -261,7 +214,7 @@ echo "Script downloaded: ${filePath}"
}
try {
await readFile(filePath, 'utf8');
await import('fs/promises').then(fs => fs.readFile(filePath, 'utf8'));
// File exists, continue checking other methods
} catch {
// File doesn't exist, script is not fully downloaded
@@ -275,71 +228,69 @@ echo "Script downloaded: ${filePath}"
return true;
}
/**
* Check if a script needs updating by comparing local and remote content
*/
async scriptNeedsUpdate(script) {
if (!script.install_methods?.length) return false;
async checkScriptExists(script) {
this.initializeConfig();
const files = [];
let ctExists = false;
let installExists = false;
for (const method of script.install_methods) {
if (method.script) {
const scriptPath = method.script;
const fileName = scriptPath.split('/').pop();
if (fileName) {
// Determine target directory based on script path
let targetDir;
let finalTargetDir;
let filePath;
if (scriptPath.startsWith('ct/')) {
targetDir = 'ct';
finalTargetDir = targetDir;
filePath = join(this.scriptsDirectory, targetDir, fileName);
} else if (scriptPath.startsWith('tools/')) {
targetDir = 'tools';
const subPath = scriptPath.replace('tools/', '');
const subDir = subPath.includes('/') ? subPath.substring(0, subPath.lastIndexOf('/')) : '';
finalTargetDir = subDir ? join(targetDir, subDir) : targetDir;
filePath = join(this.scriptsDirectory, finalTargetDir, fileName);
} else if (scriptPath.startsWith('vm/')) {
targetDir = 'vm';
const subPath = scriptPath.replace('vm/', '');
const subDir = subPath.includes('/') ? subPath.substring(0, subPath.lastIndexOf('/')) : '';
finalTargetDir = subDir ? join(targetDir, subDir) : targetDir;
filePath = join(this.scriptsDirectory, finalTargetDir, fileName);
} else if (scriptPath.startsWith('vw/')) {
targetDir = 'vw';
const subPath = scriptPath.replace('vw/', '');
const subDir = subPath.includes('/') ? subPath.substring(0, subPath.lastIndexOf('/')) : '';
finalTargetDir = subDir ? join(targetDir, subDir) : targetDir;
filePath = join(this.scriptsDirectory, finalTargetDir, fileName);
} else {
targetDir = 'ct';
finalTargetDir = targetDir;
filePath = join(this.scriptsDirectory, targetDir, fileName);
}
try {
// Read local content
const localContent = await readFile(filePath, 'utf8');
try {
// Check scripts based on their install methods
if (script.install_methods?.length) {
for (const method of script.install_methods) {
if (method.script) {
const scriptPath = method.script;
const fileName = scriptPath.split('/').pop();
// Download remote content
const remoteContent = await this.downloadFileFromGitHub(scriptPath);
// Compare content (simple string comparison for now)
// In a more sophisticated implementation, you might want to compare
// file modification times or use content hashing
return localContent !== remoteContent;
} catch {
// If we can't read local or download remote, assume update needed
return true;
if (fileName) {
let targetDir;
if (scriptPath.startsWith('ct/')) {
targetDir = 'ct';
} else if (scriptPath.startsWith('tools/')) {
targetDir = 'tools';
} else if (scriptPath.startsWith('vm/')) {
targetDir = 'vm';
} else {
targetDir = 'ct'; // Default fallback
}
const filePath = join(this.scriptsDirectory, targetDir, fileName);
try {
await access(filePath);
files.push(`${targetDir}/${fileName}`);
if (scriptPath.startsWith('ct/')) {
ctExists = true;
}
} catch {
// File doesn't exist
}
}
}
}
}
}
return false;
// Check for install script for CT scripts
const hasCtScript = script.install_methods?.some(method => method.script?.startsWith('ct/'));
if (hasCtScript) {
const installScriptName = `${script.slug}-install.sh`;
const installPath = join(this.scriptsDirectory, 'install', installScriptName);
try {
await access(installPath);
files.push(`install/${installScriptName}`);
installExists = true;
} catch {
// Install script doesn't exist
}
}
return { ctExists, installExists, files };
} catch (error) {
console.error('Error checking script existence:', error);
return { ctExists: false, installExists: false, files: [] };
}
}
}