修复 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 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 {
await fs.mkdir(userBinDir, { recursive: true });
} catch (error) {
if (!(await ensureDirWritable(homeDir))) {
const commandPath = await promiseExec('which node');
const commandDir = path.dirname(commandPath);
return await linkCommandToDir(commandDir);
userBinDir = path.dirname(commandPath);
}
await fs.mkdir(userBinDir, { recursive: true });
await linkCommandToDir(userBinDir);
} catch (error) {
Logger.error('Linking command failed:', error);
}
}
async function linkCommandToDir(commandDir: string) {