New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Be able to execute shell commands #1937
Comments
Hi @Saadnajmi , can you tell me something about it? |
This sort of issue comes up in our repo a lot. Here's why it's currently not possible:
Node modules are with the Node-API, while React Native modules are written with either the legacy native module system, or as turbo modules, the latter of which uses a layer called JSI to talk to native code. This issue is with React Native in general (you wouldn't be able to run your above code on iOS or Android), but it is more pronounced on our desktop platform, where one expects all their favorite node APIs to work. There is a project from @vmoroz (feel free to correct my mistakes describing) to refactor the JSI layer mentioned to support the node-api, allowing node modules to work with react native turbo modules. Then, your request would be something that is possible. Unfortunately, I don't have an ETA for when that project will land, or whether it would get adopted by React Native or React Native macOS. It's currently getting prototyped on React Native windows. |
@ospfranco: Do shell commands work on sol? @Saadnajmi : How come this command works import {spawn, kill} from 'react-native-childprocess'
useEffect(() => {
const fetchData = async () => {
cmdID = await spawn('/sbin/ping', ['google.com'], {
pwd: "/",
stdout: (output) => {
console.log('>>>', output)
}
});
console.log(cmdID)
}
fetchData()
.catch(console.error);
}, []) However, any different command does not work. Maybe a set of commands found in |
Simple native implementation and binding Probably won't work with Sandboxing enabled though. So no, React Native probably shouldn't/can't implement this, also huge security vunerability for bad actors. |
react-native-childprocess seems to be a rewrite of child_process as a react native module is why it works |
@okwasniewski , @Saadnajmi , @ospfranco : I don't know but it seems like maybe it works like this: import { spawn, kill } from 'react-native-childprocess';
....
const [pwd, setPwd] = useState(undefined);
useEffect(() => {
let cmdID;
const shell = async () => {
cmdID = await spawn('/bin/sh', ['-c','ls'], {
pwd: '/Users/',
stdout: (output) => {
const paths = output.split('\n').filter(Boolean).filter(val => val !== "Shared");
if(paths.length === 1) setPwd(`/Users/${paths[0]}/Desktop`)
},
stderr: (output) => console.log('stderr:', output),
terminate: (output) => console.log('terminate:', output),
});
};
shell().catch(console.error);
return () => {
kill(cmdID);
};
}, []);
useEffect(() => {
if(pwd === undefined) return
let cmdID;
const shell = async () => {
cmdID = await spawn('/bin/sh', ['-c','ls'], {
pwd,
stdout: (output) => {
console.log(output.split('\n').filter(Boolean))
},
stderr: (output) => console.log('stderr:', output),
terminate: (output) => console.log('terminate:', output),
});
};
shell().catch(console.error);
return () => {
kill(cmdID);
};
}, [pwd]); Let me know what you think. |
Summary
Being able to execute shell commands for example using
child_process
would be very useful.Basic Example
The text was updated successfully, but these errors were encountered: