Skip to content
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

Run terminal commands #13

Closed
Angelk90 opened this issue Sep 28, 2023 · 10 comments
Closed

Run terminal commands #13

Angelk90 opened this issue Sep 28, 2023 · 10 comments

Comments

@Angelk90
Copy link

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
Copy link
Owner

Hey, so according to this stackoverflow thread you can use this library to achive it in React Native. However this is way out of scope for react-native-menubar-extra library, therefore I'm closing this issue.

@Angelk90
Copy link
Author

@okwasniewski : I tried to make it work, but I couldn't.

Screenshot 2023-09-28 alle 22 31 32 Screenshot 2023-09-28 alle 22 31 54
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);
  }, [])

@okwasniewski
Copy link
Owner

It works for me did you ran pod install after installing this library?

@Angelk90
Copy link
Author

@okwasniewski : it stops working.

Screenshot 2023-09-28 alle 22 52 28

@okwasniewski
Copy link
Owner

You need to clean up the old build. Inside macos folder run rm -rf Pods build and then install pods again (pod install)

@Angelk90
Copy link
Author

Angelk90 commented Sep 28, 2023

@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

@Saadnajmi
Copy link

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.

@okwasniewski
Copy link
Owner

@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
Copy link

@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

@Angelk90
Copy link
Author

Angelk90 commented Oct 2, 2023

@okwasniewski , @Saadnajmi : Maybe I managed to do something, I replied on react-native-macos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants