Mjml: When using as npm script, multiple file inputs with includes built to custom output folder exits with error

Created on 6 Mar 2018  路  3Comments  路  Source: mjmlio/mjml

When attempting to build mjml using NPM Scripts with multiple input files and includes, with a custom output folder, the build exits with error.

Reproduction Steps:

  1. npm install --save mjml

    1. In my package.json in the scripts section:

      "email-build": "mjml ./assets/templates/mjml/*.mjml -o ./assets/templates/emails/",

    2. npm run email-build

Expected behavior:
All .mjml files included in input folder are compiled (with includes) and written to output folder defined

Observed behavior:
Build fails with the following error:

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! email-build: `mjml ./assets/templates/mjml/*.mjml -o ./assets/templates/emails/`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the  email-build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

As far as I can tell, it is failing because replaceExtension function in outputToFile.js in the CLI pacakge is returning the full path in this case, so I end up with a path like ./assets/templates/mjml/assets/templates/emails/welcome if welcome.mjml is a given template, causing the file to not write and the build to error out.

FWIW, the client errors out at case 37 after sending files to outputToFile in client.js

If I change the replaceExtension function to replace .mjml with .html and drop the rest of the path, this appears to work, but I haven't tested with includes or files without an output directory specified.

var replaceExtensionForCustomOutputLocation = function replaceExtension(input) {
  var replace = input.replace('.mjml', '.html');
  var output = replace.match(/\w+(?:\.\w+)*$/g);
  return output[0];
};

Note: Doing everything the same but updating the input and output args in package.json to point to a single file works as expected.

MJML version:

4.0.0

Node version:

v8.1.2

Bug CLI MJML 4

Most helpful comment

Thanks for the fix guys.
Tested quickly on 4.0.2 it works as expected.

I'm not sure thought if it supposed to maintain folder structure or not.
mjml templates/**/*.mjml -o dist/
will output all the files in templates and the folders within, in dist folder.

All 3 comments

Any idea when it will be fixed / released ?
Thanks

Installed and tested 4.0.2 鈥斅爀verything appears to be working. Thanks for the update! 馃専

Thanks for the fix guys.
Tested quickly on 4.0.2 it works as expected.

I'm not sure thought if it supposed to maintain folder structure or not.
mjml templates/**/*.mjml -o dist/
will output all the files in templates and the folders within, in dist folder.

Was this page helpful?
0 / 5 - 0 ratings