fix: cannot execute code --version in windows and linux
				
					
				
			This commit is contained in:
		@@ -102,6 +102,8 @@
 | 
				
			|||||||
    "format": "eslint src --ext ts"
 | 
					    "format": "eslint src --ext ts"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "dependencies": {
 | 
					  "dependencies": {
 | 
				
			||||||
 | 
					    "axios": "^1.7.9",
 | 
				
			||||||
 | 
					    "cheerio": "^1.0.0",
 | 
				
			||||||
    "fs-plus": "~3.1.1",
 | 
					    "fs-plus": "~3.1.1",
 | 
				
			||||||
    "node-ssh": "^13.2.0",
 | 
					    "node-ssh": "^13.2.0",
 | 
				
			||||||
    "sshpk": "^1.18.0"
 | 
					    "sshpk": "^1.18.0"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,7 +28,6 @@ export default class RemoteContainer {
 | 
				
			|||||||
        cancellable: false
 | 
					        cancellable: false
 | 
				
			||||||
      }, async (progress) => {
 | 
					      }, async (progress) => {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
 | 
					 | 
				
			||||||
          if (password === undefined) {
 | 
					          if (password === undefined) {
 | 
				
			||||||
            // connect with key
 | 
					            // connect with key
 | 
				
			||||||
            await ssh.connect({
 | 
					            await ssh.connect({
 | 
				
			||||||
@@ -40,26 +39,28 @@ export default class RemoteContainer {
 | 
				
			|||||||
            progress.report({ message: "连接成功,开始安装" });
 | 
					            progress.report({ message: "连接成功,开始安装" });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // install vscode-server and devstar extension
 | 
					            // install vscode-server and devstar extension
 | 
				
			||||||
            const vscodeCommitId = await utils.getVsCodeCommitId();
 | 
					            const vscodeCommitId = await utils.getVsCodeCommitId()
 | 
				
			||||||
            const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
 | 
					            if ("" != vscodeCommitId) {
 | 
				
			||||||
            const installVscodeServerScript = `
 | 
					              const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
 | 
				
			||||||
          mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
					              const installVscodeServerScript = `
 | 
				
			||||||
          if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
 | 
					            mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
				
			||||||
              ~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
 | 
					            if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
 | 
				
			||||||
          else
 | 
					                ~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
 | 
				
			||||||
              wget ${vscodeServerUrl} -O vscode-server-linux-x64.tar.gz && \\
 | 
					            else
 | 
				
			||||||
              mv vscode-server-linux-x64.tar.gz ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
					                wget ${vscodeServerUrl} -O vscode-server-linux-x64.tar.gz && \\
 | 
				
			||||||
              cd ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
					                mv vscode-server-linux-x64.tar.gz ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
				
			||||||
              tar -xvzf vscode-server-linux-x64.tar.gz --strip-components 1 && \\
 | 
					                cd ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
				
			||||||
              rm vscode-server-linux-x64.tar.gz && \\
 | 
					                tar -xvzf vscode-server-linux-x64.tar.gz --strip-components 1 && \\
 | 
				
			||||||
              ~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
 | 
					                rm vscode-server-linux-x64.tar.gz && \\
 | 
				
			||||||
          fi
 | 
					                ~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
 | 
				
			||||||
          `;
 | 
					            fi
 | 
				
			||||||
            await ssh.execCommand(installVscodeServerScript);
 | 
					            `;
 | 
				
			||||||
            console.log("vscode-server and extension installed");
 | 
					              await ssh.execCommand(installVscodeServerScript);
 | 
				
			||||||
            vscode.window.showInformationMessage('安装完成!');
 | 
					              console.log("vscode-server and extension installed");
 | 
				
			||||||
 | 
					              vscode.window.showInformationMessage('安装完成!');
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            await ssh.dispose();
 | 
					              await ssh.dispose();
 | 
				
			||||||
          } else {
 | 
					          } else {
 | 
				
			||||||
            // connect with password (deprecate in future)
 | 
					            // connect with password (deprecate in future)
 | 
				
			||||||
            await ssh.connect({
 | 
					            await ssh.connect({
 | 
				
			||||||
@@ -71,35 +72,27 @@ export default class RemoteContainer {
 | 
				
			|||||||
            progress.report({ message: "连接成功,开始安装" });
 | 
					            progress.report({ message: "连接成功,开始安装" });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // install vscode-server and devstar extension
 | 
					            // install vscode-server and devstar extension
 | 
				
			||||||
            const vscodeCommitId = await utils.getVsCodeCommitId();
 | 
					            const vscodeCommitId = await utils.getVsCodeCommitId()
 | 
				
			||||||
            const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
 | 
					            if ("" != vscodeCommitId) {
 | 
				
			||||||
            const installVscodeServerScript = `
 | 
					              const vscodeServerUrl = `https://vscode.download.prss.microsoft.com/dbazure/download/stable/${vscodeCommitId}/vscode-server-linux-x64.tar.gz`
 | 
				
			||||||
          mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
					              const installVscodeServerScript = `
 | 
				
			||||||
          if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
 | 
					            mkdir -p ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
				
			||||||
              ~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
 | 
					            if [ "$(ls -A ~/.vscode-server/bin/${vscodeCommitId})" ]; then
 | 
				
			||||||
          else
 | 
					                ~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
 | 
				
			||||||
              wget ${vscodeServerUrl} -O vscode-server-linux-x64.tar.gz && \\
 | 
					            else
 | 
				
			||||||
              mv vscode-server-linux-x64.tar.gz ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
					                wget ${vscodeServerUrl} -O vscode-server-linux-x64.tar.gz && \\
 | 
				
			||||||
              cd ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
					                mv vscode-server-linux-x64.tar.gz ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
				
			||||||
              tar -xvzf vscode-server-linux-x64.tar.gz --strip-components 1 && \\
 | 
					                cd ~/.vscode-server/bin/${vscodeCommitId} && \\
 | 
				
			||||||
              rm vscode-server-linux-x64.tar.gz && \\
 | 
					                tar -xvzf vscode-server-linux-x64.tar.gz --strip-components 1 && \\
 | 
				
			||||||
              ~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
 | 
					                rm vscode-server-linux-x64.tar.gz && \\
 | 
				
			||||||
          fi
 | 
					                ~/.vscode-server/bin/${vscodeCommitId}/bin/code-server --install-extension mengning.devstar
 | 
				
			||||||
          `;
 | 
					            fi
 | 
				
			||||||
            await ssh.execCommand(installVscodeServerScript);
 | 
					            `;
 | 
				
			||||||
            console.log("vscode-server and extension installed");
 | 
					              await ssh.execCommand(installVscodeServerScript);
 | 
				
			||||||
            vscode.window.showInformationMessage('安装完成!');
 | 
					              console.log("vscode-server and extension installed");
 | 
				
			||||||
 | 
					              vscode.window.showInformationMessage('安装完成!');
 | 
				
			||||||
            // add the public key to the remote authorized_keys
 | 
					 | 
				
			||||||
            if (!utils.existDefaultPublicKey() || !utils.existDefaultPrivateKey()) {
 | 
					 | 
				
			||||||
              // if there is no public key, generate one
 | 
					 | 
				
			||||||
              utils.createSSHKey()
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            const publicKey = utils.getDefaultPublicKey();
 | 
					 | 
				
			||||||
            await ssh.execCommand(`mkdir -p ~/.ssh && echo '${publicKey}' >> ~/.ssh/authorized_keys`);
 | 
					 | 
				
			||||||
            console.log('Public key added to remote authorized_keys');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            await ssh.dispose();
 | 
					            await ssh.dispose();
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										48
									
								
								src/utils.ts
									
									
									
									
									
								
							
							
						
						
									
										48
									
								
								src/utils.ts
									
									
									
									
									
								
							@@ -1,13 +1,14 @@
 | 
				
			|||||||
import { exec } from 'child_process';
 | 
					 | 
				
			||||||
import * as http from 'http';
 | 
					import * as http from 'http';
 | 
				
			||||||
import * as https from 'https';
 | 
					import * as https from 'https';
 | 
				
			||||||
import * as fs from 'fs';
 | 
					import * as fs from 'fs';
 | 
				
			||||||
import * as path from 'path';
 | 
					import * as path from 'path';
 | 
				
			||||||
import * as os from 'os';
 | 
					import * as os from 'os';
 | 
				
			||||||
import { BlobOptions } from 'buffer';
 | 
					import * as vscode from 'vscode';
 | 
				
			||||||
const {
 | 
					const {
 | 
				
			||||||
  generateKeyPairSync,
 | 
					  generateKeyPairSync,
 | 
				
			||||||
} = require('node:crypto')
 | 
					} = require('node:crypto')
 | 
				
			||||||
 | 
					const axios = require('axios');
 | 
				
			||||||
 | 
					const cheerio = require('cheerio');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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)
 | 
				
			||||||
@@ -38,26 +39,29 @@ export const Sleep = (ms: number) => {
 | 
				
			|||||||
  return new Promise(resolve => setTimeout(resolve, ms))
 | 
					  return new Promise(resolve => setTimeout(resolve, ms))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function getVsCodeCommitId(): Promise<string> {
 | 
					export async function getVsCodeCommitId(): Promise<string> {
 | 
				
			||||||
  return new Promise((resolve, reject) => {
 | 
					    // 获取vscode version
 | 
				
			||||||
    exec('code --version', (error, stdout, stderr) => {
 | 
					    const version = vscode.version
 | 
				
			||||||
      if (error) {
 | 
					
 | 
				
			||||||
        reject('Error occurred:' + error.message);
 | 
					    // 根据version提取相应的release页面中的commitid
 | 
				
			||||||
        return;
 | 
					    try{
 | 
				
			||||||
      }
 | 
					      const { data } = await axios.get(`https://github.com/microsoft/vscode/releases/tag/${version}`);
 | 
				
			||||||
      if (stderr) {
 | 
					    
 | 
				
			||||||
        reject('Error output:' + stderr);
 | 
					      // Load the HTML into cheerio
 | 
				
			||||||
        return;
 | 
					      const $ = cheerio.load(data);
 | 
				
			||||||
      }
 | 
					      // Find the <a> tag with the 'data-hovercard-type="commit"' attribute
 | 
				
			||||||
      const lines = stdout.trim().split('\n');
 | 
					      const commitLink = $('a[data-hovercard-type="commit"]');
 | 
				
			||||||
      if (lines.length > 1) {
 | 
					      // Extract the href attribute and commit hash
 | 
				
			||||||
        const commitId = lines[1]; // 第二行是 commit ID
 | 
					      const href = commitLink.attr('href'); // href example:  /microsoft/vscode/commit/fabdb6a30b49f79a7aba0f2ad9df9b399473380f
 | 
				
			||||||
        resolve(commitId);
 | 
					      const commitHash = href.split('/').pop(); 
 | 
				
			||||||
      } else {
 | 
					    
 | 
				
			||||||
        reject('Unexpected output format:' + stdout);
 | 
					      console.log('Commit Hash:', commitHash);
 | 
				
			||||||
      }
 | 
					
 | 
				
			||||||
    });
 | 
					      return commitHash
 | 
				
			||||||
  })
 | 
					    } catch(error)  {
 | 
				
			||||||
 | 
					      console.error('Failed to get commit id: ' + error)
 | 
				
			||||||
 | 
					      return "" 
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function getDefaultPrivateKeyPath(): string {
 | 
					export function getDefaultPrivateKeyPath(): string {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user