-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.ts
295 lines (276 loc) · 9.07 KB
/
main.ts
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env node
import * as child_process from 'child_process'
import * as fs from 'fs'
import { EOL } from 'os'
import * as path from 'path'
import * as url from 'url'
import * as ts from 'typescript'
import packageJson from '../package.json'
import {
GlobalCache,
mainCommand,
MultiProjectOptions,
ProjectOptions,
} from './CommandLineOptions'
import { inferTsconfig } from './inferTsconfig'
import { ProjectIndexer } from './ProjectIndexer'
import * as scip from './scip'
export function main(): void {
mainCommand((projects, options) => indexCommand(projects, options)).parse(
process.argv
)
return
}
export function indexCommand(
projects: string[],
options: MultiProjectOptions
): void {
if (options.yarnWorkspaces) {
projects.push(...listYarnWorkspaces(options.cwd, 'tryYarn1'))
} else if (options.yarnBerryWorkspaces) {
projects.push(...listYarnWorkspaces(options.cwd, 'yarn2Plus'))
} else if (options.pnpmWorkspaces) {
projects.push(...listPnpmWorkspaces(options.cwd))
} else if (projects.length === 0) {
projects.push(options.cwd)
}
options.cwd = makeAbsolutePath(process.cwd(), options.cwd)
options.output = makeAbsolutePath(options.cwd, options.output)
if (!options.indexedProjects) {
options.indexedProjects = new Set()
}
const output = fs.openSync(options.output, 'w')
let documentCount = 0
const writeIndex = (index: scip.scip.Index): void => {
documentCount += index.documents.length
fs.writeSync(output, index.serializeBinary())
}
const cache: GlobalCache = {
sources: new Map(),
parsedCommandLines: new Map(),
}
try {
writeIndex(
new scip.scip.Index({
metadata: new scip.scip.Metadata({
project_root: url.pathToFileURL(options.cwd).toString(),
text_document_encoding: scip.scip.TextEncoding.UTF8,
tool_info: new scip.scip.ToolInfo({
name: 'scip-typescript',
version: packageJson.version,
arguments: [],
}),
}),
})
)
// NOTE: we may want index these projects in parallel in the future.
// We need to be careful about which order we index the projects because
// they can have dependencies.
for (const projectRoot of projects) {
const projectDisplayName = projectRoot === '.' ? options.cwd : projectRoot
indexSingleProject(
{
...options,
projectRoot,
projectDisplayName,
writeIndex,
},
cache
)
}
} finally {
fs.close(output)
if (documentCount > 0) {
console.log(`done ${options.output}`)
} else {
process.exitCode = 1
fs.rmSync(options.output)
const prettyProjects = JSON.stringify(projects)
console.log(
`error: no files got indexed. To fix this problem, make sure that the TypeScript projects ${prettyProjects} contain input files or reference other projects.`
)
}
}
}
function makeAbsolutePath(cwd: string, relativeOrAbsolutePath: string): string {
if (path.isAbsolute(relativeOrAbsolutePath)) {
return relativeOrAbsolutePath
}
return path.resolve(cwd, relativeOrAbsolutePath)
}
function indexSingleProject(options: ProjectOptions, cache: GlobalCache): void {
if (options.indexedProjects.has(options.projectRoot)) {
return
}
options.indexedProjects.add(options.projectRoot)
let config = ts.parseCommandLine(
['-p', options.projectRoot],
(relativePath: string) => path.resolve(options.projectRoot, relativePath)
)
let tsconfigFileName: string | undefined
if (config.options.project) {
const projectPath = path.resolve(config.options.project)
if (ts.sys.directoryExists(projectPath)) {
tsconfigFileName = path.join(projectPath, 'tsconfig.json')
} else {
tsconfigFileName = projectPath
}
if (!ts.sys.fileExists(tsconfigFileName)) {
if (options.inferTsconfig) {
fs.writeFileSync(tsconfigFileName, inferTsconfig(projectPath))
} else {
console.error(`- ${options.projectDisplayName} (missing tsconfig.json)`)
return
}
}
const loadedConfig = loadConfigFile(tsconfigFileName)
if (loadedConfig !== undefined) {
config = loadedConfig
}
}
for (const projectReference of config.projectReferences || []) {
indexSingleProject(
{
...options,
projectRoot: projectReference.path,
projectDisplayName: projectReference.path,
},
cache
)
}
if (config.fileNames.length > 0) {
new ProjectIndexer(config, options, cache).index()
}
}
if (require.main === module) {
main()
}
function loadConfigFile(file: string): ts.ParsedCommandLine | undefined {
const absolute = path.resolve(file)
const readResult = ts.readConfigFile(absolute, path => ts.sys.readFile(path))
if (readResult.error) {
throw new Error(
ts.formatDiagnostics([readResult.error], ts.createCompilerHost({}))
)
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const config = readResult.config
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (config.compilerOptions !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
config.compilerOptions = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
...config.compilerOptions,
...defaultCompilerOptions(file),
}
}
const basePath = path.dirname(absolute)
const result = ts.parseJsonConfigFileContent(config, ts.sys, basePath)
const errors: ts.Diagnostic[] = []
for (const error of result.errors) {
if (error.code === 18003) {
// Ignore errors about missing 'input' fields, example:
// > TS18003: No inputs were found in config file 'tsconfig.json'. Specified 'include' paths were '[]' and 'exclude' paths were '["out","node_modules","dist"]'.
// The reason we ignore this error here is because we report the same
// error at a higher-level. It's common to hit on a single TypeScript
// project with no sources when using the --yarnWorkspaces option.
// Instead of failing fast at that single project, we only report this
// error if all projects have no files.
continue
}
errors.push(error)
}
if (errors.length > 0) {
console.log(ts.formatDiagnostics(errors, ts.createCompilerHost({})))
return undefined
}
return result
}
function defaultCompilerOptions(configFileName?: string): ts.CompilerOptions {
const options: ts.CompilerOptions =
// Not a typo, jsconfig.json is a thing https://sourcegraph.com/search?q=context:global+file:jsconfig.json&patternType=literal
configFileName && path.basename(configFileName) === 'jsconfig.json'
? {
allowJs: true,
maxNodeModuleJsDepth: 2,
allowSyntheticDefaultImports: true,
skipLibCheck: true,
noEmit: true,
}
: {}
return options
}
function listPnpmWorkspaces(directory: string): string[] {
/**
* Returns the list of projects formatted as:
* '/Users/user/sourcegraph/client/web:@sourcegraph/[email protected]:PRIVATE',
*
* See https://pnpm.io/id/cli/list#--depth-number
*/
const output = child_process.execSync(
'pnpm ls -r --depth -1 --long --parseable',
{
cwd: directory,
encoding: 'utf-8',
maxBuffer: 1024 * 1024 * 5, // 5MB
}
)
return output
.split(EOL)
.filter(project => project.includes(':'))
.map(project => project.split(':')[0])
}
function listYarnWorkspaces(
directory: string,
yarnVersion: 'tryYarn1' | 'yarn2Plus'
): string[] {
const runYarn = (cmd: string): string =>
child_process.execSync(cmd, {
cwd: directory,
encoding: 'utf-8',
maxBuffer: 1024 * 1024 * 5, // 5MB
})
const result: string[] = []
const yarn1WorkspaceInfo = (): void => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const json = JSON.parse(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
JSON.parse(runYarn('yarn --silent --json workspaces info')).data
)
for (const key of Object.keys(json)) {
const location = 'location'
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (json[key][location] !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
result.push(path.join(directory, json[key][location]))
}
}
}
const yarn2PlusWorkspaceInfo = (): void => {
const jsonLines = runYarn('yarn --json workspaces list').split(
/\r?\n|\r|\n/g
)
for (let line of jsonLines) {
line = line.trim()
if (line.length === 0) {
continue
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const json = JSON.parse(line)
if ('location' in json) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
result.push(path.join(directory, json.location))
}
}
}
if (yarnVersion === 'tryYarn1') {
try {
yarn2PlusWorkspaceInfo()
} catch {
yarn1WorkspaceInfo()
}
} else {
yarn2PlusWorkspaceInfo()
}
return result
}