Code Monkey home page Code Monkey logo

Comments (6)

Angelk90 avatar Angelk90 commented on June 2, 2024

Hi @Saadnajmi , can you tell me something about it?

from react-native-macos.

Saadnajmi avatar Saadnajmi commented on June 2, 2024

This sort of issue comes up in our repo a lot. Here's why it's currently not possible:

exec, child_process, along with many other Javascript packages are node modules, but not react native style native modules. They are re different ways to execute native code from Javascript that are currently incompatible.

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.

from react-native-macos.

Angelk90 avatar Angelk90 commented on June 2, 2024

@ospfranco: Do shell commands work on sol?
It seems to me that you have done something with JSI, do you think it is possible?

@Saadnajmi : How come this command works /sbin/ping:

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.
Like ls or pwd don't work.

Maybe a set of commands found in /sbin work.

from react-native-macos.

ospfranco avatar ospfranco commented on June 2, 2024

Simple native implementation and binding

https://github.com/ospfranco/sol/blob/1b638b1facf61520c13cd47dfa62544363a078d6/macos/sol-macOS/lib/SolNative.swift#L92

https://github.com/ospfranco/sol/blob/1b638b1facf61520c13cd47dfa62544363a078d6/macos/sol-macOS/lib/ShellHelper.swift

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.

from react-native-macos.

Saadnajmi avatar Saadnajmi commented on June 2, 2024

@ospfranco: Do shell commands work on sol?

It seems to me that you have done something with JSI, do you think it is possible?

@Saadnajmi : How come this command works /sbin/ping:

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.

Like ls or pwd don't work.

Maybe a set of commands found in /sbin work.

react-native-childprocess seems to be a rewrite of child_process as a react native module is why it works

from react-native-macos.

Angelk90 avatar Angelk90 commented on June 2, 2024

@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.

from react-native-macos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.