From 04b2d6bcaf85a9c24d94d69dc033b0fe24ceee05 Mon Sep 17 00:00:00 2001 From: Levi Yan Date: Fri, 21 Feb 2025 21:19:34 +0800 Subject: [PATCH] refactor: distinguish how to obtain GetCommitId on different operating systems --- src/utils.ts | 54 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 87ec25a..dbc323e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,12 +1,29 @@ import * as http from 'http'; import * as https from 'https'; import * as vscode from 'vscode'; +import * as os from 'os'; +import { exec } from 'child_process'; + const { generateKeyPairSync, } = require('node:crypto') const axios = require('axios'); const cheerio = require('cheerio'); + +export function isWindows(): boolean { + return os.platform() === 'win32'; +} + +export function isLinux(): boolean { + return os.platform() === 'linux'; +} + +export function isMacOS(): boolean { + return os.platform() === 'darwin'; +} + + export function fetch(url: string): Promise { // determine the library to use (based on the url protocol) const lib = url.startsWith('https://') ? https : http; @@ -37,24 +54,51 @@ export const Sleep = (ms: number) => { } export async function getVsCodeCommitId(): Promise { + if (isLinux() || isMacOS()) { + return new Promise((resolve) => { + exec('code --version', (error, stdout, stderr) => { + if (error) { + console.error('Error occurred:' + error.message); + resolve(""); + return; + } + if (stderr) { + console.error('Error output:' + stderr); + resolve(""); + return; + } + const lines = stdout.trim().split('\n'); + if (lines.length > 1) { + const commitId = lines[1]; // 第二行是 commit ID + resolve(commitId); + } else { + console.error('Unexpected output format:' + stdout); + resolve(""); + return; + } + }); + }) + } else { // 获取vscode version const version = vscode.version // 根据version提取相应的release页面中的commitid - try{ + try { const { data } = await axios.get(`https://github.com/microsoft/vscode/releases/tag/${version}`); - + // Load the HTML into cheerio const $ = cheerio.load(data); // Find the tag with the 'data-hovercard-type="commit"' attribute const commitLink = $('a[data-hovercard-type="commit"]'); // Extract the href attribute and commit hash const href = commitLink.attr('href'); // href example: /microsoft/vscode/commit/fabdb6a30b49f79a7aba0f2ad9df9b399473380f - const commitHash = href.split('/').pop(); + const commitHash = href.split('/').pop(); return commitHash - } catch(error) { + } catch (error) { console.error('Failed to get commit id: ' + error) - return "" + return "" } + + } } \ No newline at end of file