You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Node.js中解析映射驱动器的实际路径?

解决映射驱动器转实际路径的方案

Node.js 原生实现(跨Windows/Linux)

Windows 平台

Windows映射驱动器属于逻辑磁盘,可通过调用系统命令获取映射关系后转换路径:

const { execSync } = require('child_process');
const path = require('path');

function getRealPathFromMappedDrive(mappedPath) {
  const driveLetter = mappedPath.match(/^[A-Za-z]:/)[0];
  
  try {
    // 用wmic获取映射驱动器的实际路径
    const output = execSync(`wmic path win32_logicaldisk where "drivetype=4 and deviceid='${driveLetter}\\'" get providername /value`, { encoding: 'utf8' });
    const match = output.match(/Providername=(.*)/);
    if (match) {
      const realRoot = match[1].trim();
      return path.join(realRoot, mappedPath.slice(driveLetter.length));
    }
  } catch (err) {
    // wmic失败时尝试subst命令
    const substOutput = execSync('subst', { encoding: 'utf8' });
    const driveMatch = substOutput.match(new RegExp(`${driveLetter}\\\\s+([^\\r\\n]+)`));
    if (driveMatch) {
      const realRoot = driveMatch[1].trim();
      return path.join(realRoot, mappedPath.slice(driveLetter.length));
    }
  }
  
  return mappedPath;
}

Linux 平台

Linux挂载点可通过findmnt命令解析实际路径:

const { execSync } = require('child_process');
const path = require('path');

function getRealPathFromMount(mappedPath) {
  try {
    const output = execSync(`findmnt -no TARGET,SOURCE --target "${mappedPath}"`, { encoding: 'utf8' });
    const [target, source] = output.trim().split(/\s+/);
    return mappedPath.replace(target, source);
  } catch (err) {
    return mappedPath;
  }
}

跨平台整合函数

自动识别操作系统,统一路径转换逻辑:

const { execSync } = require('child_process');
const path = require('path');
const os = require('os');

function resolveRealPath(mappedPath) {
  const platform = os.platform();
  
  if (platform === 'win32') {
    const driveLetter = mappedPath.match(/^[A-Za-z]:/);
    if (!driveLetter) return mappedPath;
    
    const drive = driveLetter[0];
    try {
      const wmicOutput = execSync(`wmic path win32_logicaldisk where "drivetype=4 and deviceid='${drive}\\'" get providername /value`, { encoding: 'utf8' });
      const realRootMatch = wmicOutput.match(/Providername=(.*)/);
      if (realRootMatch) {
        const realRoot = realRootMatch[1].trim();
        return path.join(realRoot, mappedPath.slice(drive.length));
      }
      
      const substOutput = execSync('subst', { encoding: 'utf8' });
      const substMatch = substOutput.match(new RegExp(`${drive}\\\\s+([^\\r\\n]+)`));
      if (substMatch) {
        const realRoot = substMatch[1].trim();
        return path.join(realRoot, mappedPath.slice(drive.length));
      }
    } catch (err) {
      console.warn('解析映射驱动器失败:', err.message);
    }
  } else if (platform === 'linux' || platform === 'darwin') {
    try {
      const findmntOutput = execSync(`findmnt -no TARGET,SOURCE --target "${mappedPath}"`, { encoding: 'utf8' });
      const [target, source] = findmntOutput.trim().split(/\s+/);
      return mappedPath.replace(target, source);
    } catch (err) {
      console.warn('解析挂载点失败:', err.message);
    }
  }
  
  return mappedPath;
}

在Vite配置中使用

将路径转换逻辑整合到vite.config.ts,自动设置root为实际路径:

import { defineConfig } from 'vite';
import { execSync } from 'child_process';
import path from 'path';
import os from 'os';

function resolveRealPath(mappedPath: string): string {
  // 复制上述跨平台函数代码
  const platform = os.platform();
  
  if (platform === 'win32') {
    const driveLetter = mappedPath.match(/^[A-Za-z]:/);
    if (!driveLetter) return mappedPath;
    
    const drive = driveLetter[0];
    try {
      const wmicOutput = execSync(`wmic path win32_logicaldisk where "drivetype=4 and deviceid='${drive}\\'" get providername /value`, { encoding: 'utf8' });
      const realRootMatch = wmicOutput.match(/Providername=(.*)/);
      if (realRootMatch) {
        const realRoot = realRootMatch[1].trim();
        return path.join(realRoot, mappedPath.slice(drive.length));
      }
      
      const substOutput = execSync('subst', { encoding: 'utf8' });
      const substMatch = substOutput.match(new RegExp(`${drive}\\\\s+([^\\r\\n]+)`));
      if (substMatch) {
        const realRoot = substMatch[1].trim();
        return path.join(realRoot, mappedPath.slice(drive.length));
      }
    } catch (err) {
      console.warn('解析映射驱动器失败:', (err as Error).message);
    }
  } else if (platform === 'linux' || platform === 'darwin') {
    try {
      const findmntOutput = execSync(`findmnt -no TARGET,SOURCE --target "${mappedPath}"`, { encoding: 'utf8' });
      const [target, source] = findmntOutput.trim().split(/\s+/);
      return mappedPath.replace(target, source);
    } catch (err) {
      console.warn('解析挂载点失败:', (err as Error).message);
    }
  }
  
  return mappedPath;
}

export default defineConfig(() => {
  const currentCwd = process.cwd();
  const realRoot = resolveRealPath(currentCwd);
  
  return {
    root: realRoot,
    // 其他Vite配置项...
  };
});

备选方案:CLI工具(跨平台)

若Node.js方法存在问题,可通过CLI命令提前转换路径再传给Vite:

Windows

@echo off
for /f "tokens=2 delims==" %%a in ('wmic path win32_logicaldisk where "drivetype=4 and deviceid='D:'" get providername /value') do set REAL_PATH=%%a
npx vite build --root "%REAL_PATH%"

Linux

REAL_PATH=$(findmnt -no SOURCE --target "$PWD")
npx vite build --root "$REAL_PATH"

内容的提问来源于stack exchange,提问作者nvirth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 15:50:14