From 04e130c594cc18101cc1bc5e3e97dd27691b0bd7 Mon Sep 17 00:00:00 2001 From: melvinchia3636 Date: Thu, 29 Jan 2026 11:29:41 +0800 Subject: [PATCH] feat(cli): remove `stdio: 'pipe'` option from `executeCommand` when checking package existence. --- .../download-pocketbase.ts | 4 +--- .../database-initialization/superuser.ts | 4 +--- .../commands/dev/functions/startServices.ts | 8 ++------ .../locales/handlers/publishLocaleHandler.ts | 3 +-- .../functions/templates/init-git-repo.ts | 7 +++---- .../modules/handlers/publishModuleHandler.ts | 3 +-- .../functions/executeProjectCommand.ts | 3 +-- tools/src/utils/commands.ts | 19 ++++-------------- tools/src/utils/docker.ts | 14 ++++++------- tools/src/utils/github-cli.ts | 9 +++------ tools/src/utils/helpers.ts | 19 +++++++++--------- tools/src/utils/initGitRepository.ts | 20 ++++++++++++------- tools/src/utils/pocketbase.ts | 6 ++---- tools/src/utils/registry.ts | 4 ++-- 14 files changed, 50 insertions(+), 73 deletions(-) diff --git a/tools/src/commands/db/functions/database-initialization/download-pocketbase.ts b/tools/src/commands/db/functions/database-initialization/download-pocketbase.ts index b81a097b9..f47e3bb4e 100644 --- a/tools/src/commands/db/functions/database-initialization/download-pocketbase.ts +++ b/tools/src/commands/db/functions/database-initialization/download-pocketbase.ts @@ -93,9 +93,7 @@ export async function downloadPocketBaseBinary(): Promise { logger.debug('Download complete, extracting...') // Extract using unzip command - executeCommand(`unzip -o "${zipPath}" -d "${PB_DIR}"`, { - stdio: ['pipe', 'pipe', 'pipe'] - }) + executeCommand(`unzip -o "${zipPath}" -d "${PB_DIR}"`) // Clean up zip file and unnecessary files fs.unlinkSync(zipPath) diff --git a/tools/src/commands/db/functions/database-initialization/superuser.ts b/tools/src/commands/db/functions/database-initialization/superuser.ts index 5651a80fa..2f510efc6 100644 --- a/tools/src/commands/db/functions/database-initialization/superuser.ts +++ b/tools/src/commands/db/functions/database-initialization/superuser.ts @@ -35,9 +35,7 @@ export function createPocketBaseSuperuser( try { const result = executeCommand( `${PB_BINARY_PATH} superuser create ${PB_KWARGS.join(' ')}`, - { - stdio: ['pipe', 'pipe', 'pipe'] - }, + {}, [email, password] ) diff --git a/tools/src/commands/dev/functions/startServices.ts b/tools/src/commands/dev/functions/startServices.ts index 39a68557b..a2cf0ca82 100644 --- a/tools/src/commands/dev/functions/startServices.ts +++ b/tools/src/commands/dev/functions/startServices.ts @@ -30,7 +30,7 @@ export async function startSingleService( const cwd = config.cwd instanceof Function ? config.cwd() : config.cwd - executeCommand(command, { cwd, stdio: 'inherit' }, extraArgs) + executeCommand(command, { cwd }, extraArgs) return } @@ -39,11 +39,7 @@ export async function startSingleService( if (service in PROJECTS) { const projectPath = PROJECTS[service as keyof typeof PROJECTS] - executeCommand( - `cd ${projectPath} && bun run dev`, - { stdio: 'inherit' }, - extraArgs - ) + executeCommand(`cd ${projectPath} && bun run dev`, {}, extraArgs) return } diff --git a/tools/src/commands/locales/handlers/publishLocaleHandler.ts b/tools/src/commands/locales/handlers/publishLocaleHandler.ts index e88ac7fdb..e504929d3 100644 --- a/tools/src/commands/locales/handlers/publishLocaleHandler.ts +++ b/tools/src/commands/locales/handlers/publishLocaleHandler.ts @@ -37,8 +37,7 @@ export async function publishLocaleHandler(langCode: string): Promise { try { executeCommand(`npm publish --registry ${getRegistryUrl()}`, { - cwd: targetDir, - stdio: 'inherit' + cwd: targetDir }) logger.success(`Published ${chalk.blue(fullName)}`) diff --git a/tools/src/commands/modules/functions/templates/init-git-repo.ts b/tools/src/commands/modules/functions/templates/init-git-repo.ts index 6a3087f56..9f2cb6e6b 100644 --- a/tools/src/commands/modules/functions/templates/init-git-repo.ts +++ b/tools/src/commands/modules/functions/templates/init-git-repo.ts @@ -1,10 +1,9 @@ import executeCommand from '@/utils/commands' export function initializeGitRepository(modulePath: string): void { - executeCommand('git init', { cwd: modulePath, stdio: 'ignore' }) - executeCommand('git add .', { cwd: modulePath, stdio: 'ignore' }) + executeCommand('git init', { cwd: modulePath }) + executeCommand('git add .', { cwd: modulePath }) executeCommand('git commit -m "feat: initial commit"', { - cwd: modulePath, - stdio: 'ignore' + cwd: modulePath }) } diff --git a/tools/src/commands/modules/handlers/publishModuleHandler.ts b/tools/src/commands/modules/handlers/publishModuleHandler.ts index 6e7001d6c..384e53825 100644 --- a/tools/src/commands/modules/handlers/publishModuleHandler.ts +++ b/tools/src/commands/modules/handlers/publishModuleHandler.ts @@ -83,8 +83,7 @@ export async function publishModuleHandler(moduleName: string): Promise { try { executeCommand(`npm publish --registry ${getRegistryUrl()}`, { - cwd: modulePath, - stdio: 'pipe' + cwd: modulePath }) logger.success( diff --git a/tools/src/commands/project/functions/executeProjectCommand.ts b/tools/src/commands/project/functions/executeProjectCommand.ts index fa416dac6..4611a4d8c 100644 --- a/tools/src/commands/project/functions/executeProjectCommand.ts +++ b/tools/src/commands/project/functions/executeProjectCommand.ts @@ -18,8 +18,7 @@ export function executeProjectCommand( const projectPath = PROJECTS[projectType as ProjectType] executeCommand(`bun run ${commandType}`, { - cwd: projectPath, - stdio: 'inherit' + cwd: projectPath }) } } diff --git a/tools/src/utils/commands.ts b/tools/src/utils/commands.ts index 466671af9..af119ea77 100644 --- a/tools/src/utils/commands.ts +++ b/tools/src/utils/commands.ts @@ -1,4 +1,3 @@ -import { LOG_LEVELS, type LogLevel } from '@lifeforge/log' import { type IOType, spawnSync } from 'child_process' import fs from 'fs' import path from 'path' @@ -53,11 +52,11 @@ export default function executeCommand( if (logger.level === 'debug') { if (result.stdout) { - process.stdout.write(result.stdout.toString()) + logger.debug(result.stdout.toString()) } if (result.stderr) { - process.stderr.write(result.stderr.toString()) + logger.debug(result.stderr.toString()) } } @@ -90,12 +89,7 @@ export default function executeCommand( */ export function bunInstall() { executeCommand('bun install --ignore-scripts', { - cwd: ROOT_DIR, - stdio: - LOG_LEVELS.indexOf(logger.instance.level as LogLevel) > - LOG_LEVELS.indexOf('debug') - ? 'pipe' - : 'inherit' + cwd: ROOT_DIR }) } @@ -122,12 +116,7 @@ export function installPackage( logger.debug(`Installing ${fullName} from registry...`) executeCommand(`bun add ${fullName}@latest --ignore-scripts`, { - cwd: ROOT_DIR, - stdio: - LOG_LEVELS.indexOf(logger.instance.level as LogLevel) > - LOG_LEVELS.indexOf('info') - ? 'pipe' - : 'inherit' + cwd: ROOT_DIR }) const installedPath = path.join(ROOT_DIR, 'node_modules', fullName) diff --git a/tools/src/utils/docker.ts b/tools/src/utils/docker.ts index 6204f3ad3..8d835d7eb 100644 --- a/tools/src/utils/docker.ts +++ b/tools/src/utils/docker.ts @@ -1,5 +1,3 @@ -import { execSync } from 'child_process' - import executeCommand from './commands' import { isDockerMode } from './helpers' import logger from './logger' @@ -11,7 +9,7 @@ const SERVER_CONTAINER = 'lifeforge-server' */ export function isDockerRunning(): boolean { try { - execSync('docker info', { stdio: 'pipe' }) + executeCommand('docker info', { exitOnError: false }) return true } catch { @@ -24,9 +22,9 @@ export function isDockerRunning(): boolean { */ export function isContainerRunning(containerName: string): boolean { try { - const status = execSync( + const status = executeCommand( `docker ps --filter "name=${containerName}" --format "{{.Status}}"`, - { encoding: 'utf8', stdio: 'pipe' } + { exitOnError: false } ).trim() return status.length > 0 @@ -58,7 +56,7 @@ export function restartServerContainer(): void { try { logger.info('Restarting Docker server container...') - execSync(`docker restart ${SERVER_CONTAINER}`, { stdio: 'inherit' }) + executeCommand(`docker restart ${SERVER_CONTAINER}`, { exitOnError: false }) logger.success('Server container restarted') } catch (error) { logger.error(`Failed to restart Docker server.`) @@ -89,7 +87,7 @@ export function stopService(serviceName: string): void { try { logger.debug(`Stopping Docker service ${serviceName}...`) - executeCommand(`docker stop ${serviceName}`, { stdio: 'inherit' }) + executeCommand(`docker stop ${serviceName}`, { exitOnError: false }) logger.success(`Service ${serviceName} stopped`) } catch (error) { logger.error(`Failed to stop Docker service ${serviceName}.`) @@ -104,7 +102,7 @@ export function startService(serviceName: string): void { try { logger.debug(`Starting Docker service ${serviceName}...`) - executeCommand(`docker start ${serviceName}`, { stdio: 'inherit' }) + executeCommand(`docker start ${serviceName}`, { exitOnError: false }) logger.success(`Service ${serviceName} started`) } catch (error) { logger.error(`Failed to start Docker service ${serviceName}.`) diff --git a/tools/src/utils/github-cli.ts b/tools/src/utils/github-cli.ts index 532633843..666e4aff6 100644 --- a/tools/src/utils/github-cli.ts +++ b/tools/src/utils/github-cli.ts @@ -6,8 +6,7 @@ export function validateMaintainerAccess(username: string): void { try { // Check permission level on the official repo const result = executeCommand( - `gh api repos/lifeforge-app/lifeforge/collaborators/${username}/permission`, - { stdio: 'pipe' } + `gh api repos/lifeforge-app/lifeforge/collaborators/${username}/permission` ) const response = JSON.parse(result) as { @@ -38,7 +37,7 @@ export function validateMaintainerAccess(username: string): void { export function getGithubUser(): { name: string; email: string } | null { try { // Try getting basic user info first - const basicInfo = executeCommand('gh api user', { stdio: 'pipe' }) + const basicInfo = executeCommand('gh api user') const user = JSON.parse(basicInfo) as { name: string; email: string | null } @@ -47,9 +46,7 @@ export function getGithubUser(): { name: string; email: string } | null { // If email is private/null, try fetching from /user/emails if (!email) { try { - const emailsJson = executeCommand('gh api user/emails', { - stdio: 'pipe' - }) + const emailsJson = executeCommand('gh api user/emails') const emails = JSON.parse(emailsJson) as Array<{ email: string diff --git a/tools/src/utils/helpers.ts b/tools/src/utils/helpers.ts index c380cb44a..a2e852aa9 100644 --- a/tools/src/utils/helpers.ts +++ b/tools/src/utils/helpers.ts @@ -1,4 +1,3 @@ -import { spawnSync } from 'child_process' import prompts from 'prompts' import executeCommand from './commands' @@ -84,12 +83,13 @@ export function killExistingProcess( } const serverInstance = executeCommand(`pgrep -f "${processKeywordOrPID}"`, { - exitOnError: false, - stdio: 'pipe' + exitOnError: false }) if (serverInstance) { - executeCommand(`pkill -f "${processKeywordOrPID}"`) + executeCommand(`pkill -f "${processKeywordOrPID}"`, { + exitOnError: false + }) logger.debug( `Killed process matching keyword: ${processKeywordOrPID} (PID: ${serverInstance})` @@ -110,12 +110,13 @@ export function killExistingProcess( */ export function checkPortInUse(port: number): boolean { try { - const result = spawnSync('nc', ['-zv', 'localhost', port.toString()], { - stdio: 'pipe', - encoding: 'utf8' - }) + executeCommand('nc', { exitOnError: false }, [ + '-zv', + 'localhost', + port.toString() + ]) - return result.status === 0 + return true } catch { return false } diff --git a/tools/src/utils/initGitRepository.ts b/tools/src/utils/initGitRepository.ts index 7c6770cdf..3a0a0f265 100644 --- a/tools/src/utils/initGitRepository.ts +++ b/tools/src/utils/initGitRepository.ts @@ -33,18 +33,24 @@ export default function initGitRepository(targetDir: string): void { logger.info(`Initializing git repository...`) try { - executeCommand('git init', { cwd: targetDir, stdio: 'pipe' }) + executeCommand('git init', { cwd: targetDir, exitOnError: false }) executeCommand(`git remote add origin ${repoUrl}`, { cwd: targetDir, - stdio: 'pipe' + exitOnError: false }) - executeCommand('git fetch origin', { cwd: targetDir, stdio: 'pipe' }) - executeCommand('git checkout -b main', { cwd: targetDir, stdio: 'pipe' }) - executeCommand('git reset origin/main', { cwd: targetDir, stdio: 'pipe' }) - executeCommand('git add .', { cwd: targetDir, stdio: 'pipe' }) + executeCommand('git fetch origin', { cwd: targetDir, exitOnError: false }) + executeCommand('git checkout -b main', { + cwd: targetDir, + exitOnError: false + }) + executeCommand('git reset origin/main', { + cwd: targetDir, + exitOnError: false + }) + executeCommand('git add .', { cwd: targetDir, exitOnError: false }) executeCommand( 'git commit --allow-empty -m "feat: update to latest version from forgistry"', - { cwd: targetDir, stdio: 'pipe' } + { cwd: targetDir, exitOnError: false } ) logger.debug(`Git repository initialized with remote: ${repoUrl}`) diff --git a/tools/src/utils/pocketbase.ts b/tools/src/utils/pocketbase.ts index 0b6d74e8d..440dc6782 100644 --- a/tools/src/utils/pocketbase.ts +++ b/tools/src/utils/pocketbase.ts @@ -23,8 +23,7 @@ function isValidPocketbaseProcess(pid: number): boolean { // Verify it's actually a pocketbase process by checking the command const psResult = executeCommand(`ps -p ${pid} -o comm=`, { - exitOnError: false, - stdio: 'pipe' + exitOnError: false }) return psResult?.toLowerCase().includes('pocketbase') ?? false @@ -43,8 +42,7 @@ function isValidPocketbaseProcess(pid: number): boolean { export function checkRunningPBInstances(exitOnError = true): boolean { try { const result = executeCommand(`pgrep -f "pocketbase serve"`, { - exitOnError: false, - stdio: 'pipe' + exitOnError: false }) if (!result?.trim()) { diff --git a/tools/src/utils/registry.ts b/tools/src/utils/registry.ts index 4bf84a131..67f2f7911 100644 --- a/tools/src/utils/registry.ts +++ b/tools/src/utils/registry.ts @@ -41,7 +41,7 @@ export async function checkPackageExists( try { executeCommand(`npm view ${packageName} --registry ${registry}`, { cwd: ROOT_DIR, - stdio: 'pipe' + exitOnError: false }) return true @@ -67,7 +67,7 @@ export async function checkAuth(): Promise<{ `npm whoami --registry ${registry} 2>/dev/null`, { cwd: ROOT_DIR, - stdio: 'pipe' + exitOnError: false } )