sbt/vscode-sbt-scala/server/src/server.ts

49 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
import * as path from 'path';
import * as url from 'url';
import * as net from 'net';
let fs = require('fs'),
os = require('os'),
stdin = process.stdin,
stdout = process.stdout;
let socket = new net.Socket();
socket.on('data', (chunk: any) => {
// send it back to stdout
stdout.write(chunk);
}).on('end', () => {
stdin.pause();
});
connectSocket(socket);
stdin.resume();
stdin.on('data', (chunk: any) => {
socket.write(chunk);
}).on('end', () => {
socket.end();
});
function connectSocket(socket: net.Socket): net.Socket {
let u = discoverUrl();
// let socket = net.Socket();
if (u.protocol == 'tcp:') {
socket.connect(+u.port, '127.0.0.1');
} else if (u.protocol == 'local:' && os.platform() == 'win32') {
let pipePath = '\\\\.\\pipe\\' + u.hostname;
socket.connect(pipePath);
} else if (u.protocol == 'local:') {
socket.connect(u.path);
} else {
throw 'Unknown protocol ' + u.protocol;
}
return socket;
}
// the port file is hardcoded to a particular location relative to the build.
function discoverUrl(): url.Url {
let pf = path.join(process.cwd(), 'project', 'target', 'active.json');
let portfile = JSON.parse(fs.readFileSync(pf));
return url.parse(portfile.uri);
}