From 686a2e2bbb7c4434688331c8b7dc6680c53cddec Mon Sep 17 00:00:00 2001 From: Vladyslav Mikhieiev Date: Mon, 22 Jan 2024 13:47:55 +0200 Subject: [PATCH] Add support for specifying the path to nodejs executable --- README.md | 8 ++++++++ src/Mjml.php | 16 ++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 444a07c..399af1d 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,14 @@ use Spatie\Mjml\Mjml; Mjml::new()->canConvertWithoutErrors($mjml); // returns a boolean ``` +### Specifying the path to nodejs executable + +By default, the package itself will try to determine the path to the `node` executable. If the package can't find a path, you can specify a path in the environment variable `MJML_NODE_PATH` + +```shell +MJML_NODE_PATH=/home/user/.nvm/versions/node/v20.11.0/bin +``` + ## Sidecar This package also supports running through [Sidecar](https://github.com/hammerstonedev/sidecar) in Laravel projects. diff --git a/src/Mjml.php b/src/Mjml.php index 1f498cc..e6b8dd2 100755 --- a/src/Mjml.php +++ b/src/Mjml.php @@ -163,11 +163,19 @@ protected function checkForDeprecationWarning(string $result): string protected function getCommand(array $arguments): array { + $extraDirectories = [ + '/usr/local/bin', + '/opt/homebrew/bin' + ]; + + $nodePathFromEnv = getenv('MJML_NODE_PATH'); + + if ($nodePathFromEnv) { + array_unshift($extraDirectories, $nodePathFromEnv); + } + return [ - (new ExecutableFinder())->find('node', 'node', [ - '/usr/local/bin', - '/opt/homebrew/bin', - ]), + (new ExecutableFinder())->find('node', 'node', $extraDirectories), 'mjml.mjs', json_encode(array_values($arguments)), ];