Skip to content

Commit e9bb146

Browse files
committed
PoC ptt-client and proxy
fixes #1
1 parent 36b45de commit e9bb146

8 files changed

+247
-13
lines changed

.env.example

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# For testing only
2+
3+
USERNAME=
4+
PASSWORD=

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ out
22
node_modules
33
.vscode-test/
44
*.vsix
5+
6+
.env

.vscode/launch.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"request": "launch",
1111
"runtimeExecutable": "${execPath}",
1212
"args": [
13-
"--extensionDevelopmentPath=${workspaceFolder}"
13+
"--extensionDevelopmentPath=${workspaceFolder}",
14+
"--disable-extensions"
1415
],
1516
"outFiles": [
1617
"${workspaceFolder}/out/**/*.js"

package-lock.json

+149-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+10-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@
2929
"test": "npm run compile && node ./node_modules/vscode/bin/test"
3030
},
3131
"devDependencies": {
32-
"typescript": "^3.3.1",
33-
"vscode": "^1.1.28",
34-
"tslint": "^5.12.1",
32+
"@types/mocha": "^2.2.42",
3533
"@types/node": "^10.12.21",
36-
"@types/mocha": "^2.2.42"
34+
"dotenv": "^7.0.0",
35+
"tslint": "^5.12.1",
36+
"typescript": "^3.3.1",
37+
"vscode": "^1.1.28"
38+
},
39+
"dependencies": {
40+
"http-proxy": "^1.17.0",
41+
"ptt-client": "^0.4.0",
42+
"ws": "^6.2.0"
3743
}
3844
}

src/extension.ts

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
// The module 'vscode' contains the VS Code extensibility API
22
// Import the module and reference it with the alias vscode in your code below
33
import * as vscode from 'vscode';
4+
(global as any).WebSocket = require('ws');
5+
6+
const path = require('path');
7+
require('./proxy');
8+
9+
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
10+
11+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
12+
13+
import PTT from 'ptt-client';
14+
15+
const ptt = new PTT({
16+
url: 'ws://127.0.0.1:9770',
17+
});
18+
19+
let firstTime = true;
420

521
// this method is called when your extension is activated
622
// your extension is activated the very first time the command is executed
@@ -13,11 +29,31 @@ export function activate(context: vscode.ExtensionContext) {
1329
// The command has been defined in the package.json file
1430
// Now provide the implementation of the command with registerCommand
1531
// The commandId parameter must match the command field in package.json
16-
let disposable = vscode.commands.registerCommand('extension.helloWorld', () => {
17-
// The code you place here will be executed every time your command is executed
32+
let disposable = vscode.commands.registerCommand('extension.helloWorld', async () => {
33+
34+
// The code you place here will be executed every time your command is executed
35+
if (firstTime) {
36+
// !FIXME: workaround for PTT not get initialized properly
37+
await sleep(1000);
38+
firstTime = false;
39+
}
40+
41+
const isLogin = await ptt.login(process.env.USERNAME, process.env.PASSWORD)
42+
if (!isLogin) {
43+
// TODO: handle this
44+
45+
return;
46+
}
47+
48+
// get last 20 articles from specific board
49+
let articles = await ptt.getArticles('C_Chat');
50+
console.log(articles);
51+
// // get the content of specific article
52+
// let article = await ptt.getArticle('C_Chat', articles[articles.length-1].sn);
53+
// console.log(article);
1854

1955
// Display a message box to the user
20-
vscode.window.showInformationMessage('Hello World!');
56+
// vscode.window.showInformationMessage('Hello World!');
2157
});
2258

2359
context.subscriptions.push(disposable);

0 commit comments

Comments
 (0)