Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"homepage": "https://github.com/sourcegraph/lsif-typescript#readme",
"dependencies": {
"commander": "^9.1.0",
"commander": "^9.2.0",
"google-protobuf": "^3.20.0",
"pretty-ms": "^7.0.1",
"progress": "^2.0.3",
Expand Down
37 changes: 37 additions & 0 deletions src/CommandLineOptions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { test } from 'uvu'
import * as assert from 'uvu/assert'

import { mainCommand, MultiProjectOptions } from './CommandLineOptions'

function checkIndexParser(
args: string[],
expectedOptions: Partial<MultiProjectOptions>,
Copy link
Contributor

@varungandhi-src varungandhi-src Apr 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, I didn't know this was a thing you could do so easily in TypeScript. (The Partial<> type)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's very handy for cases like this! TypeScript includes several utility types https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype but you can go a bit overboard with them so use with care.

expectedProjects?: string[]
): void {
test(args.join(' '), () => {
let isAssertionTriggered = false
const actualArguments = ['node', 'lsif-typescript.js', 'index', ...args]
mainCommand((projects, options) => {
assert.equal(options, { ...options, ...expectedOptions })
if (expectedProjects) {
assert.equal(projects, expectedProjects)
}
isAssertionTriggered = true
}).parse(actualArguments)
assert.ok(isAssertionTriggered)
})
}

// defaults
checkIndexParser([], {
progressBar: true,
cwd: process.cwd(),
inferTsconfig: false,
output: 'dump.lsif-typed',
yarnWorkspaces: false,
})

checkIndexParser(['--cwd', 'qux'], { cwd: 'qux' })
checkIndexParser(['--yarn-workspaces'], { yarnWorkspaces: true })
checkIndexParser(['--infer-tsconfig'], { inferTsconfig: true })
checkIndexParser(['--no-progress-bar'], { progressBar: false })
53 changes: 53 additions & 0 deletions src/CommandLineOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Command } from 'commander'

import packageJson from '../package.json'

import * as lsif from './lsif'

/** Configuration options to index a multi-project workspace. */
export interface MultiProjectOptions {
inferTsconfig: boolean
progressBar: boolean
yarnWorkspaces: boolean
cwd: string
output: string
indexedProjects: Set<string>
}

/** Configuration options to index a single TypeScript project. */
export interface ProjectOptions extends MultiProjectOptions {
projectRoot: string
projectDisplayName: string
writeIndex: (index: lsif.lib.codeintel.lsiftyped.Index) => void
}

export function mainCommand(
indexAction: (projects: string[], otpions: MultiProjectOptions) => void
): Command {
const command = new Command()
command
.name('lsif-typescript')
.version(packageJson.version)
.description(
'LSIF indexer for TypeScript and JavaScript\nFor usage examples, see https://github.com/sourcegraph/lsif-typescript/blob/main/README.md'
)
command
.command('index')
.option('--cwd <path>', 'the working directory', process.cwd())
.option('--yarn-workspaces', 'whether to index all yarn workspaces', false)
.option(
'--infer-tsconfig',
"whether to infer the tsconfig.json file, if it's missing",
false
)
.option('--output <path>', 'path to the output file', 'dump.lsif-typed')
.option('--no-progress-bar', 'whether to disable the progress bar')
.argument('[projects...]')
.action((parsedProjects, parsedOptions) => {
indexAction(
parsedProjects as string[],
parsedOptions as MultiProjectOptions
)
})
return command
}
2 changes: 1 addition & 1 deletion src/ProjectIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import prettyMilliseconds from 'pretty-ms'
import ProgressBar from 'progress'
import * as ts from 'typescript'

import { ProjectOptions } from './CommandLineOptions'
import { FileIndexer } from './FileIndexer'
import { Input } from './Input'
import * as lsif from './lsif'
import { LsifSymbol } from './LsifSymbol'
import { ProjectOptions } from './main'
import { Packages } from './Packages'

export class ProjectIndexer {
Expand Down
4 changes: 2 additions & 2 deletions src/inferTsConfig.test.ts → src/inferTsconfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import * as process from 'process'

import { test } from 'uvu'

import { allowJsConfig, inferTsConfig, noJsConfig } from './inferTsConfig'
import { allowJsConfig, inferTsconfig, noJsConfig } from './inferTsconfig'

const inputDirectory = join(process.cwd(), 'snapshots', 'inferTsConfig')

function checkDirectory(name: string, expected: string): void {
test(name, () => {
const directory = join(inputDirectory, name)
const obtained = inferTsConfig(directory)
const obtained = inferTsconfig(directory)
if (obtained !== expected) {
throw new Error(`expected ('${expected}') != obtained ('${obtained}')`)
}
Expand Down
4 changes: 2 additions & 2 deletions src/inferTsConfig.ts → src/inferTsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'fs'
import * as path from 'path'

/**
* To limit the risk of making the `inferTsConfig` run for a very long time, we
* To limit the risk of making the `inferTsconfig` run for a very long time, we
* stop the file traversal after visiting this number of files.
*/
const maximumFileTraversalCount = 1_000
Expand All @@ -19,7 +19,7 @@ export const noJsConfig = '{}'
* If the directory contains at least one `*.{ts,tsx}` file then the config will be empty (`{}`).
* If the directory doesn't contains one `*.{ts,tsx}` file then the config will
*/
export function inferTsConfig(projectPath: string): string {
export function inferTsconfig(projectPath: string): string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I apologize for mixing this unrelated diff into the PR. It's quite painful to correct the capitalization of files on macOS and I didn't feel like doing it again to have a cleaner PR

let hasTypeScriptFile = false
let hasJavaScriptFile = false
let visitedFileCount = 0
Expand Down
54 changes: 10 additions & 44 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,25 @@ import * as fs from 'fs'
import * as path from 'path'
import * as url from 'url'

import { Command } from 'commander'
import * as ts from 'typescript'

import packageJson from '../package.json'

import { inferTsConfig } from './inferTsConfig'
import {
mainCommand,
MultiProjectOptions,
ProjectOptions,
} from './CommandLineOptions'
import { inferTsconfig } from './inferTsconfig'
import * as lsif from './lsif'
import { ProjectIndexer } from './ProjectIndexer'

export const lsiftyped = lsif.lib.codeintel.lsiftyped

/** Configuration options to index a multi-project workspace. */
export interface MultiProjectOptions {
inferTsconfig: boolean
progressBar: boolean
yarnWorkspaces: boolean
cwd: string
output: string
indexedProjects: Set<string>
}

/** Configuration options to index a single TypeScript project. */
export interface ProjectOptions extends MultiProjectOptions {
projectRoot: string
projectDisplayName: string
writeIndex: (index: lsif.lib.codeintel.lsiftyped.Index) => void
}

export function main(): void {
const program = new Command()
program
.name('lsif-typescript')
.version(packageJson.version)
.description('LSIF indexer for TypeScript and JavaScript')
program
.command('index')
.option('--cwd', 'the working directory', process.cwd())
.option('--yarn-workspaces', 'whether to index all yarn workspaces', false)
.option(
'--infer-tsconfig',
"whether to infer the tsconfig.json file, if it's missing",
false
)
.option('--output', 'path to the output file', 'dump.lsif-typed')
.option('--no-progress-bar', 'whether to disable the progress bar')
.argument('[projects...]')
.action((parsedProjects, parsedOptions) => {
indexCommand(
parsedProjects as string[],
parsedOptions as MultiProjectOptions
)
})
program.parse(process.argv)
mainCommand((projects, options) => indexCommand(projects, options)).parse(
process.argv
)
return
}

Expand Down Expand Up @@ -147,7 +113,7 @@ function indexSingleProject(options: ProjectOptions): void {
}
if (!ts.sys.fileExists(tsconfigFileName)) {
if (options.inferTsconfig) {
fs.writeFileSync(tsconfigFileName, inferTsConfig(projectPath))
fs.writeFileSync(tsconfigFileName, inferTsconfig(projectPath))
} else {
console.error(`- ${options.projectDisplayName} (missing tsconfig.json)`)
return
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -891,10 +891,10 @@ color-name@~1.1.4:
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

commander@^9.1.0:
version "9.1.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-9.1.0.tgz#a6b263b2327f2e188c6402c42623327909f2dbec"
integrity sha512-i0/MaqBtdbnJ4XQs4Pmyb+oFQl+q0lsAmokVUH92SlSw4fkeAcG3bVon+Qt7hmtF+u3Het6o4VgrcY3qAoEB6w==
commander@^9.2.0:
version "9.2.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-9.2.0.tgz#6e21014b2ed90d8b7c9647230d8b7a94a4a419a9"
integrity sha512-e2i4wANQiSXgnrBlIatyHtP1odfUp0BbV5Y5nEGbxtIrStkEOAAzCUirvLBNXHLr7kwLvJl6V+4V3XV9x7Wd9w==

comment-parser@^0.7.6:
version "0.7.6"
Expand Down