refactor: distinguish how to obtain GetCommitId on different operating systems

This commit is contained in:
Levi Yan
2025-02-21 21:19:34 +08:00
parent da5e253763
commit 04b2d6bcaf

View File

@@ -1,12 +1,29 @@
import * as http from 'http'; import * as http from 'http';
import * as https from 'https'; import * as https from 'https';
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import * as os from 'os';
import { exec } from 'child_process';
const { const {
generateKeyPairSync, generateKeyPairSync,
} = require('node:crypto') } = require('node:crypto')
const axios = require('axios'); const axios = require('axios');
const cheerio = require('cheerio'); 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<string> { export function fetch(url: string): Promise<string> {
// determine the library to use (based on the url protocol) // determine the library to use (based on the url protocol)
const lib = url.startsWith('https://') ? https : http; const lib = url.startsWith('https://') ? https : http;
@@ -37,24 +54,51 @@ export const Sleep = (ms: number) => {
} }
export async function getVsCodeCommitId(): Promise<string> { export async function getVsCodeCommitId(): Promise<string> {
if (isLinux() || isMacOS()) {
return new Promise<string>((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 // 获取vscode version
const version = vscode.version const version = vscode.version
// 根据version提取相应的release页面中的commitid // 根据version提取相应的release页面中的commitid
try{ try {
const { data } = await axios.get(`https://github.com/microsoft/vscode/releases/tag/${version}`); const { data } = await axios.get(`https://github.com/microsoft/vscode/releases/tag/${version}`);
// Load the HTML into cheerio // Load the HTML into cheerio
const $ = cheerio.load(data); const $ = cheerio.load(data);
// Find the <a> tag with the 'data-hovercard-type="commit"' attribute // Find the <a> tag with the 'data-hovercard-type="commit"' attribute
const commitLink = $('a[data-hovercard-type="commit"]'); const commitLink = $('a[data-hovercard-type="commit"]');
// Extract the href attribute and commit hash // Extract the href attribute and commit hash
const href = commitLink.attr('href'); // href example: /microsoft/vscode/commit/fabdb6a30b49f79a7aba0f2ad9df9b399473380f const href = commitLink.attr('href'); // href example: /microsoft/vscode/commit/fabdb6a30b49f79a7aba0f2ad9df9b399473380f
const commitHash = href.split('/').pop(); const commitHash = href.split('/').pop();
return commitHash return commitHash
} catch(error) { } catch (error) {
console.error('Failed to get commit id: ' + error) console.error('Failed to get commit id: ' + error)
return "" return ""
} }
}
} }