-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
executable file
·278 lines (234 loc) · 10.2 KB
/
script.js
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// URL of the JSON file
const jsonUrl = './pkgs/packages-info.json';
const imageUrl = './img/';
let packagesData; // Variable to store JSON data
let allSelected = false; // Variable to keep track of the toggle state
// Function to load and process the JSON
function loadPackages() {
fetch(jsonUrl)
.then(response => {
if (!response.ok) {
throw new Error('Failed to load packages data: ' + response.statusText);
}
return response.json();
})
.then(data => {
packagesData = data; // Assign JSON data to packagesData
generatePackages(packagesData); // Call function to generate packages
})
.catch(error => {
console.error('Error loading packages data: ', error);
});
}
// Function to generate the content of packages in the form
function generatePackages(packagesData) {
const packageContainer = document.getElementById('packageContainer');
packageContainer.innerHTML = ''; // Clear container before adding new content
// Define categories by column
const columnCategories = [
["File-Man", "File-Sharing", "Downloader"],
["Utility", "Science"],
["Virtualization"],
["Web"],
["Audio"],
["Book"],
["Office", "Finance"],
["Development"],
["Gaming"],
["Image"],
["Video"]
];
// Create columns
columnCategories.forEach(categoryList => {
const columnDiv = document.createElement('div');
columnDiv.classList.add('column');
packageContainer.appendChild(columnDiv);
categoryList.forEach(category => {
const categoryDiv = document.createElement('div');
categoryDiv.classList.add('category', category);
columnDiv.appendChild(categoryDiv);
const categoryHeading = document.createElement('h4');
categoryHeading.textContent = category;
categoryDiv.appendChild(categoryHeading);
const subcategories = {};
Object.entries(packagesData.packages).forEach(([pkgKey, pkgInfo]) => {
if (pkgInfo.category === category) {
const subcategory = pkgInfo.subcategory;
let subcategoryDiv = subcategories[subcategory];
if (!subcategoryDiv) {
subcategoryDiv = document.createElement('div');
subcategoryDiv.classList.add('subcategory', subcategory);
categoryDiv.appendChild(subcategoryDiv);
const subcategoryHeading = document.createElement('h5');
subcategoryHeading.textContent = subcategory;
subcategoryDiv.appendChild(subcategoryHeading);
subcategories[subcategory] = subcategoryDiv;
}
const packageLabel = document.createElement('label');
const packageCheckbox = document.createElement('input');
packageCheckbox.type = 'checkbox';
packageCheckbox.name = 'pkg';
packageCheckbox.value = pkgKey; // Use package key as value
packageCheckbox.id = pkgKey;
packageCheckbox.dataset.packageName = pkgInfo.name; // Store package name as data attribute
const packageImg = document.createElement('img');
packageImg.src = `${imageUrl}${pkgKey}.svg`;
packageImg.width = 30;
packageLabel.appendChild(packageCheckbox);
packageLabel.appendChild(packageImg);
packageLabel.appendChild(document.createTextNode(` ${pkgInfo.name}`));
subcategoryDiv.appendChild(packageLabel);
}
});
});
});
}
// Function to generate the installation command based on the selected distribution
async function generateCommand() {
console.log('Generate command button clicked');
try {
const selectedDistro = document.querySelector('input[name="distro"]:checked').value;
const selectedPackages = Array.from(document.querySelectorAll('input[name="pkg"]:checked'))
.map(checkbox => checkbox.value);
const installationCommands = [];
const aurPackages = [];
const nonInstallablePackages = [];
// Iterate through each selected package
selectedPackages.forEach(pkg => {
const pkgInfo = packagesData.packages[pkg];
if (pkgInfo.package_manager[selectedDistro]) {
installationCommands.push(pkgInfo.package_manager[selectedDistro]);
} else if (selectedDistro === 'arch_pacman' && pkgInfo.package_manager['arch_aur']) {
aurPackages.push(pkgInfo.package_manager['arch_aur']);
} else {
nonInstallablePackages.push(pkgInfo.name);
}
});
let commandPrefix;
switch (selectedDistro) {
case 'arch_pacman':
commandPrefix = 'sudo pacman -S';
break;
case 'debian_apt':
commandPrefix = 'sudo apt install';
break;
case 'fedora_rpm':
commandPrefix = 'sudo dnf install';
break;
case 'gentoo_emerge':
commandPrefix = 'sudo emerge';
break;
case 'nixos_nix-env':
commandPrefix = 'sudo nix-env -iA';
break;
case 'void_xbps':
commandPrefix = 'sudo xbps-install -S';
break;
case 'linux_flatpak':
commandPrefix = 'flatpak install';
break;
case 'freebsd_pkg':
commandPrefix = 'sudo pkg install';
break;
case 'macos_brew':
commandPrefix = 'brew install';
break;
case 'windows_winget':
commandPrefix = 'winget install';
break;
default:
throw new Error('Unsupported distribution');
}
const finalCommand = installationCommands.length
? `${commandPrefix} ${installationCommands.join(' ')}`
: '';
const aurCommand = aurPackages.length
? `yay -S ${aurPackages.join(' ')}`
: '';
const resultCommand = [finalCommand, aurCommand].filter(cmd => cmd).join(' && ');
if (!resultCommand) {
throw new Error('No installation commands generated. Please select at least one package.');
}
// Clear any existing content
const outputElement = document.getElementById("output");
outputElement.innerHTML = '';
// Create and append the installation command element
const installationCodeElement = document.createElement('code');
installationCodeElement.id = 'installation-command';
installationCodeElement.textContent = resultCommand;
outputElement.appendChild(installationCodeElement);
// Create and append the non-installable packages element if needed
if (nonInstallablePackages.length) {
const nonInstallablePackagesElement = document.createElement('code');
nonInstallablePackagesElement.id = 'packages-not-found';
nonInstallablePackagesElement.innerHTML = `<strong>Packages not found:</strong> ${nonInstallablePackages.join(', ')}`;
outputElement.appendChild(nonInstallablePackagesElement);
}
} catch (error) {
alert(`There was an error generating the installation command: ${error.message}`);
}
}
// Call the function to load JSON and generate packages on page load
document.addEventListener('DOMContentLoaded', loadPackages);
// Function to select or deselect all packages
function toggleSelectAllPackages() {
const checkboxes = document.querySelectorAll('input[type="checkbox"]');
checkboxes.forEach(cb => {
cb.checked = !allSelected;
});
// Toggle the state
allSelected = !allSelected;
}
// Function to export selected packages (mocking data)
function exportPackages() {
const categories = document.querySelectorAll('.category');
const selectedPackages = [];
categories.forEach(category => {
const subcategories = category.querySelectorAll('.subcategory');
subcategories.forEach(subcategory => {
const checkboxes = subcategory.querySelectorAll('input[type="checkbox"]:checked');
checkboxes.forEach(checkbox => {
selectedPackages.push(checkbox.value);
});
});
});
const jsonData = JSON.stringify(selectedPackages, null, 2);
const blob = new Blob([jsonData], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'toolbox-exported-packages.json';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
alert('Selected packages exported successfully!');
}
document.getElementById('fileInput').addEventListener('change', function (e) {
importPackages(e.target.files[0]);
});
async function importPackages(file) {
const reader = new FileReader();
reader.onload = async function (event) {
try {
const contents = event.target.result;
const importedPackages = JSON.parse(contents);
console.log(importedPackages);
// Simulate fetching data and processing checkboxes after a short delay
await new Promise(resolve => setTimeout(resolve, 500));
importedPackages.forEach(pkgId => {
const checkbox = document.getElementById(pkgId);
if (checkbox) {
checkbox.checked = true;
} else {
console.warn(`Checkbox with ID ${pkgId} not found.`);
}
});
alert('Packages imported successfully!');
} catch (error) {
console.error('Error parsing JSON file:', error);
alert('Error importing packages. Please check the JSON file format.');
}
};
reader.readAsText(file);
}