-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
103 lines (79 loc) · 3.34 KB
/
main.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
/* Copyright (c) 2024 Serhii I. Myshko
https://github.com/sergeiown/Winget_Upgrade/blob/main/LICENSE */
'use strict';
const fs = require('fs').promises;
const os = require('os');
const { exec } = require('child_process');
const { promisify } = require('util');
const {
setConsoleTitle,
waitForKeyPressAndExit,
logMessage,
checkAndTrimLogFile,
executeAndLog,
filterIgnoredPackages,
} = require('./utils');
const settings = require('./settings');
const execAsync = promisify(exec);
async function getWingetVersion() {
try {
const { stdout } = await execAsync(settings.wingetVersion);
const version = stdout.trim().replace(/^v/, '');
const [major, minor] = version.split('.').map(Number);
if (major < 1 || (major === 1 && minor < 4)) {
const versionMessage = `Error: Outdated winget version (${version}). Update required.${os.EOL}`;
await logMessage(versionMessage);
console.log(settings.outdatedVersionInstructions + os.EOL + `Press any key to exit...`);
await waitForKeyPressAndExit(1);
}
return version;
} catch (error) {
logMessage(`Error: Failed to retrieve winget version: ${error}${os.EOL}`);
return null;
}
}
async function tryToPerformUpgrade() {
console.clear();
const currentDate = settings.date;
logMessage(`${os.EOL}>> ${currentDate}${os.EOL}`);
await setConsoleTitle(settings.wingetUpgradeVersion);
try {
const { stdout } = await execAsync(settings.wingetPath);
const version = await getWingetVersion();
if (version) {
console.log(`Winget ${version} is installed on the system.${os.EOL}`);
} else {
throw new Error(`Winget is not installed.`);
}
const wingetLocation = stdout.trim();
const exportCommand = `${wingetLocation} ${settings.wingetArgs.export.join(' ')}`;
await executeAndLog(exportCommand, settings.logFilePath, async () => {
await filterIgnoredPackages(settings.ignoreFilePath, settings.listFilePath);
const importCommand = `${wingetLocation} ${settings.wingetArgs.import.join(' ')}`;
await executeAndLog(importCommand, settings.logFilePath, async () => {
await checkAndTrimLogFile(settings.logFilePath, settings.maxLogFileSize);
await fs.unlink(settings.listFilePath);
try {
await logMessage(settings.finalLogMessage);
console.log(settings.finalMessage);
await Promise.race([
waitForKeyPressAndExit(0),
new Promise((resolve) => setTimeout(resolve, 10000)),
]);
process.exit(0);
} catch (error) {
console.error(`An error occurred:`, error);
}
});
});
} catch (error) {
if (error.message.includes(`Winget is not installed.`)) {
await logMessage(`Error: winget is not installed on this system.${os.EOL}`);
console.log(settings.notInstalledSollutions + os.EOL + `Press any key to exit...`);
} else {
await logMessage(`Unexpected error occurred: ${error.message}${os.EOL}`);
}
await waitForKeyPressAndExit(1);
}
}
tryToPerformUpgrade();