An open API service indexing awesome lists of open source software.

https://github.com/shiyangzhaoa/tsserver

A lite wrapper around tsserver
https://github.com/shiyangzhaoa/tsserver

tsserver typescript

Last synced: 2 months ago
JSON representation

A lite wrapper around tsserver

Awesome Lists containing this project

README

        

# TSServer wrapper

A lite wrapper around tsserver


tsserver



Total Downloads
Latest Release
License

## Install

```shell
yarn add tsserver-lite
```

## Example

For example, your project structure:

```shell
├── src
│ └── test.ts
└──── xxx
```

```ts
// src/test.ts
export const number = 123;
export const string = 'abc';
```

How to use:
```mjs
// index.mjs
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';

import { client } from 'tsserver-lite';

const updateTSFile = async (info) => {
const fileInfo = Object.assign(
{
changedFiles: [],
closedFiles: [],
openFiles: [],
},
info,
);

await client.execute('updateOpen', fileInfo);
};

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const filePath = path.resolve(__dirname, './test.ts');

const main = async () => {
client.startService();

const openFile = {
file: filePath,
fileContent: fs.readFileSync(filePath, 'utf-8'),
projectRootPath: path.resolve(__dirname, '../'),
scriptKindName: 'ts',
};

try {
await updateTSFile({
openFiles: [openFile]
});

const result = await client.execute(
'quickinfo',
{
file: filePath,
line: 1,
offset: 15,
},
);
if (result.type === 'response') {
console.log('result---', result);
}
} catch (err) {
console.log(`tsserver error: ${err.message}`);
}

client.closeService();
};

main().catch(err => {
console.error(err);
process.exit(1);
});
```

Try run script:

```shell
node index.mjs
```

You will see these logs:

```ts
result: {
seq: 0,
type: 'response',
command: 'quickinfo',
request_seq: 3,
success: true,
body: {
kind: 'const',
kindModifiers: 'export',
start: { line: 1, offset: 14 },
end: { line: 1, offset: 20 },
displayString: 'const number: 123',
documentation: '',
tags: []
}
}
```