/
index.test.js
147 lines (127 loc) · 3.72 KB
/
index.test.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
'use strict';
const fs = require('fs');
const path = require('path');
const uuid = require('uuid');
const assert = require('assert');
const { pipeline } = require('stream');
const sleep = require('mz-modules/sleep');
const awaitEvent = require('await-event');
const rimraf = require('mz-modules/rimraf');
const ClusterTsServerProcess = require('../lib');
const { TsServerDecoder } = require('../lib/protocol');
const tsServerPath = path.join(path.dirname(require.resolve('typescript')), 'tsserver.js');
const tsServerForkOptions = {
silent: true,
};
const logFile = path.join(__dirname, '.tmp', 'logs/tsserver.log');
describe('test/index.test.js', () => {
before(async () => {
await rimraf(path.join(__dirname, '.tmp'));
});
it('should work', async () => {
const options = {
tsServerPath,
args: [
'--useInferredProjectPerProjectRoot',
'--enableTelemetry',
'--cancellationPipeName',
path.join(__dirname, '.tmp/tscancellation', uuid.v4() + '.tmp*'),
'--locale', 'zh-CN',
'--noGetErrOnBackgroundUpdate',
'--validateDefaultNpmLocation',
],
tsServerForkOptions,
logFile,
logLevel: 'DEBUG',
};
const proc = new ClusterTsServerProcess(options);
assert(proc && proc.requestCanceller);
const decoder = new TsServerDecoder();
pipeline(proc.stdout, decoder, err => {
if (err) {
console.error(err);
}
});
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: path.join(__dirname, 'fixtures/ts-app/index.ts'),
fileContent: fs.readFileSync(path.join(__dirname, 'fixtures/ts-app/index.ts'), 'utf8'),
scriptKindName: 'TS',
projectRootPath: path.join(__dirname, 'fixtures/ts-app'),
}],
},
});
proc.write({
seq: 3,
type: 'request',
command: 'geterr',
arguments: {
delay: 0,
files: [ path.join(__dirname, 'fixtures/ts-app/index.ts') ],
},
});
proc.write({
seq: 4,
type: 'request',
command: 'getSupportedCodeFixes',
arguments: null,
});
const reqSet = new Set([ 0, 1, 2, 3, 4 ]);
decoder.on('message', msg => {
if (msg.type === 'response') {
console.log('response --------------->', msg);
assert(reqSet.has(msg.request_seq));
reqSet.delete(msg.request_seq);
} else if (msg.type === 'event' && msg.event === 'requestCompleted') {
console.log('requestCompleted -------------->', msg);
assert(reqSet.has(msg.body.request_seq));
reqSet.delete(msg.body.request_seq);
}
if (reqSet.size === 0) {
decoder.emit('finished');
}
});
await awaitEvent(decoder, 'finished');
console.log('----------------> kill');
proc.kill();
await proc.await('exit');
await sleep(100);
assert(fs.existsSync(logFile));
});
it('should support export default', () => {
assert(ClusterTsServerProcess === ClusterTsServerProcess.default);
});
});