Skip to content
Open
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
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@
"command": "tsperf.tracer.runTrace",
"category": "Tracer"
},
{
"title": "Trace current file",
"command": "tsperf.tracer.runTraceActiveFile",
"category": "Tracer"
},
{
"title": "Send Trace to Trace Viewer",
"command": "tsperf.tracer.sendTrace",
Expand Down
3 changes: 3 additions & 0 deletions scripts/generate-contributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const commandRecord: Record<CommandId, Command> = {
explorerContext: 'resourceFilename =~ /./',
},
},
'tsperf.tracer.runTraceActiveFile': {
title: 'Trace current file',
},
'tsperf.tracer.sendTrace': {
title: 'Send Trace to Trace Viewer',
when: {
Expand Down
28 changes: 24 additions & 4 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { addTraceFile, getWorkspacePath, openTerminal, openTraceDirectoryExterna
import { addTraceDiagnostics, clearTaceDiagnostics } from './traceDiagnostics'
import { setStatusBarState } from './statusBar'
import { afterWatches, projectPath, saveName, state, traceFiles, traceRunning } from './appState'
import { traceSaveNameForFile } from './traceCommand'

const readdir = promisify(readdirC)

Expand All @@ -21,6 +22,7 @@ const commandHandlers: Record<
(context: vscode.ExtensionContext) => (...args: any[]) => void
> = {
'tsperf.tracer.runTrace': () => (...args: unknown[]) => runTrace(args),
'tsperf.tracer.runTraceActiveFile': () => () => runTraceActiveFile(),
'tsperf.tracer.openInBrowser': (context: vscode.ExtensionContext) => () => prepareWebView(context),
'tsperf.tracer.gotoTracePosition': (context: vscode.ExtensionContext) => () => gotoTracePosition(context),
'tsperf.tracer.sendTrace': () => (event: unknown) => {
Expand All @@ -35,6 +37,20 @@ const commandHandlers: Record<
'tsperf.tracer.openTraceDirExternal': () => () => openTraceDirectoryExternal(),
} as const

async function runTraceActiveFile() {
const editor = vscode.window.activeTextEditor
if (!editor || editor.document.uri.scheme !== 'file') {
vscode.window.showWarningMessage('Open a TypeScript file before running a mini trace')
return
}

const workspacePath = state.workspacePath.value || getWorkspacePath()
await runTrace([], {
cwd: workspacePath,
saveName: traceSaveNameForFile(workspacePath, editor.document.uri.fsPath),
})
}

async function sendTrace(dirName: string, fileName: string) {
const fullFileName = join(dirName, fileName)

Expand Down Expand Up @@ -97,7 +113,7 @@ function gotoTracePosition(context: vscode.ExtensionContext) {
showTree('', relativePath, startOffset - (editor.document.getText()[startOffset + 1] === '\n' ? 0 : 1))
}

async function runTrace(args?: unknown[]) {
async function runTrace(args?: unknown[], options?: { cwd?: string, saveName?: string }) {
const workspacePath = state.workspacePath.value
const { traceCmd } = getCurrentConfig()

Expand All @@ -114,7 +130,10 @@ async function runTrace(args?: unknown[]) {
}
}

if (dirName) {
if (options?.saveName) {
saveName.value = options.saveName
}
else if (dirName) {
log(`dirName: ${dirName}`)
saveName.value = relative(workspacePath, dirName)
}
Expand All @@ -128,13 +147,14 @@ async function runTrace(args?: unknown[]) {
return
}

const traceCwd = options?.cwd ?? newDirName ?? workspacePath
const quotedTraceDir = `'${traceDir}'`
// eslint-disable-next-line no-template-curly-in-string
const fullCmd = `(cd '${newDirName ?? workspacePath}'; ${traceCmd.replace('${traceDir}', quotedTraceDir)})`
const fullCmd = `(cd '${traceCwd}'; ${traceCmd.replace('${traceDir}', quotedTraceDir)})`

log(fullCmd)

const newProjectPath = newDirName ?? projectPath.value
const newProjectPath = traceCwd || projectPath.value
if (!newProjectPath) {
vscode.window.showErrorMessage('could not get project path from workspace folders')
return
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const commandIds = [
'tsperf.tracer.gotoTracePosition',
'tsperf.tracer.openInBrowser',
'tsperf.tracer.runTrace',
'tsperf.tracer.runTraceActiveFile',
'tsperf.tracer.sendTrace',
'tsperf.tracer.openTerminal',
'tsperf.tracer.openTraceDirExternal',
Expand Down
7 changes: 7 additions & 0 deletions src/traceCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { extname, relative } from 'node:path'

export function traceSaveNameForFile(workspacePath: string, filePath: string): string {
const relativeFile = relative(workspacePath, filePath)
const extension = extname(relativeFile)
return extension ? relativeFile.slice(0, -extension.length) : relativeFile
}
12 changes: 12 additions & 0 deletions test/traceCommand.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, expect, it } from 'vitest'
import { traceSaveNameForFile } from '../src/traceCommand'

describe('traceSaveNameForFile', () => {
it('uses the workspace-relative file path without extension', () => {
expect(traceSaveNameForFile('/repo', '/repo/src/features/example.ts')).toBe('src/features/example')
})

it('preserves files without extensions', () => {
expect(traceSaveNameForFile('/repo', '/repo/tsconfig')).toBe('tsconfig')
})
})