Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NSTask to execute ionic build commands - launch path not accessible

I'm working with Ionic which has a command line interface. I would like to create a small Mac app that helps to execute certain commands.

In the terminal I cd Users/me/Desktop/Repos/ionic-project/myApp After changing I would e.g. ionic run

NSTask *task = [[NSTask alloc]init];

task.launchPath = @"/bin/bash";
task.currentDirectoryPath = @"cd Users/me/Desktop/Repos/ionic-tryouts/myApp";
task.arguments = @[@"ionic run"];

[task launch];

But this gives me 'working directory doesn't exist.' I've already read quite some threads here on SO. Whats my mistake?


EDIT:

Thanks Christik for the detailed answer. I now have this code:

NSTask *task = [[NSTask alloc]init];
task.launchPath = @"/usr/local/bin/ionic";
task.currentDirectoryPath = @"/Users/me/ionic-tryouts/myApp";
task.arguments = @[@"run"];

[task launch];

Now I get the following error: env: node: No such file or directory. I guess this comes from a problem, that node.js isn't found (ionic is build on top of node). I found this issue - a missing symlink with the right name might be the cause. But setting the symlink for node didn't help. Any ideas?


EDIT2: I gave Christik the correct answer, even though I couldn't finally get it to work. I'm still investigation. Maybe it's the node installation thats wrong.


EDIT3: I saw some posts that mentioned that it might be better, if node was re-installed by homebrew since homebrew installs it without sudo. Short comment: didn't help

like image 606
brainray Avatar asked May 21 '15 13:05

brainray


2 Answers

If you want to use bash to launch ionic, the equivalent command that you'll need to execute is /bin/bash -c ionic run, thus you'll need to change the following:

  1. remove the leading cd from currentDirectoryPath (that's probably a typo from a copy+paste from terminal) and add a leading / to have an absolute path
  2. change argumentsto @[@"-c",@"ionic",@"run"], as each argument to NSTask should represent an item of the array.

If you have problems with /usr/bin/bash due to some other tools not being found, you can try using /bin/sh.

Update As Aditya Vaidyam pointed out here you might also need to setup the environment variables (especially the PATH one), to simulate the same conditions as for the terminal. If you want to find out which environment variables are set, just run the envcommand.

like image 53
Cristik Avatar answered Nov 15 '22 20:11

Cristik


On top of what Cristik suggested, you need to add to the environment variables (i.e. PATH), the location of node.js, unless it's already in /usr/bin/ or so.

like image 22
Aditya Vaidyam Avatar answered Nov 15 '22 21:11

Aditya Vaidyam