forked from arjun-g/vs-swagger-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
196 lines (181 loc) · 7.16 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
var vscode = require('vscode');
var path = require('path');
var ports = {}
, servers = {}
, ios = {}
;
class Viewer {
constructor(context) {
this.context = context;
this.uri = vscode.Uri.parse('swagger://preview');
this.Emmittor = new vscode.EventEmitter();
this.onDidChange = this.Emmittor.event;
}
provideTextDocumentContent(uri, token) {
var port = this.port || 9000;
var html = `
<html>
<body style="margin:0px;padding:0px;overflow:hidden">
<div style="position:fixed;height:100%;width:100%;">
<iframe src="http://localhost:${port}" frameborder="0" style="overflow:hidden;height:100%;width:100%" height="100%" width="100%"></iframe>
</div>
</body>
</html>
`;
return html;
}
display() {
var editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
return vscode.commands.executeCommand('vscode.previewHtml', this.uri, vscode.ViewColumn.Two, 'Swagger Preview')
.then(success => {
vscode.window.showTextDocument(editor.document);
return;
}, reason => {
vscode.window.showErrorMessage(reason);
});
}
register() {
var ds = [];
var disposable = vscode.workspace.registerTextDocumentContentProvider('swagger', this);
ds.push(disposable);
return ds;
}
setPort(port) {
this.port = port;
}
upate() {
this.Emmittor.fire(this.uri);
}
};
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
var viewer = new Viewer(context);
var ds = viewer.register();
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
var disposable = vscode.commands.registerCommand('extension.previewSwagger', function () {
var openBrowser = vscode.workspace.getConfiguration('swaggerViewer').previewInBrowser || false;
var defaultPort = vscode.workspace.getConfiguration('swaggerViewer').defaultPort || 9000;
let handlePreviewResponse = (option) => {
if (typeof option == 'undefined') {
return;
}
if (option.action == "open") {
let uri = vscode.Uri.parse(option.url);
vscode.commands.executeCommand('vscode.open', uri);
}
};
var editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
var doc = editor.document;
var fileName = doc.fileName.toLowerCase();
if (!servers[fileName]) {
// Display a message box to the user
//vscode.window.showInformationMessage('Hello World!');
var express = require('express');
var http = require('http');
var app = express();
app.use(express.static(path.join(__dirname, 'static')));
//app.set('port', 3002);
var server = http.createServer(app);
var io = require('socket.io')(server);
function startServer(port) {
app.set('port', port);
try {
server.listen(port, function (err) {
servers[fileName] = server;
ports[fileName] = port;
ios[fileName] = io;
if (openBrowser) {
vscode.window.showInformationMessage('Preview "' + fileName.substring((fileName.lastIndexOf("\\") || fileName.lastIndexOf("/")) + 1) + '" in http://localhost:' + port + "/",
{
title: 'Open',
action: 'open',
url: 'http://localhost:' + port + '/'
}).then(handlePreviewResponse);
} else {
viewer.setPort(port);
viewer.display();
}
//console.log('Example app listening on port 3000!');
ios[fileName].on("connection", function (socket) {
socket.on("GET_UPDATE", function (data, fn) {
fn(doc.getText());
})
})
var previewSwagger = new PreviewSwagger(fileName);
var previewSwaggerController = new PreviewSwaggerController(previewSwagger);
context.subscriptions.push(previewSwagger);
context.subscriptions.push(previewSwaggerController);
previewSwagger.update();
viewer.upate();
});
server.on("error", function (err) {
startServer(++port);
})
}
catch (ex) {
startServer(++port);
}
}
startServer(defaultPort);
}
else{
if (openBrowser) {
vscode.window.showInformationMessage('Preview "' + fileName.substring((fileName.lastIndexOf("\\") || fileName.lastIndexOf("/")) + 1) + '" in http://localhost:' + ports[fileName] + "/",
{
title: 'Open',
action: 'open',
url: 'http://localhost:' + ports[fileName] + '/'
}).then(handlePreviewResponse);
} else {
viewer.display();
}
}
});
context.subscriptions.push(disposable);
context.subscriptions.push(...ds);
}
function PreviewSwagger(fileName) {
var editor = vscode.window.activeTextEditor;
var doc = editor.document;
this.update = function () {
ios[fileName].emit("TEXT_UPDATE", doc.getText());
}
this.close = function () {
servers[fileName].close();
console.log("CLOSED");
}
}
function PreviewSwaggerController(swag) {
var subscriptions = [];
function update() {
var editor = vscode.window.activeTextEditor;
if (!editor) { return; }
var doc = editor.document;
if (doc.languageId === "yaml") {
swag.update();
} else {
swag.close();
}
}
vscode.window.onDidChangeActiveTextEditor(update, this, subscriptions);
vscode.window.onDidChangeTextEditorSelection(update, this, subscriptions);
swag.update();
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate;