File size: 4,263 Bytes
613c9ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { app } from "../../scripts/app.js";
import { api } from "../../scripts/api.js";

export async function sleep(ms) {
	return new Promise(resolve => setTimeout(resolve, ms));
}

export function rebootAPI() {
	if (confirm("Are you sure you'd like to reboot the server?")) {
		try {
			api.fetchApi("/manager/reboot");
		}
		catch(exception) {

		}
		return true;
	}

	return false;
}

export async function install_checked_custom_node(grid_rows, target_i, caller, mode) {
	if(caller) {
		let failed = '';

		caller.disableButtons();

		for(let i in grid_rows) {
			if(!grid_rows[i].checkbox.checked && i != target_i)
				continue;

			var target;

			if(grid_rows[i].data.custom_node) {
				target = grid_rows[i].data.custom_node;
			}
			else {
				target = grid_rows[i].data;
			}

			caller.startInstall(target);

			try {
				const response = await api.fetchApi(`/customnode/${mode}`, {
														method: 'POST',
														headers: { 'Content-Type': 'application/json' },
														body: JSON.stringify(target)
													});

				if(response.status == 400) {
					show_message(`${mode} failed: ${target.title}`);
					continue;
				}

				const status = await response.json();
				app.ui.dialog.close();
				target.installed = 'True';
				continue;
			}
			catch(exception) {
				failed += `<BR> ${target.title}`;
			}
		}

		if(failed != '') {
			show_message(`${mode} failed: ${failed}`);
		}

		await caller.invalidateControl();
		caller.updateMessage("<BR>To apply the installed/updated/disabled/enabled custom node, please <button id='cm-reboot-button' class='cm-small-button'>RESTART</button> ComfyUI. And refresh browser.", 'cm-reboot-button');
	}
};

export var manager_instance = null;

export function setManagerInstance(obj) {
	manager_instance = obj;
}

function isValidURL(url) {
	if(url.includes('&'))
		return false;

	const pattern = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/;
	return pattern.test(url);
}

export async function install_pip(packages) {
	if(packages.includes('&'))
		app.ui.dialog.show(`Invalid PIP package enumeration: '${packages}'`);

	const res = await api.fetchApi(`/customnode/install/pip?packages=${packages}`);

	if(res.status == 200) {
		app.ui.dialog.show(`PIP package installation is processed.<br>To apply the pip packages, please click the <button id='cm-reboot-button'><font size='3px'>RESTART</font></button> button in ComfyUI.`);

		const rebootButton = document.getElementById('cm-reboot-button');
		const self = this;

		rebootButton.addEventListener("click", rebootAPI);

		app.ui.dialog.element.style.zIndex = 10010;
	}
	else {
		app.ui.dialog.show(`Failed to install '${packages}'<BR>See terminal log.`);
		app.ui.dialog.element.style.zIndex = 10010;
	}
}

export async function install_via_git_url(url, manager_dialog) {
	if(!url) {
		return;
	}

	if(!isValidURL(url)) {
		app.ui.dialog.show(`Invalid Git url '${url}'`);
		app.ui.dialog.element.style.zIndex = 10010;
		return;
	}

	app.ui.dialog.show(`Wait...<BR><BR>Installing '${url}'`);
	app.ui.dialog.element.style.zIndex = 10010;

	const res = await api.fetchApi(`/customnode/install/git_url?url=${url}`);

	if(res.status == 200) {
		app.ui.dialog.show(`'${url}' is installed<BR>To apply the installed custom node, please <button id='cm-reboot-button'><font size='3px'>RESTART</font></button> ComfyUI.`);

		const rebootButton = document.getElementById('cm-reboot-button');
		const self = this;

		rebootButton.addEventListener("click",
			function() {
				if(rebootAPI()) {
					manager_dialog.close();
				}
			});

		app.ui.dialog.element.style.zIndex = 10010;
	}
	else {
		app.ui.dialog.show(`Failed to install '${url}'<BR>See terminal log.`);
		app.ui.dialog.element.style.zIndex = 10010;
	}
}

export async function free_models() {
	let res = await api.fetchApi(`/free`, {
						method: 'POST',
						headers: { 'Content-Type': 'application/json' },
						body: '{}'
					});

	if(res.status == 200) {
		app.ui.dialog.show('Models have been unloaded.')
	}
	else {
		app.ui.dialog.show('Unloading of models failed.<BR><BR>Installed ComfyUI may be an outdated version.')
	}
	app.ui.dialog.element.style.zIndex = 10010;
}

export function show_message(msg) {
	app.ui.dialog.show(msg);
	app.ui.dialog.element.style.zIndex = 10010;
}