Im trying to create CLI using node.js and I have some tasks where I need to move a folder from one place to another. So to achieve this I used node.js's child_process
. Like this:
const { exec } = require("child_process");
function moveFolder(folder,destination){
exec(
`mv ${folder} ${destination}`,
(error, stdout, stderr) => {
if (error) {
console.log("Move error");
}
console.log("Move success");
}
);
}
This piece of code works when I execute this with bash terminal (git-bash or in Linux). But when I execute it using CMD or PowerShell it throws an error
'mv' is not recognized as an internal or external command.
I know PowerShell and CMD have a command called move
to achieve this, and that's why it is throwing the error. But is there a way (library or some other method) to execute these types of commands in all environments? (in Windows (CMD, PowerShell), Linux (bash, zsh), Mac OS (zsh)). How can I achieve this?. Can we detect the shell type and execute different commands?