diff --git a/package.json b/package.json index 64eb268..3653033 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "vscode": "^1.65.0" }, "license": "Apache-2.0", - "displayName": "Super IDE", + "displayName": "SuperIDE", "description": "", "categories": [], "keywords": [], @@ -29,6 +29,11 @@ "command": "superide.showHome", "title": "SuperIDE Home", "category": "Super IDE" + }, + { + "command": "superide.connectRemoteContainer", + "title": "Connect to a Remote Container", + "category": "Super IDE" } ], "menus": { @@ -91,7 +96,8 @@ "vscode:package": "webpack --mode production && vsce package" }, "dependencies": { - "fs-plus": "~3.1.1" + "fs-plus": "~3.1.1", + "node-ssh": "^13.2.0" }, "devDependencies": { "@babel/core": "~7.21.3", diff --git a/src/home.js b/src/home.js index 5e2a26f..4a830ef 100644 --- a/src/home.js +++ b/src/home.js @@ -1,6 +1,79 @@ -export default class SIHome { - constructor() { +import * as vscode from 'vscode'; +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 ` + + + + + + Cat Coding + + +

Cat Coding

+ + + + + + + + `; + } } \ No newline at end of file diff --git a/src/main.js b/src/main.js index 349e52c..30116ff 100644 --- a/src/main.js +++ b/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 QuickAccessTreeProvider from './views/quick-access-tree'; -import vscode from 'vscode'; +const {NodeSSH} = require('node-ssh') class SuperIDEExtension { constructor() { @@ -12,6 +17,7 @@ class SuperIDEExtension { async activate(context) { this.context = context; + this.siHome = new SIHome(context); this.subscriptions.push( vscode.window.registerTreeDataProvider( @@ -29,7 +35,66 @@ class SuperIDEExtension { 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(); ssh.connect({ host: host, @@ -38,9 +103,8 @@ class SuperIDEExtension { port: port }).then(() => { console.log('Connected successfully'); - ssh.execCommand(`mkdir ~/.ssh && - touch ~/.ssh/authorized_keys && - echo ${publicKey} >> ~/.ssh/authorized_keys`, ) + ssh.execCommand(`mkdir -p ~/.ssh && + echo '${publicKey}' >> ~/.ssh/authorized_keys`, ) .then(result => { if (result.stdout) console.log('STDOUT: ' + result.stdout); if (result.stderr) console.log('STDERR: ' + result.stderr); @@ -50,11 +114,13 @@ class SuperIDEExtension { }); } - registerGlobalCommands() { this.subscriptions.push( - vscode.commands.registerCommand('superide.showHome', (startUrl) => - this.siHome.toggle(startUrl) + vscode.commands.registerCommand('superide.showHome', (url) => + this.siHome.toggle(url) + ), + vscode.commands.registerCommand('superide.connectRemoteContainer', () => + this.connectRemoteContainer() ) ); } diff --git a/src/views/quick-access-tree.js b/src/views/quick-access-tree.js index 7d7f9d6..1a980ba 100644 --- a/src/views/quick-access-tree.js +++ b/src/views/quick-access-tree.js @@ -33,7 +33,7 @@ export default class QuickAccessTreeProvider { undefined, undefined, vscode.TreeItemCollapsibleState.Expanded, - [new QuickItem('hello', 'superide.hello')] + [new QuickItem('Connect Remote Container', 'superide.connectRemoteContainer')] ), ]; }