Skip to content

Commit

Permalink
fix: if no process run in port
Browse files Browse the repository at this point in the history
  • Loading branch information
dmZhan committed Jun 17, 2024
1 parent b7637b9 commit df93b9b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"prepublishOnly": "nr build",
"release": "bumpp && npm publish",
"start": "esno src/index.ts",
"test": "vitest",
"test": "vitest -w",
"typecheck": "tsc --noEmit",
"prepare": "simple-git-hooks",
"update:deps": "taze major -w -r && pnpm install"
Expand Down
14 changes: 14 additions & 0 deletions src/execa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import type { execResult } from './types'

export function execa(otherArgs: string): Promise<execResult> {
try {
if (otherArgs === '') {
return Promise.resolve({
out: 'Execa nothing',
err: '',
})
}

const { cmd, arg } = getCommandByPlatform()

const child = spawn(cmd, [arg, otherArgs], { stdio: 'pipe', cwd: process.cwd() })
Expand Down Expand Up @@ -50,6 +57,13 @@ export function execa(otherArgs: string): Promise<execResult> {
export function execaSync(otherArgs: string): execResult {
const { cmd, arg } = getCommandByPlatform()

if (otherArgs === '') {
return {
out: 'Execasync nothing',
err: '',
}
}

const result = spawnSync(cmd, [arg, otherArgs], { cwd: process.cwd(), encoding: 'utf-8' })

return {
Expand Down
2 changes: 1 addition & 1 deletion src/releasePort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function killPort(platform: 'win32', pids: string[]): string
function killPort(platform: Exclude<NodeJS.Platform, 'win32'>, method: 'udp' | 'tcp', port: number): string
function killPort(platform: NodeJS.Platform, ops?: any, port?: number): string {
if (platform === 'win32') {
return `TaskKill /F /PID ${ops?.join(' /PID ')}`
return ops.length ? `TaskKill /F /PID ${ops?.join(' /PID ')}` : ''
}
else {
return `lsof -i ${ops === 'udp' ? 'udp' : 'tcp'}:${port} | grep ${ops === 'udp' ? 'UDP' : 'LISTEN'} | awk '{print $2}' | xrags kill -9`
Expand Down
22 changes: 22 additions & 0 deletions test/releasePort.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, it } from 'vitest'
import { release, releaseSync } from '../src'

describe('releasePort', () => {
it('releaseSync', () => {
expect(releaseSync(1234)).toMatchInlineSnapshot(`
{
"err": "",
"out": "Execasync nothing",
}
`)
})

it('release', async () => {
expect(await release(1234)).toMatchInlineSnapshot(`
{
"err": "",
"out": "Execa nothing",
}
`)
})
})

0 comments on commit df93b9b

Please sign in to comment.