1 import { createWriteStream, unlink } from "fs";
2 import { tmpdir } from "os";
3 import { get as httpsGet } from "https";
4 import { basename, join } from "path";
5 import Logger from "../logger.mjs";
6 import { spawn } from "child_process";
8 async function downloadWindowsInstaller(
10 destinationPath: string
13 "https://github.com/raspberrypi/pico-setup-windows/releases" +
14 `/download/v${version}/pico-setup-windows-x64-standalone.exe`;
16 //const installerFile = createWriteStream(destinationPath);
18 const installerFile = createWriteStream(destinationPath);
20 return new Promise<void>((resolve, reject) => {
21 const request = httpsGet(url, response => {
22 if (response.statusCode === 302) {
23 const redirectedUrl = response.headers.location || "";
24 httpsGet(redirectedUrl, redirectedResponse => {
25 redirectedResponse.pipe(installerFile);
27 redirectedResponse.on("end", () => {
28 installerFile.on("finish", () => {
29 installerFile.close();
30 console.log(`File downloaded!`);
35 redirectedResponse.on("error", error => {
36 unlink(destinationPath, () => {
44 response.pipe(installerFile);
46 response.on("end", () => {
47 installerFile.on("finish", () => {
48 installerFile.close();
49 console.log(`File downloaded!`);
54 response.on("error", error => {
55 unlink(destinationPath, () => {
64 request.on("error", error => {
65 unlink(destinationPath, () => {
74 async function runInstaller(installerPath: string): Promise<void> {
75 return new Promise<void>((resolve, reject) => {
76 const installerName = basename(installerPath);
77 // cant spawn installer directly and with silent falg because error EACCESS
78 //const silentArgs = "/S"; // Specify the silent mode argument
80 const child = spawn("explorer.exe", [installerPath], {
87 child.on("error", error => {
89 `Failed to execute installer "${installerName}": ${error.message}`
94 child.on("exit", code => {
96 console.log(`Installer "${installerName}" started successfully.`);
100 `Installer "${installerName}" exited with code ${code ?? "N/A"}.`
104 `Installer "${installerName}" exited with code ${code ?? "N/A"}.`
112 export async function downloadAndInstallPicoSDKWindows(
114 ): Promise<boolean> {
115 const destinationPath = join(
117 "pico-setup-windows-x64-standalone.exe"
121 await downloadWindowsInstaller(version, destinationPath);
122 Logger.log(`Download of installer v${version} finished.`);
124 await runInstaller(destinationPath);
125 Logger.log(`Installation of SDK v${version} started.`);
130 `[ERROR] Download and/or installation of SDK v${version} failed: ${
131 (error as Error).message