generated from pagopa/io-template-typescript
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
58 lines (51 loc) · 1.79 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const path = require("path");
const gulp = require("gulp");
const textSimple = require("gulp-text-simple");
const rename = require("gulp-rename");
const mjml2html = require("mjml");
const prettier = require('gulp-prettier');
const TYPESCRIPT_SOURCE_DIR = "src";
const TEMPLATES_SOURCE_DIR = "templates/mjml";
const TEMPLATES_SOURCE = `${TEMPLATES_SOURCE_DIR}/*.mjml`;
const TEMPLATES_OUTPUT_DIR = `${TYPESCRIPT_SOURCE_DIR}/templates/html`;
/**
* Transform a mjml template to a Typescript function outputting HTML.
*
* @param content the content of mjml template as string
* @param options options object (see gulp-text-simple plugin https://www.npmjs.com/package/gulp-text-simple)
*/
const toMjml = (content, options) => {
const name = path.basename(options.sourcePath);
return [
`// DO NOT EDIT THIS FILE`,
`// this file was auto generated from '${name}' by gulp generate:templates`,
`// eslint-disable-next-line max-params`,
`export default function(`,
` title: string,`,
` headlineText: string,`,
` senderOrganizationName: string,`,
` senderServiceName: string,`,
` organizationFiscalCode: string,`,
` titleText: string,`,
` contentHtml: string,`,
` footerHtml: string,`,
` organizationLogoPath: string = "https://assets.cdn.io.italia.it/logos/organizations"`,
`): string {`,
` return \``,
`${mjml2html(content, { minify: true }).html}\`;`,
`}`,
""
].join("\n");
};
/**
* Generate Typescript template files from mjml (https://mjml.io/).
*/
gulp.task("generate:templates", () => {
return gulp
.src(TEMPLATES_SOURCE)
.pipe(textSimple(toMjml)())
.pipe(prettier())
.pipe(rename(filepath => (filepath.extname = ".ts")))
.pipe(gulp.dest(TEMPLATES_OUTPUT_DIR));
});
gulp.task("default", ["generate:templates"]);