From 72d4c8bfd4d419602cbf08befb0f70ffdac25444 Mon Sep 17 00:00:00 2001 From: whyour Date: Sat, 15 Nov 2025 02:27:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20homedir=20=E6=9D=83?= =?UTF-8?q?=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/loaders/deps.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/back/loaders/deps.ts b/back/loaders/deps.ts index 7e468e15..166bb36c 100644 --- a/back/loaders/deps.ts +++ b/back/loaders/deps.ts @@ -4,6 +4,8 @@ import os from 'os'; import chokidar from 'chokidar'; import config from '../config/index'; import { promiseExec } from '../config/util'; +import { W_OK } from 'constants'; +import Logger from './logger'; async function linkToNodeModule(src: string, dst?: string) { const target = path.join(config.rootPath, 'node_modules', dst || src); @@ -17,19 +19,29 @@ async function linkToNodeModule(src: string, dst?: string) { } catch (error) { } } +async function ensureDirWritable(dir: string) { + try { + await fs.access(dir, W_OK); + return true; + } catch { + return false; + } +} + async function linkCommand() { const homeDir = os.homedir(); - const userBinDir = path.join(homeDir, 'bin'); + let userBinDir = path.join(homeDir, 'bin'); try { + if (!(await ensureDirWritable(homeDir))) { + const commandPath = await promiseExec('which node'); + userBinDir = path.dirname(commandPath); + } await fs.mkdir(userBinDir, { recursive: true }); + await linkCommandToDir(userBinDir); } catch (error) { - const commandPath = await promiseExec('which node'); - const commandDir = path.dirname(commandPath); - return await linkCommandToDir(commandDir); + Logger.error('Linking command failed:', error); } - - await linkCommandToDir(userBinDir); } async function linkCommandToDir(commandDir: string) {