refactor: distinguish how to obtain GetCommitId on different operating systems
This commit is contained in:
		
							
								
								
									
										54
									
								
								src/utils.ts
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								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<string> {
 | 
			
		||||
  // 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<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
 | 
			
		||||
    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 <a> 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 ""
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user