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
7 changes: 3 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 { buildTraceCommand } from './shell'

const readdir = promisify(readdirC)

Expand Down Expand Up @@ -128,9 +129,7 @@ async function runTrace(args?: unknown[]) {
return
}

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

log(fullCmd)

Expand All @@ -148,7 +147,7 @@ async function runTrace(args?: unknown[]) {
setStatusBarState('traceError', false)

log(`shell: ${process.env.SHELL}`)
const cmdProcess = spawn(fullCmd, [], { cwd: newProjectPath, shell: process.env.SHELL })
const cmdProcess = spawn(fullCmd, [], { cwd: newProjectPath, shell: process.env.SHELL || true })

let err = ''
cmdProcess.stderr.on('data', data => err += data.toString())
Expand Down
15 changes: 15 additions & 0 deletions src/shell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as process from 'node:process'

type ShellPlatform = NodeJS.Platform
const traceDirVariable = '$' + '{traceDir}'

export function quoteShellArg(arg: string, platform: ShellPlatform = process.platform): string {
if (platform === 'win32')
return `"${arg.replace(/"/g, '""')}"`

return `'${arg.replace(/'/g, '\'\\\'\'')}'`
}

export function buildTraceCommand(traceCmd: string, traceDir: string, platform: ShellPlatform = process.platform): string {
return traceCmd.split(traceDirVariable).join(quoteShellArg(traceDir, platform))
}
25 changes: 25 additions & 0 deletions test/shell.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, it } from 'vitest'
import { buildTraceCommand, quoteShellArg } from '../src/shell'

describe('shell command helpers', () => {
it('quotes trace directories for POSIX shells', () => {
expect(quoteShellArg('/tmp/type traces')).toBe('\'/tmp/type traces\'')
expect(quoteShellArg('/tmp/owner\'s traces')).toBe('\'/tmp/owner\'\\\'\'s traces\'')
})

it('quotes trace directories for Windows shells', () => {
expect(quoteShellArg('c:\\Users\\me\\type traces', 'win32')).toBe('"c:\\Users\\me\\type traces"')
})

it('builds the trace command without changing directories in the shell command', () => {
const traceDirVariable = '$' + '{traceDir}'
const cmd = buildTraceCommand(
`npx tsc --noEmit --generateTrace ${traceDirVariable}`,
'c:\\Users\\me\\AppData\\Roaming\\Code\\User\\globalStorage\\tsperf.tracer\\project\\traces',
'win32',
)

expect(cmd).toBe('npx tsc --noEmit --generateTrace "c:\\Users\\me\\AppData\\Roaming\\Code\\User\\globalStorage\\tsperf.tracer\\project\\traces"')
expect(cmd).not.toContain('cd ')
})
})