Run terminal commands
Closed this issue · 10 comments
Hi @okwasniewski, It would be useful to be able to execute terminal-type commands, like this one here:
const { exec } = require("child_process");
...
exec("ls -la", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
So I thought it would be useful to adopt a server solution like this: https://testableapple.com/gaining-access-to-command-line-from-maestro/
@okwasniewski : I tried to make it work, but I couldn't.
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);
}, [])
It works for me did you ran pod install
after installing this library?
@okwasniewski : it stops working.
You need to clean up the old build. Inside macos
folder run rm -rf Pods build
and then install pods again (pod install
)
@okwasniewski : I don't understand why it's always macos from all these problems.
the first time it was worth:
console.log('o:', cmdID);
then changed it by saving it, then it updated:
console.log('oo:', cmdID);
I tried this:
useEffect(() => {
let cmdID;
const shell = async () => {
cmdID = await spawn('pwd', ['-LP'], {
pwd: '/Users',
stdout: (output) => {
console.log('stdout:', output);
},
stderr: (output) => {
console.log('stderr:', output);
},
terminate: (output) => {
console.log('terminate:', output);
},
});
console.log('oo:', cmdID);
};
shell().catch(console.error);
return () => {
console.log("kill")
kill(cmdID);
};
}, []);
But the command written this way seems to work well, I get this:
LOG o: -1
LOG kill
LOG [Error: invalid cmdID]
LOG oo: -1
See my explanation here: microsoft/react-native-macos#1937 (comment)
It's not currently possible to run arbitrary node native modules with React Native macOS, as they have two completely separate methods of JS->Native (C++) communication.
@Saadnajmi The solution for this particular issue would be to create an RN Macos library that would wrap another native library to execute shell commands like https://github.com/JohnSundell/ShellOut right?
@Saadnajmi The solution for this particular issue would be to create an RN Macos library that would wrap another native library to execute shell commands like https://github.com/JohnSundell/ShellOut right?
Yep, unless the other project I mentioned lands and we can get all node native modules to "just work" with react native macOS (while using Hermes). But you know, that's a ways away I think, and writing your own native module OS the way to go IMO
@okwasniewski , @Saadnajmi : Maybe I managed to do something, I replied on react-native-macos.