refactor: convert js to ts
This commit is contained in:
79
src/home.js
79
src/home.js
@@ -1,79 +0,0 @@
|
||||
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 `
|
||||
<!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>
|
||||
`;
|
||||
}
|
||||
}
|
78
src/home.ts
Normal file
78
src/home.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
export default class SIHome {
|
||||
|
||||
constructor() {
|
||||
}
|
||||
|
||||
toggle(url: string) {
|
||||
console.log('url: ', 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,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
openFolderInVSCode(path: string): void {
|
||||
try {
|
||||
vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(path))
|
||||
console.log('Opened folder: ', path);
|
||||
} 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>
|
||||
`;
|
||||
}
|
||||
}
|
@@ -4,29 +4,23 @@ import path from 'path';
|
||||
import os from 'os';
|
||||
import QuickAccessTreeProvider from './views/quick-access-tree';
|
||||
import SIHome from './home';
|
||||
import * as utils from './utils';
|
||||
|
||||
const {NodeSSH} = require('node-ssh')
|
||||
const { NodeSSH } = require('node-ssh')
|
||||
|
||||
class SuperIDEExtension {
|
||||
constructor() {
|
||||
this.siHome = undefined;
|
||||
this.context = undefined;
|
||||
this.subscriptions = [];
|
||||
}
|
||||
export class SuperIDEExtension {
|
||||
siHome: SIHome;
|
||||
|
||||
async activate(context) {
|
||||
this.context = context;
|
||||
this.siHome = new SIHome(context);
|
||||
constructor(private context: vscode.ExtensionContext) {
|
||||
this.siHome = new SIHome();
|
||||
|
||||
this.subscriptions.push(
|
||||
context.subscriptions.push(
|
||||
vscode.window.registerTreeDataProvider(
|
||||
'superide.quickAccess',
|
||||
new QuickAccessTreeProvider()
|
||||
)
|
||||
);
|
||||
|
||||
this.registerGlobalCommands();
|
||||
this.registerGlobalCommands(context);
|
||||
|
||||
this.startSuperIDEHome();
|
||||
}
|
||||
@@ -69,78 +63,57 @@ class SuperIDEExtension {
|
||||
)
|
||||
|
||||
// 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
|
||||
`;
|
||||
const sshConfigContent =
|
||||
`\nHost ${host}\n HostName ${host}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ~/.ssh/id_rsa\n `;
|
||||
// append the host to the local ssh config file
|
||||
fs.writeFileSync(sshConfigPath, sshConfigContent, { encoding: 'utf8', flag: 'a' });
|
||||
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);
|
||||
});
|
||||
try {
|
||||
await vscode.commands.executeCommand('opensshremotes.openEmptyWindowInCurrentWindow', host)
|
||||
} catch (error) {
|
||||
console.log("Error connecting to host: ", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async firstConnect(host, username, password, port, publicKey) {
|
||||
async firstConnect(host: string, username: string, password: string, port: number, publicKey: string): Promise<void> {
|
||||
const ssh = new NodeSSH();
|
||||
ssh.connect({
|
||||
host: host,
|
||||
username: username,
|
||||
password: password,
|
||||
port: port
|
||||
}).then(() => {
|
||||
console.log('Connected successfully');
|
||||
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);
|
||||
});
|
||||
}).catch(error => {
|
||||
console.log('Error: ', error);
|
||||
});
|
||||
|
||||
try {
|
||||
await ssh.connect({
|
||||
host: host,
|
||||
username: username,
|
||||
password: password,
|
||||
port: port
|
||||
});
|
||||
|
||||
await ssh.execCommand(`mkdir -p ~/.ssh && echo '${publicKey}' >> ~/.ssh/authorized_keys`);
|
||||
console.log('Public key added to remote authorized_keys');
|
||||
} catch (error) {
|
||||
console.error('Error adding public key: ', error);
|
||||
} finally {
|
||||
await ssh.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
registerGlobalCommands() {
|
||||
this.subscriptions.push(
|
||||
registerGlobalCommands(context: vscode.ExtensionContext) {
|
||||
context.subscriptions.push(
|
||||
vscode.commands.registerCommand('superide.showHome', (url) =>
|
||||
this.siHome.toggle(url)
|
||||
),
|
||||
vscode.commands.registerCommand('superide.connectRemoteContainer', () =>
|
||||
this.connectRemoteContainer()
|
||||
this.connectRemoteContainer()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
disposeSubscriptions() {
|
||||
utils.disposeSubscriptions(this.subscriptions);
|
||||
}
|
||||
|
||||
deactivate() {
|
||||
this.disposeSubscriptions();
|
||||
}
|
||||
}
|
||||
|
||||
export const extension = new SuperIDEExtension();
|
||||
|
||||
export function activate(context) {
|
||||
extension.activate(context);
|
||||
return extension;
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
return new SuperIDEExtension(context);
|
||||
}
|
||||
|
||||
export function deactivate() {
|
||||
extension.deactivate();
|
||||
}
|
@@ -1,5 +0,0 @@
|
||||
export function disposeSubscriptions(subscriptions) {
|
||||
while (subscriptions.length) {
|
||||
subscriptions.pop().dispose();
|
||||
}
|
||||
}
|
0
src/utils.ts
Normal file
0
src/utils.ts
Normal file
@@ -1,7 +1,9 @@
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
class QuickItem extends vscode.TreeItem {
|
||||
constructor(label, command, args, collapsibleState, children) {
|
||||
customChildren: QuickItem[] | undefined;
|
||||
|
||||
constructor(label: string, command?: string, args?: any, collapsibleState?: vscode.TreeItemCollapsibleState, children?: QuickItem[]) {
|
||||
super(label, collapsibleState);
|
||||
if (command) {
|
||||
this.command = {
|
||||
@@ -15,7 +17,7 @@ class QuickItem extends vscode.TreeItem {
|
||||
}
|
||||
|
||||
export default class QuickAccessTreeProvider {
|
||||
getChildren(element) {
|
||||
getChildren(element: QuickItem) {
|
||||
if (element && element.customChildren) {
|
||||
return element.customChildren;
|
||||
}
|
||||
@@ -38,7 +40,7 @@ export default class QuickAccessTreeProvider {
|
||||
];
|
||||
}
|
||||
|
||||
getTreeItem(element) {
|
||||
getTreeItem(element: QuickItem) {
|
||||
return element;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user