Skip to content

Commit

Permalink
Fixed following issues,
Browse files Browse the repository at this point in the history
1, Preview of JSON Swagger files not getting updated in realtime.
2, Preview window in vs code not switching to latest file.
3, File name in preview window to identify which file is in preview.
4, Instead of retrying from default port until next port is available, try with the last incremented port.
  • Loading branch information
arjun-g committed Mar 24, 2017
1 parent b8a1e51 commit cac5980
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 19 deletions.
52 changes: 34 additions & 18 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ class Viewer {
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);
});
return vscode.commands.executeCommand('vscode.previewHtml', this.uri, vscode.ViewColumn.Two, 'Swagger Preview - ' + path.basename(editor.document.fileName.toLowerCase()))
.then(success => {
vscode.window.showTextDocument(editor.document);
}, reason => {
vscode.window.showErrorMessage(reason);
});
}

register() {
Expand All @@ -62,17 +61,21 @@ class Viewer {
// your extension is activated the very first time the command is executed
function activate(context) {

var viewer = new Viewer(context);
var ds = viewer.register();
// 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 lastDefaultPort
var defaultPort = lastDefaultPort = vscode.workspace.getConfiguration('swaggerViewer').defaultPort || 9000;
var disposable = vscode.commands.registerCommand('extension.previewSwagger', function () {
var openBrowser = vscode.workspace.getConfiguration('swaggerViewer').previewInBrowser || false;
var defaultPort = vscode.workspace.getConfiguration('swaggerViewer').defaultPort || 9000;
if(vscode.workspace.getConfiguration('swaggerViewer').defaultPort && lastDefaultPort != vscode.workspace.getConfiguration('swaggerViewer').defaultPort){
defaultPort = lastDefaultPort = vscode.workspace.getConfiguration('swaggerViewer').defaultPort
}
let handlePreviewResponse = (option) => {
if (typeof option == 'undefined') {
return;
Expand All @@ -90,6 +93,7 @@ function activate(context) {
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');
Expand All @@ -107,18 +111,25 @@ function activate(context) {
ports[fileName] = port;
ios[fileName] = io;
if (openBrowser) {
vscode.window.showInformationMessage('Preview "' + fileName.substring((fileName.lastIndexOf("\\") || fileName.lastIndexOf("/")) + 1) + '" in http://localhost:' + port + "/",
vscode.window.showInformationMessage('Preview "' + path.basename(fileName) + '" in http://localhost:' + port + "/",
{
title: 'Open',
action: 'open',
url: 'http://localhost:' + port + '/'
}).then(handlePreviewResponse);
} else {
var viewer = new Viewer(context);
var ds = viewer.register();
context.subscriptions.push(...ds);
viewer.setPort(port);
viewer.display();
viewer.upate();
}
//console.log('Example app listening on port 3000!');
ios[fileName].on("connection", function (socket) {
socket.on("GET_NAME", function (data, fn) {
fn(path.basename(fileName));
})
socket.on("GET_UPDATE", function (data, fn) {
fn(doc.getText());
})
Expand All @@ -128,33 +139,39 @@ function activate(context) {
context.subscriptions.push(previewSwagger);
context.subscriptions.push(previewSwaggerController);
previewSwagger.update();
viewer.upate();
defaultPort++;
//viewer.upate();
});
server.on("error", function (err) {
startServer(++port);
startServer(++defaultPort);
})
}
catch (ex) {
startServer(++port);
startServer(++defaultPort);
}
}
startServer(defaultPort);
}
else{
if (openBrowser) {
vscode.window.showInformationMessage('Preview "' + fileName.substring((fileName.lastIndexOf("\\") || fileName.lastIndexOf("/")) + 1) + '" in http://localhost:' + ports[fileName] + "/",
vscode.window.showInformationMessage('Preview "' + path.basename(fileName) + '" in http://localhost:' + ports[fileName] + "/",
{
title: 'Open',
action: 'open',
url: 'http://localhost:' + ports[fileName] + '/'
}).then(handlePreviewResponse);
} else {
var viewer = new Viewer(context);
var ds = viewer.register();
context.subscriptions.push(...ds);
viewer.setPort(ports[fileName]);
viewer.display();
viewer.upate();
}
}
});
context.subscriptions.push(disposable);
context.subscriptions.push(...ds);
//context.subscriptions.push(...ds);
}

function PreviewSwagger(fileName) {
Expand All @@ -165,7 +182,6 @@ function PreviewSwagger(fileName) {
}
this.close = function () {
servers[fileName].close();
console.log("CLOSED");
}
}

Expand All @@ -175,7 +191,7 @@ function PreviewSwaggerController(swag) {
var editor = vscode.window.activeTextEditor;
if (!editor) { return; }
var doc = editor.document;
if (doc.languageId === "yaml") {
if (doc.languageId === "yaml" || doc.languageId === "json") {
swag.update();
} else {
swag.close();
Expand Down
28 changes: 27 additions & 1 deletion static/index.html
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
<!doctype html><meta charset="utf-8"><title>Swagger Editor</title><meta name="description" content="Swagger Editor"><meta name="viewport" content="width=device-width"><link rel="icon" type="image/png" href="./images/favicon-32x32.png" sizes="32x32"><link rel="icon" type="image/png" href="./images/favicon-16x16.png" sizes="16x16"><link rel="stylesheet" href="styles/main.css"><link rel="stylesheet" href="styles/branding.css"><div class="total-wrapper" ui-view></div><script src="scripts/vendor.js"></script><script src="scripts/scripts.js"></script><script src="scripts/branding.js"></script><script src="/socket.io/socket.io.js"></script><script>var socket = io.connect();</script>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Swagger Viewer</title>
<meta name="description" content="Swagger Viewer">
<meta name="viewport" content="width=device-width">
<link rel="icon" type="image/png" href="./images/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="./images/favicon-16x16.png" sizes="16x16">
<link rel="stylesheet" href="styles/main.css">
<link rel="stylesheet" href="styles/branding.css">
</head>
<body>
<div class="total-wrapper" ui-view>
</div>
<script src="scripts/vendor.js"></script>
<script src="scripts/scripts.js"></script>
<script src="scripts/branding.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
socket.emit('GET_NAME', {}, (name) => {
if(name) document.title = 'Swagger Viewer - ' + name
})
</script>
</body>
</html>

0 comments on commit cac5980

Please sign in to comment.