|
| 1 | +import { |
| 2 | + groupSoftwareByCategory, |
| 3 | + getConfigObjects, |
| 4 | + formatWingetCommand, |
| 5 | + getDateTime, |
| 6 | +} from './script-utils'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Generate Windows Batch (.bat) script |
| 10 | + * @param {Array} selectedSoftwareIds - Array of selected software IDs |
| 11 | + * @param {Array} selectedConfigIds - Array of selected configuration IDs |
| 12 | + * @returns {string} - Generated batch script |
| 13 | + */ |
| 14 | +export const generateBatchScript = (selectedSoftwareIds, selectedConfigIds) => { |
| 15 | + const header = generateBatchHeader(); |
| 16 | + const softwareSection = generateBatchSoftwareSection(selectedSoftwareIds); |
| 17 | + const configSection = generateBatchConfigSection(selectedConfigIds); |
| 18 | + const footer = generateBatchFooter(); |
| 19 | + |
| 20 | + return header + softwareSection + configSection + footer; |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * Generate batch script header |
| 25 | + */ |
| 26 | +const generateBatchHeader = () => { |
| 27 | + return `@echo off |
| 28 | +chcp 65001 >nul |
| 29 | +echo ======================================== |
| 30 | +echo Windows Post-Install Script (.bat) |
| 31 | +echo Generated: ${getDateTime()} |
| 32 | +echo ======================================== |
| 33 | +echo. |
| 34 | +
|
| 35 | +:: Check for Administrator privileges |
| 36 | +net session >nul 2>&1 |
| 37 | +if %errorLevel% neq 0 ( |
| 38 | + echo ERROR: This script requires Administrator privileges. |
| 39 | + echo Please right-click and select "Run as administrator" |
| 40 | + echo. |
| 41 | + pause |
| 42 | + exit /b 1 |
| 43 | +) |
| 44 | +
|
| 45 | +echo Starting installation process... |
| 46 | +echo. |
| 47 | +`; |
| 48 | +}; |
| 49 | + |
| 50 | +/** |
| 51 | + * Generate software installation section |
| 52 | + */ |
| 53 | +const generateBatchSoftwareSection = (selectedIds) => { |
| 54 | + if (!selectedIds || selectedIds.length === 0) { |
| 55 | + return `:: No software selected for installation\necho No software selected.\necho.\n\n`; |
| 56 | + } |
| 57 | + |
| 58 | + let script = `::============================================ |
| 59 | +:: SOFTWARE INSTALLATION |
| 60 | +::============================================ |
| 61 | +echo. |
| 62 | +echo Installing selected software... |
| 63 | +echo This may take a while depending on your internet connection. |
| 64 | +echo. |
| 65 | +`; |
| 66 | + |
| 67 | + const grouped = groupSoftwareByCategory(selectedIds); |
| 68 | + |
| 69 | + Object.entries(grouped).forEach(([categoryName, softwares]) => { |
| 70 | + script += `\n:: ${categoryName}\n`; |
| 71 | + script += `echo Installing ${categoryName}...\n`; |
| 72 | + |
| 73 | + softwares.forEach((software) => { |
| 74 | + script += `echo - ${software.name}\n`; |
| 75 | + script += `${formatWingetCommand(software.wingetId)} 2>>errors.log\n`; |
| 76 | + }); |
| 77 | + |
| 78 | + script += `echo.\n`; |
| 79 | + }); |
| 80 | + |
| 81 | + return script + '\n'; |
| 82 | +}; |
| 83 | + |
| 84 | +/** |
| 85 | + * Generate configuration section |
| 86 | + */ |
| 87 | +const generateBatchConfigSection = (selectedIds) => { |
| 88 | + if (!selectedIds || selectedIds.length === 0) { |
| 89 | + return `:: No system configurations selected\necho No system configurations selected.\necho.\n\n`; |
| 90 | + } |
| 91 | + |
| 92 | + let script = `::============================================ |
| 93 | +:: SYSTEM CONFIGURATIONS |
| 94 | +::============================================ |
| 95 | +echo. |
| 96 | +echo Applying system configurations... |
| 97 | +echo. |
| 98 | +`; |
| 99 | + |
| 100 | + const configs = getConfigObjects(selectedIds); |
| 101 | + |
| 102 | + configs.forEach((config) => { |
| 103 | + script += `\n:: ${config.name}\n`; |
| 104 | + script += `echo Applying: ${config.name}\n`; |
| 105 | + |
| 106 | + // Add registry commands |
| 107 | + if (config.registryBat && config.registryBat.length > 0) { |
| 108 | + config.registryBat.forEach((cmd) => { |
| 109 | + script += `${cmd}\n`; |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + // Add regular commands |
| 114 | + if (config.commandBat && config.commandBat.length > 0) { |
| 115 | + config.commandBat.forEach((cmd) => { |
| 116 | + script += `${cmd}\n`; |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + script += `echo.\n`; |
| 121 | + }); |
| 122 | + |
| 123 | + return script + '\n'; |
| 124 | +}; |
| 125 | + |
| 126 | +/** |
| 127 | + * Generate batch script footer |
| 128 | + */ |
| 129 | +const generateBatchFooter = () => { |
| 130 | + return `::============================================ |
| 131 | +:: INSTALLATION COMPLETE |
| 132 | +::============================================ |
| 133 | +echo. |
| 134 | +echo ======================================== |
| 135 | +echo Installation Complete! |
| 136 | +echo ======================================== |
| 137 | +echo. |
| 138 | +echo Next steps: |
| 139 | +echo 1. Restart your computer if required |
| 140 | +echo 2. Log into your installed applications |
| 141 | +echo 3. Customize your settings as needed |
| 142 | +echo. |
| 143 | +echo If any installations failed, check errors.log |
| 144 | +echo. |
| 145 | +echo Generated by Windows Post-Install Generator |
| 146 | +echo https://github.com/kaic/win-post-install |
| 147 | +echo. |
| 148 | +pause |
| 149 | +`; |
| 150 | +}; |
0 commit comments