]> Git Repo - pico-vscode.git/blob - src/utils/gitUtil.mts
Add elf2uf2 and pioasm for sdk 1.5.1
[pico-vscode.git] / src / utils / gitUtil.mts
1 import { promisify } from "util";
2 import { exec } from "child_process";
3 import Logger from "../logger.mjs";
4 import { unlink } from "fs/promises";
5
6 const execAsync = promisify(exec);
7
8 /**
9  * Initialize git submodules in downloaded Pico-SDK.
10  *
11  * @param sdkDirectory The directory of the downloaded Pico-SDK.
12  * @returns True if the submodules were initialized successfully, false otherwise.
13  */
14 export async function initSubmodules(
15   sdkDirectory: string,
16   gitExecutable: string = "git"
17 ): Promise<boolean> {
18   try {
19     // Use the "git submodule update --init" command in the specified directory
20     const command =
21       `cd "${sdkDirectory}" && ` +
22       `${
23         process.env.ComSpec === "powershell.exe" ? "&" : ""
24       }"${gitExecutable}" submodule update --init`;
25     await execAsync(command);
26
27     return true;
28   } catch (error) {
29     console.error(error);
30
31     return false;
32   }
33 }
34
35 export async function cloneRepository(
36   repository: string,
37   branch: string,
38   targetDirectory: string,
39   gitExecutable: string = "git"
40 ): Promise<boolean> {
41   // Clone the repository at the specified tag into the target directory
42   const cloneCommand =
43     `${
44       process.env.ComSpec === "powershell.exe" ? "&" : ""
45     }"${gitExecutable}" -c advice.detachedHead=false clone --branch ` +
46     `${branch} ${repository} "${targetDirectory}"`;
47
48   try {
49     await execAsync(cloneCommand);
50
51     Logger.log(`SDK/Pyenv ${branch} has been cloned and installed.`);
52
53     return true;
54   } catch (error) {
55     try {
56       await unlink(targetDirectory);
57     } catch {
58       /* */
59     }
60
61     const err = error instanceof Error ? error.message : (error as string);
62     if (err.includes("already exists")) {
63       return true;
64     }
65     Logger.log(`Error while cloning repository: ${err}`);
66
67     return false;
68   }
69 }
This page took 0.027188 seconds and 4 git commands to generate.