修复 homedir 权限

This commit is contained in:
whyour 2025-11-15 02:27:27 +08:00
parent 8f23c61fee
commit 72d4c8bfd4

View File

@ -4,6 +4,8 @@ import os from 'os';
import chokidar from 'chokidar'; import chokidar from 'chokidar';
import config from '../config/index'; import config from '../config/index';
import { promiseExec } from '../config/util'; import { promiseExec } from '../config/util';
import { W_OK } from 'constants';
import Logger from './logger';
async function linkToNodeModule(src: string, dst?: string) { async function linkToNodeModule(src: string, dst?: string) {
const target = path.join(config.rootPath, 'node_modules', dst || src); const target = path.join(config.rootPath, 'node_modules', dst || src);
@ -17,19 +19,29 @@ async function linkToNodeModule(src: string, dst?: string) {
} catch (error) { } } catch (error) { }
} }
async function linkCommand() { async function ensureDirWritable(dir: string) {
const homeDir = os.homedir();
const userBinDir = path.join(homeDir, 'bin');
try { try {
await fs.mkdir(userBinDir, { recursive: true }); await fs.access(dir, W_OK);
} catch (error) { return true;
const commandPath = await promiseExec('which node'); } catch {
const commandDir = path.dirname(commandPath); return false;
return await linkCommandToDir(commandDir); }
} }
async function linkCommand() {
const homeDir = os.homedir();
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); await linkCommandToDir(userBinDir);
} catch (error) {
Logger.error('Linking command failed:', error);
}
} }
async function linkCommandToDir(commandDir: string) { async function linkCommandToDir(commandDir: string) {