Skip to content

Commit

Permalink
Merge pull request #1 from gxcsoccer/first
Browse files Browse the repository at this point in the history
feat: 实现单实例版本 tsserver
  • Loading branch information
gxcsoccer committed Mar 1, 2020
2 parents 712f472 + b0cc020 commit 8cc0ffe
Show file tree
Hide file tree
Showing 31 changed files with 1,882 additions and 8 deletions.
6 changes: 5 additions & 1 deletion .autod.conf.js
Expand Up @@ -11,10 +11,14 @@ module.exports = {
'autod',
'egg-ci',
'egg-bin',
'vscode',
'eslint',
'eslint-config-egg',
'typescript',
'contributors',
],
keep: [],
keep: [
'vscode',
],
semver: [],
};
2 changes: 1 addition & 1 deletion .eslintignore
@@ -1,5 +1,5 @@
test/fixtures
coverage
examples/**/app/public
example
logs
run
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -102,3 +102,5 @@ dist

# TernJS port file
.tern-port
.vscode
test/.tmp
38 changes: 37 additions & 1 deletion README.md
@@ -1,5 +1,5 @@
# singleton-tsserver
singleton tsserver
单实例的 [tsserver](https://github.com/Microsoft/TypeScript/wiki/Standalone-Server-%28tsserver%29)

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
Expand All @@ -20,3 +20,39 @@ singleton tsserver
[snyk-url]: https://snyk.io/test/npm/singleton-tsserver
[download-image]: https://img.shields.io/npm/dm/singleton-tsserver.svg?style=flat-square
[download-url]: https://npmjs.org/package/singleton-tsserver

针对同样的参数,只启动一个 tsserver 实例

## 用法

```js
const ClusterTsServerProcess = require('singleton-tsserver');

const options = {
tsServerPath: '<tsServerPath>',
args: [
'--useInferredProjectPerProjectRoot',
'--enableTelemetry',
'--noGetErrOnBackgroundUpdate',
'--validateDefaultNpmLocation',
],
};

const proc = new ClusterTsServerProcess(options);
proc.stdout.on('data', data => {
console.log(data.toString());
});

proc.write({
seq: 0,
type: 'request',
command: 'configure',
arguments: {
hostInfo: 'vscode',
preferences: {
providePrefixAndSuffixTextForRename: true,
allowRenameOfImportPath: true,
},
},
});
```
10 changes: 10 additions & 0 deletions example/c.js
@@ -0,0 +1,10 @@
'use strict';

const net = require('net');
const path = require('path');

const socket = net.connect(path.join(__dirname, 'xxx.sock'));

socket.once('connect', () => {
console.log('connect');
});
33 changes: 33 additions & 0 deletions example/client.js
@@ -0,0 +1,33 @@
'use strict';

const path = require('path');
const TsProxyClient = require('../lib/proxy_client');

async function main() {

const client = new TsProxyClient();

client.stdout.on('data', data => {
console.log(data.toString());
});

client.write({
seq: 1,
type: 'request',
command: 'open',
arguments: { file: path.join(__dirname, '../lib/protocol.js') },
});

client.write({
seq: 2,
type: 'request',
command: 'quickinfo',
arguments: {
file: path.join(__dirname, '../lib/protocol.js'),
line: 5,
offset: 7,
},
});
}

main();
33 changes: 33 additions & 0 deletions example/client2.js
@@ -0,0 +1,33 @@
'use strict';

const path = require('path');
const TsProxyClient = require('../lib/proxy_client');

async function main() {

const client = new TsProxyClient();

client.stdout.on('data', data => {
console.log(data.toString());
});

// client.write({
// seq: 1,
// type: 'request',
// command: 'open',
// arguments: { file: path.join(__dirname, '../lib/protocol.js') },
// });

client.write({
seq: 2,
type: 'request',
command: 'quickinfo',
arguments: {
file: path.join(__dirname, '../lib/protocol.js'),
line: 5,
offset: 7,
},
});
}

main();
47 changes: 47 additions & 0 deletions example/index.js
@@ -0,0 +1,47 @@
'use strict';

const path = require('path');
const cp = require('child_process');
const { PassThrough } = require('stream');
const { TrServerDecoder } = require('./lib/protocol');

const tsServerPath = path.join(path.dirname(require.resolve('typescript')), 'tsserver.js');

const proc = cp.fork(tsServerPath, [
'--useInferredProjectPerProjectRoot',
'--noGetErrOnBackgroundUpdate',
'--validateDefaultNpmLocation',
], {
silent: true,
});

const pass = new PassThrough();
const decoder = new TrServerDecoder();

proc.stdout.pipe(decoder);

decoder.on('message', msg => {
console.log(msg);
});

function encode(obj) {
return JSON.stringify(obj) + '\r\n';
}

proc.stdin.write(encode({
seq: 1,
type: 'request',
command: 'open',
arguments: { file: path.join(__dirname, 'lib/tsserver_decoder.js') },
}));

proc.stdin.write(encode({
seq: 2,
type: 'request',
command: 'quickinfo',
arguments: {
file: path.join(__dirname, 'lib/tsserver_decoder.js'),
line: 5,
offset: 7,
},
}));
20 changes: 20 additions & 0 deletions example/md5.js
@@ -0,0 +1,20 @@
'use strict';

const path = require('path');
const utils = require('../lib/utils');

const tsServerPath = path.join(path.dirname(require.resolve('typescript')), 'tsserver.js');
const args = [
'--useInferredProjectPerProjectRoot',
'--noGetErrOnBackgroundUpdate',
'--validateDefaultNpmLocation',
];
const tsServerForkOptions = {
silent: true,
};

console.log(utils.md5(JSON.stringify({
tsServerPath,
args,
tsServerForkOptions,
})));
20 changes: 20 additions & 0 deletions example/server.js
@@ -0,0 +1,20 @@
'use strict';

const path = require('path');
const TsProxyServer = require('../lib/proxy_server');

const tsServerPath = path.join(path.dirname(require.resolve('typescript')), 'tsserver.js');
const args = [
'--useInferredProjectPerProjectRoot',
'--noGetErrOnBackgroundUpdate',
'--validateDefaultNpmLocation',
];
const tsServerForkOptions = {
silent: true,
};

const server = new TsProxyServer({
tsServerPath,
args,
tsServerForkOptions,
});
42 changes: 42 additions & 0 deletions example/t.js
@@ -0,0 +1,42 @@
'use strict';

const path = require('path');
const ClusterTsServerProcess = require('../lib');

const tsServerPath = path.join(path.dirname(require.resolve('typescript')), 'tsserver.js');
const args = [
'--useInferredProjectPerProjectRoot',
'--noGetErrOnBackgroundUpdate',
'--validateDefaultNpmLocation',
];
const tsServerForkOptions = {
silent: true,
};

async function main() {
const proc = new ClusterTsServerProcess({ tsServerPath, args, tsServerForkOptions });
proc.stdout.on('data', data => {
console.log(data.toString());
});
await proc.ready();

proc.write({
seq: 1,
type: 'request',
command: 'open',
arguments: { file: path.join(__dirname, '../lib/protocol.js') },
});

proc.write({
seq: 2,
type: 'request',
command: 'quickinfo',
arguments: {
file: path.join(__dirname, '../lib/protocol.js'),
line: 5,
offset: 7,
},
});
}

main();
90 changes: 90 additions & 0 deletions example/test.js
@@ -0,0 +1,90 @@
'use strict';

const path = require('path');
const fs = require('fs');
const ClusterTsServerProcess = require('../lib');

const options = {
"tsServerPath": "/Users/gaoxiaochen/projj/gitlab.alipay-inc.com/cloud-ide/api-server/_extensions/kaitian.typescript-language-features-1.37.1-patch.12/node_modules/typescript/lib/tsserver.js",
"args": [
"--useInferredProjectPerProjectRoot",
"--enableTelemetry",
"--cancellationPipeName",
"/var/folders/q4/4nwl16wn32ndm69rzh1zyvhh0000gn/T/vscode-typescript501/e6ea02d3b441c4be4f4c/tscancellation-6ecdd2ccb16d73006bfb.tmp*",
"--locale", "zh-CN",
"--noGetErrOnBackgroundUpdate",
"--validateDefaultNpmLocation"
],
"tsServerForkOptions": {
"execArgv": []
}
}

const proc = new ClusterTsServerProcess(options);
proc.stdout.on('data', data => {
console.log(data.toString());
});

proc.write({
seq: 0,
type: 'request',
command: 'configure',
arguments: {
hostInfo: 'vscode',
preferences: {
providePrefixAndSuffixTextForRename: true,
allowRenameOfImportPath: true
}
}
});

proc.write({
seq: 1,
type: 'request',
command: 'compilerOptionsForInferredProjects',
arguments: {
options: {
module: 'commonjs',
target: 'es2016',
jsx: 'preserve',
allowJs: true,
allowSyntheticDefaultImports: true,
allowNonTsExtensions: true
}
}
});

proc.write({
"seq": 2,
"type": "request",
"command": "updateOpen",
"arguments": {
"changedFiles": [],
"closedFiles": [],
"openFiles": [{
"file": "/Users/gaoxiaochen/projj/gitlab.alipay-inc.com/cloud-ide/api-server/app/controller/home.ts",
"fileContent": fs.readFileSync('/Users/gaoxiaochen/projj/gitlab.alipay-inc.com/cloud-ide/api-server/app/controller/home.ts', 'utf8'),
"scriptKindName": "TS",
"projectRootPath": "/Users/gaoxiaochen/projj/gitlab.alipay-inc.com/cloud-ide/api-server"
}]
}
});

// setTimeout(() => {
proc.write({
seq: 3,
type: 'request',
command: 'geterr',
arguments: {
delay: 0,
files: ['/Users/gaoxiaochen/projj/gitlab.alipay-inc.com/cloud-ide/api-server/app/controller/home.ts']
}
});
// }, 5000);

proc.write({
seq: 4,
type: 'request',
command: 'getSupportedCodeFixes',
arguments: null
});

0 comments on commit 8cc0ffe

Please sign in to comment.