Skip to content

Commit

Permalink
Generate using a file with the cli when generating locally
Browse files Browse the repository at this point in the history
  • Loading branch information
riasvdv committed Jan 3, 2025
1 parent e31e7be commit 5aefa29
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 81 deletions.
28 changes: 0 additions & 28 deletions bin/mjml.mjs

This file was deleted.

1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
],
"require": {
"php": "^8.1",
"spatie/temporary-directory": "^2.2",
"symfony/process": "^6.3.2|^7.0"
},
"require-dev": {
Expand Down
114 changes: 69 additions & 45 deletions src/Mjml.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Spatie\Mjml\Exceptions\CouldNotConvertMjml;
use Spatie\Mjml\Exceptions\SidecarPackageUnavailable;
use Spatie\MjmlSidecar\MjmlFunction;
use Spatie\TemporaryDirectory\TemporaryDirectory;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
Expand All @@ -23,8 +24,6 @@ class Mjml

protected string $filePath = '.';

protected string $workingDirectory;

protected bool $sidecar = false;

public static function new(): self
Expand All @@ -35,8 +34,6 @@ public static function new(): self
protected function __construct()
{
$this->validationLevel = ValidationLevel::Soft;

$this->workingDirectory = realpath(dirname(__DIR__).'/bin');
}

public function keepComments(bool $keepComments = true): self
Expand Down Expand Up @@ -93,13 +90,6 @@ public function filePath(string $filePath): self
return $this;
}

public function workingDirectory(string $workingDirectory): self
{
$this->workingDirectory = $workingDirectory;

return $this;
}

public function canConvert(string $mjml): bool
{
try {
Expand Down Expand Up @@ -134,19 +124,11 @@ public function convert(string $mjml, array $options = []): MjmlResult
$this->configOptions($options),
];

$resultString = $this->sidecar
? $this->getSideCarResult($arguments)
: $this->getLocalResult($arguments);

$resultString = $this->checkForDeprecationWarning($resultString);

$resultProperties = json_decode($resultString, true);

if (array_key_exists('mjmlError', $resultProperties)) {
throw CouldNotConvertMjml::make($resultProperties['mjmlError']);
if ($this->sidecar) {
return $this->getSideCarResult($arguments);
}

return new MjmlResult($resultProperties);
return $this->getLocalResult($arguments);
}

protected function checkForDeprecationWarning(string $result): string
Expand All @@ -160,24 +142,24 @@ protected function checkForDeprecationWarning(string $result): string
return $result;
}

protected function getCommand(array $arguments): array
public function getCommand(TemporaryDirectory $tempDir, string $templatePath, string $outputPath, $arguments): array
{
$extraDirectories = [
'/usr/local/bin',
'/opt/homebrew/bin',
];
$executableFinder = new ExecutableFinder();
$mjml = $executableFinder->find('mjml');

$nodePathFromEnv = getenv('MJML_NODE_PATH');
if (! $mjml) {
$tempDir->delete();

if ($nodePathFromEnv) {
array_unshift($extraDirectories, $nodePathFromEnv);
throw CouldNotConvertMjml::make("No MJML binary found. Make sure it is installed on your system.");
}

return [
(new ExecutableFinder)->find('node', 'node', $extraDirectories),
'mjml.mjs',
base64_encode(json_encode(array_values($arguments))),
];
$command = [$mjml, $templatePath, '-o', $outputPath];

foreach ($arguments as $configKey => $configValue) {
$command[] = "-c.{$configKey}";
$command[] = $configValue;
}
return $command;
}

protected function configOptions(array $overrides): array
Expand All @@ -194,33 +176,75 @@ protected function configOptions(array $overrides): array
return array_merge($defaults, $overrides);
}

protected function getSideCarResult(array $arguments): string
protected function getSideCarResult(array $arguments): MjmlResult
{
if (! class_exists(MjmlFunction::class)) {
throw SidecarPackageUnavailable::make();
}

return MjmlFunction::execute([
$result = MjmlFunction::execute([
'mjml' => $arguments[0],
'options' => $arguments[1],
])->body();

$result = $this->checkForDeprecationWarning($result);

$resultProperties = json_decode($result, true);

if (array_key_exists('mjmlError', $resultProperties)) {
throw CouldNotConvertMjml::make($resultProperties['mjmlError']);
}

return new MjmlResult($resultProperties);
}

protected function getLocalResult(array $arguments): string
protected function getLocalResult(array $arguments): MjmlResult
{
$process = new Process(
$this->getCommand($arguments),
$this->workingDirectory,
);
$tempDir = TemporaryDirectory::make();
$filename = date('U');

$templatePath = $tempDir->path("{$filename}.mjml");
file_put_contents($templatePath, $arguments[0]);

$outputPath = $tempDir->path("{$filename}.html");

$command = $this->getCommand($tempDir, $templatePath, $outputPath, $arguments[1]);

$process = new Process($command);
$process->run();

if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
$output = explode("\n", $process->getErrorOutput());
$errors = array_filter($output, fn (string $output) => str_contains($output, 'Error'));

$tempDir->delete();

throw CouldNotConvertMjml::make($errors[0] ?? $process->getErrorOutput());
}

$errors = [];

if ($process->getErrorOutput()) {
$errors = array_filter(explode("\n", $process->getErrorOutput()));
$errors = array_map(function (string $error) {
preg_match('/Line (\d+) of (.+) \((.+)\) — (.+)/u', $error, $matches);
[, $line, , $tagName, $message] = $matches;

return [
'line' => $line,
'message' => $message,
'tagName' => $tagName,
];
}, $errors);
}

$items = explode("\n", $process->getOutput());
$html = file_get_contents($outputPath);

$tempDir->delete();

return base64_decode(end($items));
return new MjmlResult([
'html' => $html,
'errors' => $errors,
]);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="und" dir="auto" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">

<head>
<title></title>
Expand Down Expand Up @@ -40,6 +40,7 @@
display: block;
margin: 13px 0;
}

</style>
<!--[if mso]>
<noscript>
Expand All @@ -60,6 +61,7 @@
<link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700" rel="stylesheet" type="text/css">
<style type="text/css">
@import url(https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700);

</style>
<!--<![endif]-->
<style type="text/css">
Expand All @@ -69,17 +71,23 @@
max-width: 100%;
}
}

</style>
<style media="screen and (min-width:480px)">
.moz-text-html .mj-column-per-100 {
width: 100% !important;
max-width: 100%;
}

</style>
<style type="text/css">
</style>
<style type="text/css">
</style>
</head>

<body style="word-spacing:normal;">
<div style="" lang="und" dir="auto">
<div style="">
<!--[if mso | IE]><table align="center" border="0" cellpadding="0" cellspacing="0" class="" role="presentation" style="width:600px;" width="600" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]-->
<div style="margin:0px auto;max-width:600px;">
<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;">
Expand Down Expand Up @@ -108,4 +116,4 @@
</div>
</body>

</html>
</html>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!doctype html><html lang="und" dir="auto" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"><head><title></title><!--[if !mso]><!--><meta http-equiv="X-UA-Compatible" content="IE=edge"><!--<![endif]--><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><style type="text/css">#outlook a { padding:0; }
<!doctype html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"><head><title></title><!--[if !mso]><!--><meta http-equiv="X-UA-Compatible" content="IE=edge"><!--<![endif]--><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><style type="text/css">#outlook a { padding:0; }
body { margin:0;padding:0;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%; }
table, td { border-collapse:collapse;mso-table-lspace:0pt;mso-table-rspace:0pt; }
img { border:0;height:auto;line-height:100%; outline:none;text-decoration:none;-ms-interpolation-mode:bicubic; }
Expand All @@ -17,4 +17,4 @@
</style>
<![endif]--><!--[if !mso]><!--><link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700" rel="stylesheet" type="text/css"><style type="text/css">@import url(https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700);</style><!--<![endif]--><style type="text/css">@media only screen and (min-width:480px) {
.mj-column-per-100 { width:100% !important; max-width: 100%; }
}</style><style media="screen and (min-width:480px)">.moz-text-html .mj-column-per-100 { width:100% !important; max-width: 100%; }</style></head><body style="word-spacing:normal;"><div lang="und" dir="auto"><!--[if mso | IE]><table align="center" border="0" cellpadding="0" cellspacing="0" class="" role="presentation" style="width:600px;" width="600" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]--><div style="margin:0px auto;max-width:600px;"><table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;"><tbody><tr><td style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;"><!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr><td class="" style="vertical-align:top;width:600px;" ><![endif]--><div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;"><table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%"><tbody><tr><td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"><div style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:13px;line-height:1;text-align:left;color:#000000;">Hello World</div></td></tr></tbody></table></div><!--[if mso | IE]></td></tr></table><![endif]--></td></tr></tbody></table></div><!--[if mso | IE]></td></tr></table><![endif]--></div></body></html>
}</style><style media="screen and (min-width:480px)">.moz-text-html .mj-column-per-100 { width:100% !important; max-width: 100%; }</style><style type="text/css"></style><style type="text/css"></style></head><body style="word-spacing:normal;"><div><!--[if mso | IE]><table align="center" border="0" cellpadding="0" cellspacing="0" class="" role="presentation" style="width:600px;" width="600" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]--><div style="margin:0px auto;max-width:600px;"><table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;"><tbody><tr><td style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;"><!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr><td class="" style="vertical-align:top;width:600px;" ><![endif]--><div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;"><table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%"><tbody><tr><td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"><div style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:13px;line-height:1;text-align:left;color:#000000;">Hello World</div></td></tr></tbody></table></div><!--[if mso | IE]></td></tr></table><![endif]--></td></tr></tbody></table></div><!--[if mso | IE]></td></tr></table><![endif]--></div></body></html>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!doctype html>
<html lang="und" dir="auto" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
<title></title>
<!--[if !mso]><!-->
Expand Down Expand Up @@ -48,17 +48,21 @@
.moz-text-html .mj-column-per-100 { width:100% !important; max-width: 100%; }
</style>



<style type="text/css">


</style>
<style type="text/css">

</style>

</head>
<body style="word-spacing:normal;">


<div
style="" lang="und" dir="auto"
style=""
>


Expand Down

0 comments on commit 5aefa29

Please sign in to comment.