-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
198 lines (134 loc) · 4.2 KB
/
app.js
File metadata and controls
198 lines (134 loc) · 4.2 KB
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
const path = require("path");
const fs = require("fs");
const { exec } = require("child_process");
const { PowerShell } = require("node-powershell");
// ================= CONFIG =================
//studio
let driverUrl = "https://gfwsl.geforce.com/services_toolkit/services/com/nvidia/services/AjaxDriverService.php?func=DriverManualLookup&psid=131&pfid=1076&osID=135&languageCode=1033&beta=0&isWHQL=0&dltype=-1&dch=1&upCRD=1&qnf=0&sort1=1&numberOfResults=10";
// game
//driverUrl = "https://gfwsl.geforce.com/services_toolkit/services/com/nvidia/services/AjaxDriverService.php?func=DriverManualLookup&psid=131&pfid=1076&osID=135&languageCode=1033&beta=0&isWHQL=0&dltype=-1&dch=1&upCRD=0&qnf=0&sort1=1&numberOfResults=10";
const DRIVER_DIR = path.resolve(__dirname, "drivers");
// ================= MAIN =================
main();
// ================= FLOW =================
async function main() {
try {
ensureDir();
const installed = await getInstalledVersion();
const latest = await getLatestDriver();
console.log("Installed:", installed);
console.log("Available:", latest.version);
if (latest.version <= installed) {
console.log("Up to date");
process.exit(0);
}
const filePath = await download(latest.url);
launchInstaller(filePath);
} catch (e) {
console.error(e);
process.exit(1);
}
}
// ================= SETUP =================
function ensureDir() {
if (!fs.existsSync(DRIVER_DIR)) {
fs.mkdirSync(DRIVER_DIR, { recursive: true });
}
}
// ================= VERSION =================
async function getInstalledVersion() {
const ps = new PowerShell({
executionPolicy: "Bypass",
noProfile: true,
});
const cmd = PowerShell.command`
(Get-WmiObject Win32_PnPSignedDriver |
Where-Object {
$_.devicename -like "*nvidia*" -and
$_.devicename -notlike "*audio*" -and
$_.devicename -notlike "*USB*" -and
$_.devicename -notlike "*SHIELD*"
}).DriverVersion.SubString(6).Remove(1,1).Insert(3,".")
`;
const out = await ps.invoke(cmd);
ps.dispose();
return Number(out.raw);
}
// ================= LOOKUP =================
async function getLatestDriver() {
const res = await fetch(driverUrl);
if (!res.ok) {
throw new Error("Driver lookup failed");
}
const data = await res.json();
const list = data.IDS;
list.sort(
(a, b) =>
Number(b.downloadInfo.Version) -
Number(a.downloadInfo.Version)
);
const d = list[0].downloadInfo;
return {
version: Number(d.Version),
url: d.DownloadURL,
};
}
// ================= DOWNLOAD =================
function formatBytes(bytes) {
if (bytes < 1024) return bytes + " B";
const units = ["KB", "MB", "GB", "TB"];
let i = -1;
do {
bytes /= 1024;
i++;
} while (bytes >= 1024 && i < units.length - 1);
return bytes.toFixed(1) + " " + units[i];
}
async function download(url) {
const name = path.basename(url);
const filePath = path.join(DRIVER_DIR, name);
console.log("Downloading:", name);
const res = await fetch(url);
if (!res.ok) {
throw new Error("Download failed");
}
const total = Number(res.headers.get("content-length")) || 0;
const file = fs.createWriteStream(filePath);
const reader = res.body.getReader();
let downloaded = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
downloaded += value.length;
file.write(Buffer.from(value));
if (total) {
const pct = ((downloaded / total) * 100).toFixed(1);
process.stdout.write(
`\r${formatBytes(downloaded)} / ${formatBytes(total)} (${pct}%)`
);
} else {
process.stdout.write(
`\r${formatBytes(downloaded)}`
);
}
}
file.end();
await new Promise(r => file.on("finish", r));
process.stdout.write("\n");
console.log("Saved:", filePath);
return filePath;
}
// ================= INSTALL =================
function launchInstaller(p) {
const fullPath = path.resolve(p);
console.log("Launching installer:", fullPath);
// Wrap path in double quotes
const cmd = `start "" "${fullPath}"`;
exec(cmd, { windowsHide: false }, (err) => {
if (err) {
console.error("Failed to launch installer:", err);
process.exit(1);
}
process.exit(0);
});
}