fswin32
Version:
The ultimate Node.js module for detailed Windows file system access.
46 lines (39 loc) • 1.35 kB
JavaScript
// src/network.js
import { executePowerShellCommand } from './utils.js';
/**
* Maps a network drive.
* @param {string} driveLetter The drive letter to map.
* @param {string} remotePath The remote path to map.
* @returns {Promise<string|null>} The result of the command.
*/
export const mapNetworkDrive = async (driveLetter, remotePath) => {
return await executePowerShellCommand(`net use ${driveLetter}: "${remotePath}"`);
};
/**
* Unmaps a network drive.
* @param {string} driveLetter The drive letter to unmap.
* @returns {Promise<string|null>} The result of the command.
*/
export const unmapNetworkDrive = async (driveLetter) => {
return await executePowerShellCommand(`net use ${driveLetter}: /delete`);
};
/**
* Gets a list of mapped network drives.
* @returns {Promise<Array<Object>|null>} A list of mapped network drives.
*/
export const getMappedDrives = async () => {
const stdout = await executePowerShellCommand('net use');
if (!stdout) {
return null;
}
const lines = stdout.split('\n').filter(line => line.startsWith('OK') || line.startsWith('Disconnected'));
const drives = lines.map(line => {
const parts = line.split(/\s+/);
return {
status: parts[0],
local: parts[1],
remote: parts[2],
};
});
return drives;
};