@@ -2,32 +2,85 @@ import { applyPatch } from 'diff';
2
2
import fs from 'node:fs/promises' ;
3
3
4
4
/**
5
- * @param {string } filePath
6
- * @param {string } patchFilePath
5
+ * Patches files based on the {@link tasks} list.
6
+ *
7
+ * Each CLI argument is treated as a task name.
8
+ *
9
+ * Paths are relative to the project root.
10
+ * @example
11
+ * ```bash
12
+ * node scripts/patchComfyUI.js frontend requirements
13
+ * ```
7
14
*/
8
- async function patchFile ( filePath , patchFilePath ) {
15
+ const tasks = new Map ( [
16
+ [
17
+ 'frontend' ,
18
+ {
19
+ target : './assets/ComfyUI/app/frontend_management.py' ,
20
+ patch : './scripts/core-remove-frontend.patch' ,
21
+ } ,
22
+ ] ,
23
+ [
24
+ 'requirements' ,
25
+ {
26
+ target : './assets/ComfyUI/requirements.txt' ,
27
+ patch : './scripts/core-requirements.patch' ,
28
+ } ,
29
+ ] ,
30
+ ] ) ;
31
+
32
+ // Main execution
33
+ const args = process . argv . slice ( 2 ) ;
34
+
35
+ // Error if no args / any invalid args
36
+
37
+ if ( args . length === 0 ) {
38
+ console . error ( 'No arguments provided' ) ;
39
+ process . exit ( 15 ) ;
40
+ }
41
+
42
+ const invalidArgs = args . filter ( ( arg ) => ! tasks . has ( arg ) ) ;
43
+
44
+ if ( invalidArgs . length > 0 ) {
45
+ console . error ( `Invalid argument(s): ${ invalidArgs . map ( ( arg ) => `"${ arg } "` ) . join ( ', ' ) } ` ) ;
46
+ process . exit ( 255 ) ;
47
+ }
48
+
49
+ // Apply patches
50
+ const promises = args . map ( ( arg ) => patchFile ( tasks . get ( arg ) . target , tasks . get ( arg ) . patch ) ) ;
51
+ await Promise . all ( promises ) ;
52
+
53
+ //#region Functions
54
+
55
+ /**
56
+ * Applies a regular diff patch to a single file
57
+ * @param {string } targetPath Target file path
58
+ * @param {string } patchFilePath Patch file to apply to the target file
59
+ */
60
+ async function patchFile ( targetPath , patchFilePath ) {
9
61
try {
10
62
// Read the original file and patch file
11
63
const [ originalContent , patchContent ] = await Promise . all ( [
12
- fs . readFile ( filePath , 'utf8' ) ,
64
+ fs . readFile ( targetPath , 'utf8' ) ,
13
65
fs . readFile ( patchFilePath , 'utf8' ) ,
14
66
] ) ;
15
67
16
68
// Apply the patch
17
69
const patchedContent = applyPatch ( originalContent , patchContent ) ;
18
70
19
- // If patch was successfully applied (not false or null )
71
+ // If patch was successfully applied (not falsy )
20
72
if ( patchedContent ) {
21
73
// Write the result to the output file
22
- await fs . writeFile ( filePath , patchedContent , 'utf8' ) ;
74
+ await fs . writeFile ( targetPath , patchedContent , 'utf8' ) ;
23
75
console . log ( 'Patch applied successfully!' ) ;
24
76
} else {
25
- console . error ( 'Failed to apply patch - patch may be invalid or incompatible' ) ;
77
+ throw new Error (
78
+ `ComfyUI core patching returned falsy value (${ typeof patchedContent } ) - .patch file probably requires update`
79
+ ) ;
26
80
}
27
81
} catch ( error ) {
28
- console . error ( ' Error applying patch:' , error . message ) ;
82
+ throw new Error ( ` Error applying core patch: ${ error . message } ` , { cause : error } ) ;
29
83
}
30
84
}
31
85
32
- await patchFile ( './assets/ComfyUI/app/frontend_management.py' , './scripts/core-remove-frontend.patch' ) ;
33
- await patchFile ( './assets/ComfyUI/requirements.txt' , './scripts/core-requirements.patch' ) ;
86
+ //#endregion Functions
0 commit comments