How to 'bash' it right? make local apache call node.js bin

I am working on OSX and have mjml globally installed via node.js. what i want to do is create a shell script thats called by PHP exec to enable MAMP PRO apache to use my local mjml installation.

that should be possible, right?

  • I can call mjml PARAMS at my terminal. it renders mjml to html.
  • which mjml => /usr/local/bin/mjml
  • my $PATH has /usr/local/bin

but i can not get my shell script to work. its dynamically generated with proper paths and all, so paths here are just placeholders for you. I also call chmod +x BASH_FILE. the content of shell script is valid since when I copy it to terminal it works.

#!/bin/bash
mjml -r IN_PATH -o OUT_PATH;

probably I need to setup my .bash_profile differently. but I am not good at this.

cat ~/.bash_profile gives me…

source ~/.profile
export PATH="$PATH:$HOME/.rvm/bin:/usr/local/bin:~/.composer/vendor/bin" # Add RVM to PATH for scripting
export PATH=/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/bnomei/.rvm/bin:/usr/local/bin:~/.composer/vendor/bin:/Users/bnomei/.rvm/bin:/usr/local/bin:~/.composer/vendor/bin

Depending on the environment in which PHP runs, it may well be that PHP uses a completely different $PATH variable. Does it work if you add the full path to mjml in your shell script?

no it does not work if I use /usr/local/bin/mjml instead of mjml in my shell script.

  • since i have access to apache conf in MAMP Pro can I cross the environments somehow (total noob question I presume)?
  • or just copy the mjml bin into my mamp installation where mamps php enviroment can find it?

With the absolute path, every environment should find it. The issue needs to be something else. Is there any output from the script?

script output is mjml: command not found. same with full path.
i did some research: maybe it has to do with chrooted apache like described here but I am not sure how to add /bin/sh to my MAMP Pro apache enviroment.

edit: adding stuff to apache $PATH seems to be non trivial either.

is there no simpler way to get mjml into my local apache? just copy the bin and reference it somehow?

I don’t think your Apache is chrooted. That only really makes sense on servers and most admins who use such a setup don’t really understand security either. Having that in MAMP doesn’t make sense at all.

It also can’t be related to your $PATH. As I said, absolute paths to the binary should always work.

Could you please try running ls /usr/local/bin from PHP (replace the mjml command with it) and post the output here?

mjml is listed, return value of calling ls in exec is 0 (aka valid).

exec('ls /usr/local/bin', $out, $ret);
f::write($log, implode(PHP_EOL, [implode(PHP_EOL, $out), $ret]));

if I try calling mjml I get 127 (aka generic invalid). but same commands are valid on terminal.

exec('mjml --render "/PATH/site/snippets/mjml-veranstaltung.mjml" --output "/PATH/site/snippets/mjml-veranstaltung.html"', $out, $ret); // 127
// or
exec('mjml -h', $out, $ret); // 127

edit:
apache.log in mamp pro:
sh: mjml: command not found

Is it the same error code and error message if you use the full path? That can’t really be the case, otherwise something is broken in your setup.

exec('/usr/local/bin/mjml -h', $out, $ret);

does it matter that it is a link? seems not…

exec('ls -l /usr/local/bin', $out, $ret);
lrwxr-xr-x  1 MYUSER  admin        DATE mjml -> ../lib/node_modules/mjml/bin/mjml

exec('/usr/local/bin/mjml -h', $out, $ret); // 127 in php. valid in terminal.
exec('/usr/local/lib/node_modules/mjml/bin/mjml -h', $out, $ret); // 127 in php. valid in terminal.

I am running apache in mamp pro as MYUSER so rights should not be a problem.
maybe its special because its a node.js bin? I do not know much about node.js or their standalone bin (yet).

Could you please try another bin that is in /usr/local/bin?

If I understand correctly, you want MAMP Pro to render the mjml into html on the fly? I would suggest simpler ways of doing this.

I have a local development server setup using Vagrant and Gulp that allows me to build and test emails with MJML locally, with live reload in the browser using Browsersync and email testing via MailGun. This lets me actually send it to an email address directly from my machine so that i can test it in a mail client. I can also test the emails being sent by the CMS system because Scotchbox contains Mailcatcher.

Personally, I gave up MAMP Pro a long time ago, because it caused great pain when it came to doing anything outside of the box. You will be much happier with a Vagrant based setup.

You can set this up quite easily. I would recommend Scotchbox as this is what I build Kirby sites with. It can be configured as a multisite setup using local domains. You just need to tweak the Scotchbox Multisite.

I’m happy to share my Gulp tasks that handle the compilation of the Emails & MailGun testing if you want.

Alternatively, I’m working on a development framework that will be publicly released soon that has this baked in. It is what i use to build Kirby sites. :slight_smile:

Alternatively, if you don’t like the sound of all that, i’m assuming you are using NPM for package management? A lot of people don’t know that you can actually use NPM as a build tool. it allows you to execute shell commands prior, during and after build time. In theory you can get NPM to compile your MJML and the auto spin up your Mamp Server. There are plenty of guides on Google for using NPM as a build tool.

1 Like

@lukasbestle seems only node.js stuff fails in php. all of them work in terminal.

/usr/local/bin/sass -h => works
/usr/local/bin/node -h => works. its a os-bin not a 'node'-bin.
/usr/local/bin/sass-lint -h => fails. its a link to a /node_module/ .js file
/usr/local/bin/bower -h => fails. its a /node_module/ bin

thanks for the in depth explanation. I will definitely take a look at it.

in my current case i am developing a plugin and wanted to keep the technical-setup-bar as low as possible for users of my plugin. oh, I will just use php exec to call mjml seemed simple enough at first. :wink:

No problem. I think the problem your having is that MAMP is not “real”, its a self contained, pre-packaged version of PHP, Apache and MySQL that is kind of set in stone. If you go the Vagrant route, your server is as if it was a live production server. That was the goal of Vagrant in the first place. If you need to update something you can just SSH in the Vagrant VM and upgrade just as you would on a production server.

Whilst it does take a few steps to get it running, it’s not that hard and I certainly don’t miss MAMP.