-
Notifications
You must be signed in to change notification settings - Fork 63
/
getSuggestions.js
executable file
·99 lines (94 loc) · 2.37 KB
/
getSuggestions.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
#!/usr/bin/env node
const cp = require('child_process')
const fs = require('fs').promises
const path = require('path')
async function getFiles(dir) {
let files = await fs.readdir(dir)
files = await Promise.all(
files.map(async file => {
if (
file === 'node_modules' ||
file === 'dist' ||
file === 'umd' ||
file === 'build' ||
file === 'coverage' ||
file === 'templates'
) {
return null
}
const filePath = path.resolve(path.join(dir, file))
const stats = await fs.stat(filePath)
if (stats.isDirectory()) {
return getFiles(filePath)
}
if (
!(
file.endsWith('.ts') ||
file.endsWith('.tsx') ||
file.endsWith('.js') ||
file.endsWith('.jsx')
)
) {
return null
}
if (stats.isFile()) {
return filePath
}
return null
}),
)
return files.filter(Boolean).flat()
}
// eslint-disable-next-line @typescript-eslint/no-floating-promises
;(async () => {
const child = cp.spawn('yarn', ['tsserver'])
const files = await getFiles('.')
files.forEach((file, idx) => {
child.stdin.write(
`${JSON.stringify({
seq: idx * 2,
type: 'request',
command: 'open',
arguments: { file },
})}\n`,
)
child.stdin.write(
`${JSON.stringify({
seq: idx * 2 + 1,
type: 'request',
command: 'suggestionDiagnosticsSync',
arguments: { file },
})}\n`,
)
})
child.stdout.on('data', data => {
const lines = data
.toString()
.split(/\r?\n/)
.filter(line => line.startsWith('{'))
lines.forEach(line => {
let response
try {
response = JSON.parse(line)
} catch (error) {
console.error(data.toString())
}
if (response.request_seq && response.body?.length) {
const warnings = []
response.body.forEach(contents => {
warnings.push(
`${contents.start.line}:${contents.start.offset}-${contents.end.line}:${contents.end.offset} ${contents.text}`,
)
})
if (warnings.length) {
console.log(files[(response.request_seq - 1) / 2])
warnings.forEach(warning => {
console.log(warning)
})
console.log()
}
}
})
})
child.stdin.end()
})()