-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
154 lines (129 loc) · 5.1 KB
/
utils.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
/* 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 { createWriteStream } = require('fs');
const settings = require('./settings');
const execAsync = promisify(exec);
async function setConsoleTitle(title) {
try {
await execAsync(`title ${title}`);
} catch (error) {
console.error(`Failed to set console title: ${error}`);
}
}
async function waitForKeyPressAndExit(exitCode) {
process.stdin.setRawMode(true);
process.stdin.resume();
await new Promise((resolve) => {
process.stdin.once('data', () => {
resolve();
});
});
process.exit(exitCode);
}
async function logMessage(message) {
await fs
.appendFile(settings.logFilePath, message)
.catch((err) => console.error(`Error writing to log file: ${err.message}`));
console.log(message);
}
async function executeAndLog(command, logFilePath, callback) {
try {
const childProcess = exec(command);
const logStream = createWriteStream(logFilePath, { flags: 'a' });
childProcess.stdout.on('data', (data) => {
const lines = data.toString().split(os.EOL);
lines.forEach((line) => {
const trimmedLine = line.trim();
if (
!/[░▒█]/.test(trimmedLine) &&
/[a-zA-Zа-яА-Я0-9]/.test(trimmedLine) &&
!trimmedLine.includes('Found an existing package already installed.') &&
!trimmedLine.includes('No available upgrade found.') &&
!trimmedLine.includes('No newer package versions are available from the configured sources.')
) {
logStream.write(trimmedLine + os.EOL);
}
});
});
childProcess.stderr.on('data', (data) => {
console.error(`Error: ${data}`);
logStream.end();
});
childProcess.stderr.pipe(logStream);
childProcess.stdout.pipe(process.stdout);
childProcess.stderr.pipe(process.stderr);
await new Promise((resolve) => childProcess.on('exit', resolve));
logStream.end();
callback(logFilePath);
} catch (error) {
console.error(`Execution failed: ${error}`);
}
}
async function filterIgnoredPackages(ignoreFilePath, listFilePath) {
try {
let ignoreData = { Packages: [{ name: 'REPLACE_WITH_PACKAGE_NAME' }, { name: 'REPLACE_WITH_PACKAGE_NAME' }] };
try {
const data = await fs.readFile(ignoreFilePath, 'utf-8');
ignoreData = JSON.parse(data);
const ignoreStatusMessage = `Ignore list successfully applied.`;
logMessage(`${ignoreStatusMessage}${os.EOL}`);
} catch (error) {
await fs.writeFile(ignoreFilePath, JSON.stringify(ignoreData, null, 2));
const message = `Info: Created new ignore list template at ${ignoreFilePath}`;
logMessage(`${message}${os.EOL}`);
}
const listData = JSON.parse(await fs.readFile(listFilePath, 'utf-8'));
const removedPackages = [];
listData.Sources.forEach((source) => {
source.Packages = source.Packages.filter((pkg) => {
const isIgnored = ignoreData.Packages.some((ignorePkg) =>
pkg.PackageIdentifier.includes(ignorePkg.name)
);
if (isIgnored) {
removedPackages.push(pkg.PackageIdentifier);
}
return !isIgnored;
});
});
await fs.writeFile(listFilePath, JSON.stringify(listData, null, 2));
const removalMessages =
removedPackages.length > 0
? removedPackages.map((packageList) => `Package is ignored: ${packageList}${os.EOL}`).join('')
: `Ignore list does not contain any packages.${os.EOL}`;
logMessage(removalMessages);
} catch (error) {
const errorMessage = `Failed to filter ignored packages: ${error.message}${os.EOL}`;
logMessage(errorMessage);
}
}
async function checkAndTrimLogFile(logFilePath, maxFileSizeInBytes) {
try {
const stats = await fs.stat(logFilePath);
if (stats.size > maxFileSizeInBytes) {
const logContent = await fs.readFile(logFilePath, 'utf-8');
const blocks = logContent.split(`${os.EOL}${os.EOL}`);
if (blocks.length > 1) {
const trimmedLog = blocks.slice(2).join(`${os.EOL}${os.EOL}`);
await fs.writeFile(logFilePath, trimmedLog, 'utf-8');
logMessage(`Log file size reduced.${os.EOL}`);
} else {
await fs.truncate(logFilePath, 0);
}
}
} catch (error) {
console.error(`Failed to trim log file: ${error}`);
}
}
module.exports = {
setConsoleTitle,
waitForKeyPressAndExit,
logMessage,
executeAndLog,
checkAndTrimLogFile,
filterIgnoredPackages,
};