Skip to content

Commit 2a9921a

Browse files
Merge pull request #348 from community-scripts/fix/update_script
fix: Detect script changes from remote repository to allow Script updates
2 parents 50f657b + 5d5eba7 commit 2a9921a

File tree

3 files changed

+90
-37
lines changed

3 files changed

+90
-37
lines changed

package-lock.json

Lines changed: 0 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/_components/ScriptDetailModal.tsx

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ export function ScriptDetailModal({
6161
isLoading: comparisonLoading,
6262
} = api.scripts.compareScriptContent.useQuery(
6363
{ slug: script?.slug ?? "" },
64-
{ enabled: !!script && isOpen },
64+
{
65+
enabled: !!script && isOpen,
66+
refetchOnMount: true,
67+
staleTime: 0,
68+
},
6569
);
6670

6771
// Load script mutation
@@ -547,19 +551,60 @@ export function ScriptDetailModal({
547551
</div>
548552
{scriptFilesData?.success &&
549553
(scriptFilesData.ctExists ||
550-
scriptFilesData.installExists) &&
551-
comparisonData?.success &&
552-
!comparisonLoading && (
554+
scriptFilesData.installExists) && (
553555
<div className="flex items-center space-x-2">
554-
<div
555-
className={`h-2 w-2 rounded-full ${comparisonData.hasDifferences ? "bg-warning" : "bg-success"}`}
556-
></div>
557-
<span>
558-
Status:{" "}
559-
{comparisonData.hasDifferences
560-
? "Update available"
561-
: "Up to date"}
562-
</span>
556+
{comparisonData?.success ? (
557+
<>
558+
<div
559+
className={`h-2 w-2 rounded-full ${comparisonData.hasDifferences ? "bg-warning" : "bg-success"}`}
560+
></div>
561+
<span>
562+
Status:{" "}
563+
{comparisonData.hasDifferences
564+
? "Update available"
565+
: "Up to date"}
566+
</span>
567+
</>
568+
) : comparisonLoading ? (
569+
<>
570+
<div className="h-2 w-2 rounded-full bg-muted animate-pulse"></div>
571+
<span>Checking for updates...</span>
572+
</>
573+
) : comparisonData?.error ? (
574+
<>
575+
<div className="h-2 w-2 rounded-full bg-destructive"></div>
576+
<span className="text-destructive">Error: {comparisonData.error}</span>
577+
</>
578+
) : (
579+
<>
580+
<div className="h-2 w-2 rounded-full bg-muted"></div>
581+
<span>Status: Unknown</span>
582+
</>
583+
)}
584+
<button
585+
onClick={() => void refetchComparison()}
586+
disabled={comparisonLoading}
587+
className="ml-2 p-1.5 rounded-md hover:bg-accent transition-colors disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center"
588+
title="Refresh comparison"
589+
>
590+
{comparisonLoading ? (
591+
<div className="h-4 w-4 animate-spin rounded-full border-2 border-current border-t-transparent"></div>
592+
) : (
593+
<svg
594+
className="h-4 w-4 text-muted-foreground hover:text-foreground"
595+
fill="none"
596+
stroke="currentColor"
597+
viewBox="0 0 24 24"
598+
>
599+
<path
600+
strokeLinecap="round"
601+
strokeLinejoin="round"
602+
strokeWidth={2}
603+
d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
604+
/>
605+
</svg>
606+
)}
607+
</button>
563608
</div>
564609
)}
565610
</div>

src/server/services/scriptDownloader.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -519,13 +519,16 @@ export class ScriptDownloaderService {
519519
comparisonPromises.push(
520520
this.compareSingleFile(script, scriptPath, `${finalTargetDir}/${fileName}`)
521521
.then(result => {
522+
if (result.error) {
523+
console.error(`[Comparison] Error comparing ${result.filePath}: ${result.error}`);
524+
}
522525
if (result.hasDifferences) {
523526
hasDifferences = true;
524527
differences.push(result.filePath);
525528
}
526529
})
527-
.catch(() => {
528-
// Don't add to differences if there's an error reading files
530+
.catch((error) => {
531+
console.error(`[Comparison] Promise error for ${scriptPath}:`, error);
529532
})
530533
);
531534
}
@@ -541,13 +544,16 @@ export class ScriptDownloaderService {
541544
comparisonPromises.push(
542545
this.compareSingleFile(script, installScriptPath, installScriptPath)
543546
.then(result => {
547+
if (result.error) {
548+
console.error(`[Comparison] Error comparing ${result.filePath}: ${result.error}`);
549+
}
544550
if (result.hasDifferences) {
545551
hasDifferences = true;
546552
differences.push(result.filePath);
547553
}
548554
})
549-
.catch(() => {
550-
// Don't add to differences if there's an error reading files
555+
.catch((error) => {
556+
console.error(`[Comparison] Promise error for ${installScriptPath}:`, error);
551557
})
552558
);
553559
}
@@ -567,13 +573,16 @@ export class ScriptDownloaderService {
567573
comparisonPromises.push(
568574
this.compareSingleFile(script, alpineInstallScriptPath, alpineInstallScriptPath)
569575
.then(result => {
576+
if (result.error) {
577+
console.error(`[Comparison] Error comparing ${result.filePath}: ${result.error}`);
578+
}
570579
if (result.hasDifferences) {
571580
hasDifferences = true;
572581
differences.push(result.filePath);
573582
}
574583
})
575-
.catch(() => {
576-
// Don't add to differences if there's an error reading files
584+
.catch((error) => {
585+
console.error(`[Comparison] Promise error for ${alpineInstallScriptPath}:`, error);
577586
})
578587
);
579588
} catch {
@@ -584,10 +593,11 @@ export class ScriptDownloaderService {
584593
// Wait for all comparisons to complete
585594
await Promise.all(comparisonPromises);
586595

596+
console.log(`[Comparison] Completed comparison for ${script.slug}: hasDifferences=${hasDifferences}, differences=${differences.length}`);
587597
return { hasDifferences, differences };
588598
} catch (error) {
589-
console.error('Error comparing script content:', error);
590-
return { hasDifferences: false, differences: [] };
599+
console.error(`[Comparison] Error comparing script content for ${script.slug}:`, error);
600+
return { hasDifferences: false, differences: [], error: error.message };
591601
}
592602
}
593603

@@ -597,27 +607,39 @@ export class ScriptDownloaderService {
597607
const repoUrl = this.getRepoUrlForScript(script);
598608
const branch = process.env.REPO_BRANCH || 'main';
599609

610+
console.log(`[Comparison] Comparing ${filePath} from ${repoUrl} (branch: ${branch})`);
611+
600612
// Read local content
601613
const localContent = await readFile(localPath, 'utf-8');
614+
console.log(`[Comparison] Local file size: ${localContent.length} bytes`);
602615

603616
// Download remote content from the script's repository
604617
const remoteContent = await this.downloadFileFromGitHub(repoUrl, remotePath, branch);
618+
console.log(`[Comparison] Remote file size: ${remoteContent.length} bytes`);
605619

606620
// Apply modification only for CT scripts, not for other script types
607621
let modifiedRemoteContent;
608622
if (remotePath.startsWith('ct/')) {
609623
modifiedRemoteContent = this.modifyScriptContent(remoteContent);
624+
console.log(`[Comparison] Applied CT script modifications`);
610625
} else {
611626
modifiedRemoteContent = remoteContent; // Don't modify tools or vm scripts
612627
}
613628

614629
// Compare content
615630
const hasDifferences = localContent !== modifiedRemoteContent;
616631

632+
if (hasDifferences) {
633+
console.log(`[Comparison] Differences found in ${filePath}`);
634+
} else {
635+
console.log(`[Comparison] No differences in ${filePath}`);
636+
}
637+
617638
return { hasDifferences, filePath };
618639
} catch (error) {
619-
console.error(`Error comparing file ${filePath}:`, error);
620-
return { hasDifferences: false, filePath };
640+
console.error(`[Comparison] Error comparing file ${filePath}:`, error.message);
641+
// Return error information so it can be handled upstream
642+
return { hasDifferences: false, filePath, error: error.message };
621643
}
622644
}
623645

0 commit comments

Comments
 (0)