feat: connect remote container and showHome
This commit is contained in:
		
							
								
								
									
										10
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								package.json
									
									
									
									
									
								
							@@ -6,7 +6,7 @@
 | 
				
			|||||||
    "vscode": "^1.65.0"
 | 
					    "vscode": "^1.65.0"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "license": "Apache-2.0",
 | 
					  "license": "Apache-2.0",
 | 
				
			||||||
  "displayName": "Super IDE",
 | 
					  "displayName": "SuperIDE",
 | 
				
			||||||
  "description": "",
 | 
					  "description": "",
 | 
				
			||||||
  "categories": [],
 | 
					  "categories": [],
 | 
				
			||||||
  "keywords": [],
 | 
					  "keywords": [],
 | 
				
			||||||
@@ -29,6 +29,11 @@
 | 
				
			|||||||
        "command": "superide.showHome",
 | 
					        "command": "superide.showHome",
 | 
				
			||||||
        "title": "SuperIDE Home",
 | 
					        "title": "SuperIDE Home",
 | 
				
			||||||
        "category": "Super IDE"
 | 
					        "category": "Super IDE"
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					      {
 | 
				
			||||||
 | 
					        "command": "superide.connectRemoteContainer",
 | 
				
			||||||
 | 
					        "title": "Connect to a Remote Container",
 | 
				
			||||||
 | 
					        "category": "Super IDE"
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    ],
 | 
					    ],
 | 
				
			||||||
    "menus": {
 | 
					    "menus": {
 | 
				
			||||||
@@ -91,7 +96,8 @@
 | 
				
			|||||||
    "vscode:package": "webpack --mode production && vsce package"
 | 
					    "vscode:package": "webpack --mode production && vsce package"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "dependencies": {
 | 
					  "dependencies": {
 | 
				
			||||||
    "fs-plus": "~3.1.1"
 | 
					    "fs-plus": "~3.1.1",
 | 
				
			||||||
 | 
					    "node-ssh": "^13.2.0"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "devDependencies": {
 | 
					  "devDependencies": {
 | 
				
			||||||
    "@babel/core": "~7.21.3",
 | 
					    "@babel/core": "~7.21.3",
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										77
									
								
								src/home.js
									
									
									
									
									
								
							
							
						
						
									
										77
									
								
								src/home.js
									
									
									
									
									
								
							@@ -1,6 +1,79 @@
 | 
				
			|||||||
export default class SIHome {
 | 
					import * as vscode from 'vscode';
 | 
				
			||||||
    constructor() {
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default class SIHome {
 | 
				
			||||||
 | 
					    constructor(context) {
 | 
				
			||||||
 | 
					        this.context = context;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    toggle(url) {
 | 
				
			||||||
 | 
					        const panel = vscode.window.createWebviewPanel(
 | 
				
			||||||
 | 
					        'myWebview',
 | 
				
			||||||
 | 
					        'My Webview',
 | 
				
			||||||
 | 
					        vscode.ViewColumn.One,
 | 
				
			||||||
 | 
					        {}
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        panel.webview.options = {
 | 
				
			||||||
 | 
					            enableScripts: true,
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        panel.webview.html = this.getWebviewContent();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        panel.webview.onDidReceiveMessage(
 | 
				
			||||||
 | 
					            (message) => {
 | 
				
			||||||
 | 
					                switch (message.command) {
 | 
				
			||||||
 | 
					                    case 'alert':
 | 
				
			||||||
 | 
					                        vscode.window.showInformationMessage(message.text);
 | 
				
			||||||
 | 
					                        return;
 | 
				
			||||||
 | 
					                    case 'openFolder':
 | 
				
			||||||
 | 
					                        this.openFolderInVSCode(message.path);
 | 
				
			||||||
 | 
					                        return;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            undefined,
 | 
				
			||||||
 | 
					            this.context.subscriptions
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    openFolderInVSCode(path) {
 | 
				
			||||||
 | 
					        vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(path))
 | 
				
			||||||
 | 
					        .then((result) => {
 | 
				
			||||||
 | 
					            console.log('Opened folder: ', result);
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					        .catch((error) => {
 | 
				
			||||||
 | 
					            console.error('Error opening folder: ', error);
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    getWebviewContent() {
 | 
				
			||||||
 | 
					    return `
 | 
				
			||||||
 | 
					        <!DOCTYPE html>
 | 
				
			||||||
 | 
					        <html lang="en">
 | 
				
			||||||
 | 
					        <head>
 | 
				
			||||||
 | 
					            <meta charset="UTF-8">
 | 
				
			||||||
 | 
					            <meta name="viewport" content="width=device-width, initial-scale=1.0">
 | 
				
			||||||
 | 
					            <title>Cat Coding</title>
 | 
				
			||||||
 | 
					        </head>
 | 
				
			||||||
 | 
					        <body>
 | 
				
			||||||
 | 
					            <h1>Cat Coding</h1>
 | 
				
			||||||
 | 
					            <img src="https://media.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif" width="300" />
 | 
				
			||||||
 | 
					            <button onclick="showAlert()">Show Alert</button>
 | 
				
			||||||
 | 
					            <button onclick="openFolder()">Open Project</button>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            <script>
 | 
				
			||||||
 | 
					                const vscode = acquireVsCodeApi();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                function showAlert() {
 | 
				
			||||||
 | 
					                    vscode.postMessage({ command: 'alert', text: 'Hello from the Webview!' });
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                function openFolder() {
 | 
				
			||||||
 | 
					                    vscode.postMessage({ command: 'openFolder', path: 'D:/Programs/hello' });
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            </script>
 | 
				
			||||||
 | 
					        </body>
 | 
				
			||||||
 | 
					        </html>
 | 
				
			||||||
 | 
					    `;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										84
									
								
								src/main.js
									
									
									
									
									
								
							
							
						
						
									
										84
									
								
								src/main.js
									
									
									
									
									
								
							@@ -1,7 +1,12 @@
 | 
				
			|||||||
 | 
					import vscode from 'vscode';
 | 
				
			||||||
 | 
					import fs from 'fs';
 | 
				
			||||||
 | 
					import path from 'path';
 | 
				
			||||||
 | 
					import os from 'os';
 | 
				
			||||||
 | 
					import QuickAccessTreeProvider from './views/quick-access-tree';
 | 
				
			||||||
 | 
					import SIHome from './home';
 | 
				
			||||||
import * as utils from './utils';
 | 
					import * as utils from './utils';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import QuickAccessTreeProvider from './views/quick-access-tree';
 | 
					const {NodeSSH} = require('node-ssh')
 | 
				
			||||||
import vscode from 'vscode';
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class SuperIDEExtension {
 | 
					class SuperIDEExtension {
 | 
				
			||||||
  constructor() {
 | 
					  constructor() {
 | 
				
			||||||
@@ -12,6 +17,7 @@ class SuperIDEExtension {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  async activate(context) {
 | 
					  async activate(context) {
 | 
				
			||||||
    this.context = context;
 | 
					    this.context = context;
 | 
				
			||||||
 | 
					    this.siHome = new SIHome(context);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this.subscriptions.push(
 | 
					    this.subscriptions.push(
 | 
				
			||||||
      vscode.window.registerTreeDataProvider(
 | 
					      vscode.window.registerTreeDataProvider(
 | 
				
			||||||
@@ -29,7 +35,66 @@ class SuperIDEExtension {
 | 
				
			|||||||
    vscode.commands.executeCommand('superide.showHome');
 | 
					    vscode.commands.executeCommand('superide.showHome');
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  firstConnect(host, username, password, port, publicKey) {
 | 
					  // TODO: finish firstConnect (automatically)
 | 
				
			||||||
 | 
					  async connectRemoteContainer() {
 | 
				
			||||||
 | 
					    const host = 'mengning.com.cn';
 | 
				
			||||||
 | 
					    const username = 'linux';
 | 
				
			||||||
 | 
					    const password = 'a7%Xs&&TXG';
 | 
				
			||||||
 | 
					    const port = 31874;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    console.log('Connecting to remote container...');
 | 
				
			||||||
 | 
					    // detect if local ssh config has the host registered
 | 
				
			||||||
 | 
					    // TODO: use remote.SSH.configFile
 | 
				
			||||||
 | 
					    const sshConfigPath = path.join(os.homedir(), '.ssh', 'config');
 | 
				
			||||||
 | 
					    if (fs.existsSync(sshConfigPath)) {
 | 
				
			||||||
 | 
					      const config = fs.readFileSync(sshConfigPath, 'utf8');
 | 
				
			||||||
 | 
					      // TODO: more robust regex
 | 
				
			||||||
 | 
					      if (config.includes(host)) {
 | 
				
			||||||
 | 
					        console.log('Host already registered in local ssh config');
 | 
				
			||||||
 | 
					        // TODO: connect the specified folder
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        // the host has not been registered in the local ssh config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // connect the host and add the public key to the remote authorized_keys
 | 
				
			||||||
 | 
					        const defaultPublicKeyPath = path.join(os.homedir(), '.ssh', 'id_rsa.pub');
 | 
				
			||||||
 | 
					        // TODO: if there is no public key, generate one
 | 
				
			||||||
 | 
					        const publicKey = fs.readFileSync(defaultPublicKeyPath, 'utf8');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        await this.firstConnect(
 | 
				
			||||||
 | 
					          host,
 | 
				
			||||||
 | 
					          username,
 | 
				
			||||||
 | 
					          password,
 | 
				
			||||||
 | 
					          port,
 | 
				
			||||||
 | 
					          publicKey
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // append the host to the local ssh config file 
 | 
				
			||||||
 | 
					        const sshConfigContent = 
 | 
				
			||||||
 | 
					        `\n
 | 
				
			||||||
 | 
					        Host ${host}
 | 
				
			||||||
 | 
					          HostName ${host}
 | 
				
			||||||
 | 
					          Port ${port}
 | 
				
			||||||
 | 
					          User ${username}
 | 
				
			||||||
 | 
					          PreferredAuthentications publickey
 | 
				
			||||||
 | 
					          IdentityFile ~/.ssh/id_rsa
 | 
				
			||||||
 | 
					          `;
 | 
				
			||||||
 | 
					        // append the host to the local ssh config file
 | 
				
			||||||
 | 
					        fs.writeFileSync(sshConfigPath, sshConfigContent, { encoding: 'utf8',  flag: 'a' });
 | 
				
			||||||
 | 
					        console.log('Host registered in local ssh config');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // connect the host with remote-ssh extension
 | 
				
			||||||
 | 
					        await vscode.commands.executeCommand('opensshremotes.openEmptyWindowInCurrentWindow', host)
 | 
				
			||||||
 | 
					          .then(() => {
 | 
				
			||||||
 | 
					            console.log('Connected to host');
 | 
				
			||||||
 | 
					          })
 | 
				
			||||||
 | 
					          .catch((error) => {
 | 
				
			||||||
 | 
					            console.error('Error connecting to host: ', error);
 | 
				
			||||||
 | 
					          });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  async firstConnect(host, username, password, port, publicKey) {
 | 
				
			||||||
    const ssh = new NodeSSH();
 | 
					    const ssh = new NodeSSH();
 | 
				
			||||||
    ssh.connect({
 | 
					    ssh.connect({
 | 
				
			||||||
    	host: host,
 | 
					    	host: host,
 | 
				
			||||||
@@ -38,9 +103,8 @@ class SuperIDEExtension {
 | 
				
			|||||||
    	port: port
 | 
					    	port: port
 | 
				
			||||||
    }).then(() => {
 | 
					    }).then(() => {
 | 
				
			||||||
    	console.log('Connected successfully');
 | 
					    	console.log('Connected successfully');
 | 
				
			||||||
    	ssh.execCommand(`mkdir ~/.ssh && 
 | 
					    	ssh.execCommand(`mkdir -p ~/.ssh && 
 | 
				
			||||||
    					touch ~/.ssh/authorized_keys &&  
 | 
					    					echo '${publicKey}' >> ~/.ssh/authorized_keys`, )
 | 
				
			||||||
    					echo ${publicKey} >> ~/.ssh/authorized_keys`, )
 | 
					 | 
				
			||||||
    		.then(result => {
 | 
					    		.then(result => {
 | 
				
			||||||
    			if (result.stdout) console.log('STDOUT: ' + result.stdout);
 | 
					    			if (result.stdout) console.log('STDOUT: ' + result.stdout);
 | 
				
			||||||
    			if (result.stderr) console.log('STDERR: ' + result.stderr);
 | 
					    			if (result.stderr) console.log('STDERR: ' + result.stderr);
 | 
				
			||||||
@@ -50,11 +114,13 @@ class SuperIDEExtension {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
  registerGlobalCommands() {
 | 
					  registerGlobalCommands() {
 | 
				
			||||||
    this.subscriptions.push(
 | 
					    this.subscriptions.push(
 | 
				
			||||||
      vscode.commands.registerCommand('superide.showHome', (startUrl) =>
 | 
					      vscode.commands.registerCommand('superide.showHome', (url) =>
 | 
				
			||||||
        this.siHome.toggle(startUrl)
 | 
					        this.siHome.toggle(url)
 | 
				
			||||||
 | 
					      ),
 | 
				
			||||||
 | 
					      vscode.commands.registerCommand('superide.connectRemoteContainer', () =>
 | 
				
			||||||
 | 
					        this.connectRemoteContainer() 
 | 
				
			||||||
      )
 | 
					      )
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -33,7 +33,7 @@ export default class QuickAccessTreeProvider {
 | 
				
			|||||||
        undefined,
 | 
					        undefined,
 | 
				
			||||||
        undefined,
 | 
					        undefined,
 | 
				
			||||||
        vscode.TreeItemCollapsibleState.Expanded,
 | 
					        vscode.TreeItemCollapsibleState.Expanded,
 | 
				
			||||||
        [new QuickItem('hello', 'superide.hello')]
 | 
					        [new QuickItem('Connect Remote Container', 'superide.connectRemoteContainer')]
 | 
				
			||||||
      ),
 | 
					      ),
 | 
				
			||||||
    ];
 | 
					    ];
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user