node子进程(Child Process)获取硬盘分区

时间:2021-08-25 01:22:11

node   child_process文档

 

child_process.exec(command[, options][, callback])

  • command <string> The command to run, with space-separated arguments.
  • options<Object>
  • cwd <string> Current working directory of the child process. Default: null.
  • env <Object> Environment key-value pairs. Default: null.
  • encoding <string> Default: 'utf8'
  • shell <string> Shell to execute the command with. See Shell Requirements and Default Windows Shell. Default: '/bin/sh' on UNIX, process.env.ComSpec on Windows.
  • timeout <number> Default: 0
  • maxBuffer <number> Largest amount of data in bytes allowed on stdout or stderr. If exceeded, the child process is terminated. See caveat at maxBuffer. Default: 200 * 1024.
  • killSignal <string> | <integer> Default: 'SIGTERM'
  • uid <number> Sets the user identity of the process (see setuid(2)).
  • gid <number> Sets the group identity of the process (see setgid(2)).
  • windowsHide <boolean> Hide the subprocess console window that would normally be created on Windows systems. Default: false.
  • callback<Function> called with the output when process terminates.
init(list){//得到硬盘分区的数组
            let ths = this;
            let lists = ths.arrayTrim(list);
            return lists;    
}
arrayTrim(arr){//数组中各元素的空格去除
            arr.shift();
            arr.forEach(function(item, index){
                arr[index] = item.trim();
            });
            return arr;
}
logicaldisk(){//获取自盘分区的字符串
            var ths = this;
            spawn.exec('wmic logicaldisk get caption', {
                windowsHide: true
            }, function(err, stdout, stderr) {
                if(err || stderr) { 
                    console.log("root path open failed" + err + stderr);
                    return;
                }
                let disks = stdout.trim().split('\n');
                ths.init(disks);
            })
}