Skip to content

Commit da523cf

Browse files
committed
feat(cr): cr
1 parent 3082a6e commit da523cf

File tree

6 files changed

+54
-46
lines changed

6 files changed

+54
-46
lines changed

src/cli.mts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ import { socketPackageLink } from './utils/terminal-link.mts'
2828

2929
const __filename = fileURLToPath(import.meta.url)
3030

31+
// Capture CLI start time at module level for global error handlers.
32+
const cliStartTime = Date.now()
33+
3134
void (async () => {
3235
// Track CLI start for telemetry.
33-
const cliStartTime = await trackCliStart(process.argv)
36+
await trackCliStart(process.argv)
3437

3538
const registryUrl = lookupRegistryUrl()
3639
await updateNotifier({
@@ -129,7 +132,7 @@ void (async () => {
129132
console.error('Fatal error:', err)
130133

131134
// Track CLI error for fatal exceptions.
132-
await trackCliError(process.argv, Date.now(), err, 1)
135+
await trackCliError(process.argv, cliStartTime, err, 1)
133136

134137
// Finalize telemetry before fatal exit.
135138
await finalizeTelemetry()
@@ -143,7 +146,7 @@ process.on('uncaughtException', async err => {
143146
console.error('Uncaught exception:', err)
144147

145148
// Track CLI error for uncaught exception.
146-
await trackCliError(process.argv, Date.now(), err, 1)
149+
await trackCliError(process.argv, cliStartTime, err, 1)
147150

148151
// Finalize telemetry before exit.
149152
await finalizeTelemetry()
@@ -158,7 +161,7 @@ process.on('unhandledRejection', async (reason, promise) => {
158161

159162
// Track CLI error for unhandled rejection.
160163
const error = reason instanceof Error ? reason : new Error(String(reason))
161-
await trackCliError(process.argv, Date.now(), error, 1)
164+
await trackCliError(process.argv, cliStartTime, error, 1)
162165

163166
// Finalize telemetry before exit.
164167
await finalizeTelemetry()

src/commands/npm/cmd-npm.mts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,17 @@ async function run(
102102
// See https://nodejs.org/api/child_process.html#event-exit.
103103
spawnPromise.process.on(
104104
'exit',
105-
async (code: number | null, signalName: NodeJS.Signals | null) => {
106-
// Track subprocess exit and flush telemetry.
107-
await trackSubprocessExit(NPM, subprocessStartTime, code)
108-
109-
if (signalName) {
110-
process.kill(process.pid, signalName)
111-
} else if (typeof code === 'number') {
112-
// eslint-disable-next-line n/no-process-exit
113-
process.exit(code)
114-
}
105+
(code: number | null, signalName: NodeJS.Signals | null) => {
106+
// Track subprocess exit and flush telemetry before exiting.
107+
// Use .then() to ensure telemetry completes before process.exit().
108+
void trackSubprocessExit(NPM, subprocessStartTime, code).then(() => {
109+
if (signalName) {
110+
process.kill(process.pid, signalName)
111+
} else if (typeof code === 'number') {
112+
// eslint-disable-next-line n/no-process-exit
113+
process.exit(code)
114+
}
115+
})
115116
},
116117
)
117118

src/commands/npx/cmd-npx.mts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,17 @@ async function run(
8787
// See https://nodejs.org/api/child_process.html#event-exit.
8888
spawnPromise.process.on(
8989
'exit',
90-
async (code: number | null, signalName: NodeJS.Signals | null) => {
91-
// Track subprocess exit and flush telemetry.
92-
await trackSubprocessExit(NPX, subprocessStartTime, code)
93-
94-
if (signalName) {
95-
process.kill(process.pid, signalName)
96-
} else if (typeof code === 'number') {
97-
// eslint-disable-next-line n/no-process-exit
98-
process.exit(code)
99-
}
90+
(code: number | null, signalName: NodeJS.Signals | null) => {
91+
// Track subprocess exit and flush telemetry before exiting.
92+
// Use .then() to ensure telemetry completes before process.exit().
93+
void trackSubprocessExit(NPX, subprocessStartTime, code).then(() => {
94+
if (signalName) {
95+
process.kill(process.pid, signalName)
96+
} else if (typeof code === 'number') {
97+
// eslint-disable-next-line n/no-process-exit
98+
process.exit(code)
99+
}
100+
})
100101
},
101102
)
102103

src/commands/pnpm/cmd-pnpm.mts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,17 @@ async function run(
9696
// See https://nodejs.org/api/child_process.html#event-exit.
9797
spawnPromise.process.on(
9898
'exit',
99-
async (code: number | null, signalName: NodeJS.Signals | null) => {
100-
// Track subprocess exit and flush telemetry.
101-
await trackSubprocessExit(PNPM, subprocessStartTime, code)
102-
103-
if (signalName) {
104-
process.kill(process.pid, signalName)
105-
} else if (typeof code === 'number') {
106-
// eslint-disable-next-line n/no-process-exit
107-
process.exit(code)
108-
}
99+
(code: number | null, signalName: NodeJS.Signals | null) => {
100+
// Track subprocess exit and flush telemetry before exiting.
101+
// Use .then() to ensure telemetry completes before process.exit().
102+
void trackSubprocessExit(PNPM, subprocessStartTime, code).then(() => {
103+
if (signalName) {
104+
process.kill(process.pid, signalName)
105+
} else if (typeof code === 'number') {
106+
// eslint-disable-next-line n/no-process-exit
107+
process.exit(code)
108+
}
109+
})
109110
},
110111
)
111112

src/commands/yarn/cmd-yarn.mts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,17 @@ async function run(
9696
// See https://nodejs.org/api/child_process.html#event-exit.
9797
spawnPromise.process.on(
9898
'exit',
99-
async (code: number | null, signalName: NodeJS.Signals | null) => {
100-
// Track subprocess exit and flush telemetry.
101-
await trackSubprocessExit(YARN, subprocessStartTime, code)
102-
103-
if (signalName) {
104-
process.kill(process.pid, signalName)
105-
} else if (typeof code === 'number') {
106-
// eslint-disable-next-line n/no-process-exit
107-
process.exit(code)
108-
}
99+
(code: number | null, signalName: NodeJS.Signals | null) => {
100+
// Track subprocess exit and flush telemetry before exiting.
101+
// Use .then() to ensure telemetry completes before process.exit().
102+
void trackSubprocessExit(YARN, subprocessStartTime, code).then(() => {
103+
if (signalName) {
104+
process.kill(process.pid, signalName)
105+
} else if (typeof code === 'number') {
106+
// eslint-disable-next-line n/no-process-exit
107+
process.exit(code)
108+
}
109+
})
109110
},
110111
)
111112

src/utils/telemetry/integration.mts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { homedir } from 'node:os'
3333
import process from 'node:process'
3434

3535
import { debugFn } from '@socketsecurity/registry/lib/debug'
36+
import { escapeRegExp } from '@socketsecurity/registry/lib/regexps'
3637

3738
import { TelemetryService } from './service.mts'
3839
import constants, { CONFIG_KEY_DEFAULT_ORG } from '../../constants.mts'
@@ -200,7 +201,7 @@ function sanitizeArgv(argv: string[]): string[] {
200201
// Remove user home directory from file paths.
201202
const homeDir = homedir()
202203
if (homeDir) {
203-
return arg.replace(new RegExp(homeDir, 'g'), '~')
204+
return arg.replace(new RegExp(escapeRegExp(homeDir), 'g'), '~')
204205
}
205206

206207
return arg
@@ -222,7 +223,7 @@ function sanitizeErrorAttribute(input: string | undefined): string | undefined {
222223
// Remove user home directory.
223224
const homeDir = homedir()
224225
if (homeDir) {
225-
return input.replace(new RegExp(homeDir, 'g'), '~')
226+
return input.replace(new RegExp(escapeRegExp(homeDir), 'g'), '~')
226227
}
227228

228229
return input

0 commit comments

Comments
 (0)