From 4cd9e65078c2e5b989485f3adcfb38b0275bc5d9 Mon Sep 17 00:00:00 2001 From: Kirk Lin Date: Sun, 26 Mar 2023 17:29:47 +0800 Subject: [PATCH] perf: optimize code performance (#148) --- src/utils.ts | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 1ac79e1..2a5d348 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,3 @@ -import os from 'os' -import { execSync } from 'child_process' import which from 'which' export function remove(arr: T[], v: T) { @@ -11,27 +9,16 @@ export function remove(arr: T[], v: T) { } export function exclude(arr: T[], v: T) { - return remove(arr.slice(), v) + return arr.slice().filter(item => item !== v) } export function cmdExists(cmd: string) { - try { - // #8 - execSync( - os.platform() === 'win32' - ? `where ${cmd} > nul 2> nul"` - : `command -v ${cmd}`, - ) - return true - } - catch { - return false - } + return which.sync(cmd, { nothrow: true }) !== null } export function getVoltaPrefix(): string { // https://blog.volta.sh/2020/11/25/command-spotlight-volta-run/ const VOLTA_PREFIX = 'volta run' - const hasVoltaCommand = which.sync('volta', { nothrow: true }) !== null + const hasVoltaCommand = cmdExists('volta') return hasVoltaCommand ? VOLTA_PREFIX : '' }