@keycloakify/angular-email 0.0.1-rc.2 → 0.0.1-rc.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Angular Email
2
2
 
3
- Angular Email is a library that enables email template development using Angular, inspired by [jsx-email](https://github.com/rezqio/jsx-email). It provides a way to create email components using Angular's templating system.
3
+ Angular Email is a library that enables email template development using Angular, inspired by [jsx-email](https://github.com/shellscape/jsx-email). It provides a way to create email components using Angular's templating system.
4
4
 
5
5
  ## Installation
6
6
 
@@ -23,22 +23,69 @@ see this [example](https://github.com/keycloakify/angular-email/blob/main/projec
23
23
  To render the email as HTML or plaintext, use Angular's rendering engine:
24
24
 
25
25
  ```typescript
26
+ // email.component.ts
26
27
  ...
27
- import { render } from '@keycloakify/angular-email';
28
+ import { render, RenderToHtml } from '@keycloakify/angular-email';
28
29
 
29
- const emailHtml = render({
30
- component: AppComponent,
31
- selector: 'app-root',
32
- options: {
33
- pretty: true,
34
- },
35
- }).then((emailHtml) => {
36
- console.log(emailHtml);
37
- });
30
+ ...
31
+ export class EmailComponent {
32
+ ....
33
+ }
34
+
35
+ type EmailComponentProps = {};
36
+
37
+ export const renderToHtml: RenderToHtml<EmailComponentProps> = (props) => {
38
+ return render({
39
+ component: EmailComponent,
40
+ selector: 'app-root',
41
+ props,
42
+ options: {
43
+ pretty: true,
44
+ },
45
+ });
46
+ };
38
47
  ```
39
48
 
49
+ ```sh
50
+ # cmd
51
+
52
+ export $EMAIL_COMPONENTS_DIR_PATH="src/emails"
53
+ npx keycloakify-angular-email build -p $EMAIL_COMPONENTS_DIR_PATH
54
+ ```
55
+
56
+ NB: use `keycloakify-angular-email build` when you don't need to pass dynamic inputs to your components, otherwise see [Standalone Dynamic Rendering](#standalone-dynamic-rendering)
57
+
40
58
  ### With Keycloakify Emails
41
59
 
60
+ ```ts
61
+ // email.component.ts
62
+ ...
63
+ import { render } from '@keycloakify/angular-email';
64
+ import type { GetSubject, GetTemplate } from 'keycloakify-emails';
65
+
66
+ ...
67
+ export class EmailComponent {
68
+ ....
69
+ }
70
+
71
+ export const getTemplate: GetTemplate = async (props) => {
72
+ return await render({
73
+ component: EmailComponent,
74
+ props,
75
+ selector: 'kc-email-test',
76
+ options: {
77
+ signalInputsPrefix: '$',
78
+ pretty: true,
79
+ plainText: props.plainText,
80
+ },
81
+ });
82
+ };
83
+
84
+ export const getSubject: GetSubject = async (_props) => {
85
+ return '[KEYCLOAK] - SMTP test message';
86
+ };
87
+ ```
88
+
42
89
  ```ts
43
90
  // vite.config.ts
44
91
 
@@ -55,14 +102,16 @@ export default defineConfig(({ mode }) => ({
55
102
  postBuild: async (buildContext) => {
56
103
  await buildEmailTheme({
57
104
  templatesSrcDirPath: join(import.meta.dirname, '/emails/templates'),
58
- filterTemplate: (filePath: string) => !filePath.endsWith('.html'),
105
+ filterTemplate: (filePath: string) => !!filePath.endsWith('.component.ts'),
59
106
  themeNames: buildContext.themeNames,
60
107
  keycloakifyBuildDirPath: buildContext.keycloakifyBuildDirPath,
61
108
  locales: ['en'],
62
109
  cwd: import.meta.dirname,
63
110
  esbuild: {
64
111
  packages: 'bundle',
65
- external: ['cheerio'],
112
+ external: ['juice', 'postcss'],
113
+ format: 'cjs',
114
+ outExtension: { '.js': '.cjs' },
66
115
  plugins: [angularEsbuildPlugin(import.meta.dirname)],
67
116
  },
68
117
  });
@@ -72,6 +121,104 @@ export default defineConfig(({ mode }) => ({
72
121
  }));
73
122
  ```
74
123
 
124
+ ### Standalone Dynamic Rendering
125
+
126
+ Use it in a server environment
127
+
128
+ ```js
129
+ // index.mjs
130
+
131
+ import { toHTML } from '@keycloakify/angular-email/node';
132
+
133
+ toHTML({
134
+ filePath: 'path/to/your.component.ts',
135
+ props: { foo: 'bar' },
136
+ })
137
+ .then((html) => {
138
+ console.log(html);
139
+ })
140
+ .catch((e) => {
141
+ console.error(e);
142
+ });
143
+ ```
144
+
145
+ or
146
+
147
+ ```js
148
+ // index.cjs
149
+
150
+ const { toHTML } = require('@keycloakify/angular-email/node');
151
+
152
+ toHTML({
153
+ filePath: 'path/to/your.component.ts',
154
+ props: { foo: 'bar' },
155
+ })
156
+ .then((html) => {
157
+ console.log(html);
158
+ })
159
+ .catch((e) => {
160
+ console.error(e);
161
+ });
162
+ ```
163
+
164
+ ```sh
165
+ # cmd
166
+
167
+ node index.mjs # or node index.cjs
168
+ ```
169
+
170
+ ## API
171
+
172
+ ### @keycloakify/angular-email
173
+
174
+ ### Render
175
+
176
+ ```ts
177
+ type Render<Input extends Record<string, any>> = {
178
+ component: Type<unknown>;
179
+ /** Component selector */
180
+ selector: string;
181
+ /** Component inputs */
182
+ props?: Input;
183
+ options?: {
184
+ /** render as text */
185
+ plainText?: boolean;
186
+ /** format the html output */
187
+ pretty?: boolean;
188
+ /** tailwind configuration object */
189
+ tailwindConfig?: Partial<Config>;
190
+ /** if you use prefix conventions on signal inputs */
191
+ signalInputsPrefix?: string;
192
+ };
193
+ };
194
+ ```
195
+
196
+ ### render()
197
+
198
+ ```ts
199
+ render<Input extends Record<string, any>>({ component, selector, props, options }: Render<Input>) => Promise<string>
200
+ ```
201
+
202
+ ### @keycloakify/angular-email/esbuild
203
+
204
+ ### angularEsbuildPlugin()
205
+
206
+ ```ts
207
+ angularEsbuildPlugin(cwd: string) => Plugin
208
+ ```
209
+
210
+ ### @keycloakify/angular-email/node
211
+
212
+ ### toHTML()
213
+
214
+ ```ts
215
+ toHTML<Input extends Record<string, any>>(options: {
216
+ filePath: string;
217
+ props?: Input;
218
+ root?: string;
219
+ }) => Promise<string>
220
+ ```
221
+
75
222
  ## Contributing
76
223
 
77
224
  Contributions are welcome! Feel free to open an issue or submit a pull request on [GitHub](https://github.com/keycloakify/angular-email).
package/bin/build.mjs ADDED
@@ -0,0 +1,154 @@
1
+ #!/usr/bin/env node
2
+ import { angularEsbuildPlugin } from '@keycloakify/angular-email/esbuild';
3
+ import esbuild from 'esbuild';
4
+ import { readdir, rm, writeFile } from 'node:fs/promises';
5
+ import { basename, join, resolve } from 'node:path';
6
+ import { cwd, exit } from 'node:process';
7
+ import { pathToFileURL } from 'node:url';
8
+ import { termost } from 'termost';
9
+ import pkg from '../package.json' with { type: 'json' };
10
+ async function bundle(entryPoints, cwd, outdir) {
11
+ // we have to use a bundler to preprocess templates code
12
+ // It's better to not use the same bundling configuration used for the
13
+ // frontend theme, because for email templates there might be a different
14
+ // transpiling settings, such as other jsx-pragma or additional transpilation plugins,
15
+ const result = await esbuild.build({
16
+ entryPoints: entryPoints,
17
+ bundle: true,
18
+ outdir,
19
+ absWorkingDir: cwd,
20
+ platform: 'node',
21
+ sourcemap: true,
22
+ packages: 'bundle',
23
+ external: ['juice', 'postcss'],
24
+ format: 'cjs',
25
+ outExtension: { '.js': '.cjs' },
26
+ target: 'node20',
27
+ plugins: [angularEsbuildPlugin(cwd)],
28
+ metafile: true,
29
+ });
30
+ // collect map of entrypoints -> output files
31
+ // esbuild return paths relative to the CWD
32
+ // https://github.com/evanw/esbuild/issues/338
33
+ return Object.entries(result.metafile.outputs).reduce((acc, [filePath, meta]) => {
34
+ if (meta.entryPoint) {
35
+ // Absolute pathes doesn't work on windows.
36
+ // Have to use `pathToFileURL` to convert it to url
37
+ acc[resolve(cwd, meta.entryPoint)] = pathToFileURL(resolve(cwd, filePath)).toString();
38
+ }
39
+ return acc;
40
+ }, {});
41
+ }
42
+ async function getTemplates(dirPath, filterTemplate) {
43
+ try {
44
+ const recursiveGetFiles = async (path) => {
45
+ const items = await readdir(path, { withFileTypes: true });
46
+ const dirents = [];
47
+ for (const item of items) {
48
+ if (item.isFile())
49
+ dirents.push(item);
50
+ else if (item.isDirectory()) {
51
+ dirents.push(...(await recursiveGetFiles(join(item.parentPath, item.name))));
52
+ }
53
+ }
54
+ return dirents;
55
+ };
56
+ // Read all items in the directory
57
+ const items = (await recursiveGetFiles(dirPath))
58
+ .map((file) => join(file.parentPath, file.name))
59
+ .filter((filePath) => {
60
+ if (!filterTemplate)
61
+ return true;
62
+ return filterTemplate(filePath);
63
+ });
64
+ return items;
65
+ }
66
+ catch (err) {
67
+ console.error(`Error scanning directory: ${err.message}`);
68
+ throw err;
69
+ }
70
+ }
71
+ const build = async (emailFilesPath, outdir = 'dist/emails') => {
72
+ if (!emailFilesPath) {
73
+ console.error('emailFilesPath is required!');
74
+ exit(1);
75
+ }
76
+ try {
77
+ console.log(`building emails to ${outdir}...`);
78
+ const tmp = join(cwd(), outdir, '.tmp');
79
+ await rm(outdir, { recursive: true, force: true });
80
+ const tpls = await getTemplates(resolve(cwd(), emailFilesPath), (path) => path.endsWith('.ts'));
81
+ if (!tpls.length) {
82
+ console.error('no templates found!');
83
+ exit(1);
84
+ }
85
+ const entryPoints = [...tpls];
86
+ const bundled = await bundle(entryPoints, cwd(), tmp);
87
+ const promises = tpls.map(async (file) => {
88
+ const module = await import(bundled[file]);
89
+ console.log(`- ${file}`);
90
+ return Object.assign(Object.assign({}, module), { file });
91
+ });
92
+ const modules = await Promise.all(promises);
93
+ modules.forEach(async (module) => {
94
+ const html = await module.renderToHtml();
95
+ await writeFile(`${outdir}/${basename(module.file).split('.')[0]}.html`, html);
96
+ });
97
+ await rm(tmp, { recursive: true, force: true });
98
+ console.log('\x1b[36m%s\x1b[0m', `build complete!`);
99
+ exit(0);
100
+ }
101
+ catch (error) {
102
+ console.error(error);
103
+ exit(1);
104
+ }
105
+ };
106
+ const program = termost({
107
+ name: 'keycloakify-angular-email',
108
+ description: 'Keycloakify CLI',
109
+ version: pkg.version,
110
+ onException: (error) => {
111
+ console.error(error);
112
+ exit(1);
113
+ },
114
+ });
115
+ program.option({
116
+ key: 'emailFilesPath',
117
+ name: { long: 'emailFilesPath', short: 'p' },
118
+ description: '',
119
+ defaultValue: '',
120
+ });
121
+ program.option({
122
+ key: 'outDir',
123
+ name: { long: 'outDir', short: 'o' },
124
+ description: '',
125
+ defaultValue: 'dist/emails',
126
+ });
127
+ program
128
+ .command({
129
+ name: 'build',
130
+ description: 'build angular components to html',
131
+ })
132
+ .input({
133
+ key: 'emailFilesPath',
134
+ type: 'text',
135
+ defaultValue: undefined,
136
+ label: "what's the path of your email components files?",
137
+ skip(context) {
138
+ return Boolean(context.emailFilesPath);
139
+ },
140
+ })
141
+ .input({
142
+ key: 'outDir',
143
+ type: 'text',
144
+ defaultValue: 'dist/emails',
145
+ label: 'where do you want to output html files?',
146
+ skip(context) {
147
+ return Boolean(context.outDir);
148
+ },
149
+ })
150
+ .task({
151
+ handler: async (context) => {
152
+ await build(context.emailFilesPath, context.outDir);
153
+ },
154
+ });
@@ -1 +1,2 @@
1
- export declare const angularEsbuildPlugin: (cwd: string) => import("esbuild").Plugin;
1
+ import { Plugin } from 'esbuild';
2
+ export declare const angularEsbuildPlugin: (cwd: string) => Plugin;
@@ -13,10 +13,10 @@ const angularEsbuildPlugin = (cwd) => createCompilerPlugin({
13
13
  },
14
14
  inlineFonts: false,
15
15
  inlineStyleLanguage: 'css',
16
- optimization: false,
16
+ optimization: true,
17
17
  sourcemap: false,
18
18
  target: [],
19
- workspaceRoot: '',
19
+ workspaceRoot: cwd,
20
20
  outputNames: { bundles: '', media: '' },
21
21
  });
22
22
 
@@ -1 +1 @@
1
- {"version":3,"file":"keycloakify-angular-email-esbuild.mjs","sources":["../../../projects/angular-email/esbuild/esbuild.ts","../../../projects/angular-email/esbuild/keycloakify-angular-email-esbuild.ts"],"sourcesContent":["import { createCompilerPlugin } from '@angular/build/private';\n\nexport const angularEsbuildPlugin = (cwd: string) =>\n createCompilerPlugin(\n {\n browserOnlyBuild: false,\n incremental: false,\n sourcemap: true,\n tsconfig: cwd + '/tsconfig.json',\n },\n {\n cacheOptions: {\n basePath: cwd + '/.angular/cache',\n enabled: false,\n path: cwd + '/.angular/cache/angular-email',\n },\n inlineFonts: false,\n inlineStyleLanguage: 'css',\n optimization: false,\n sourcemap: false,\n target: [],\n workspaceRoot: '',\n outputNames: { bundles: '', media: '' },\n },\n );\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;AAEa,MAAA,oBAAoB,GAAG,CAAC,GAAW,KAC9C,oBAAoB,CAClB;AACE,IAAA,gBAAgB,EAAE,KAAK;AACvB,IAAA,WAAW,EAAE,KAAK;AAClB,IAAA,SAAS,EAAE,IAAI;IACf,QAAQ,EAAE,GAAG,GAAG,gBAAgB;CACjC,EACD;AACE,IAAA,YAAY,EAAE;QACZ,QAAQ,EAAE,GAAG,GAAG,iBAAiB;AACjC,QAAA,OAAO,EAAE,KAAK;QACd,IAAI,EAAE,GAAG,GAAG,+BAA+B;AAC5C,KAAA;AACD,IAAA,WAAW,EAAE,KAAK;AAClB,IAAA,mBAAmB,EAAE,KAAK;AAC1B,IAAA,YAAY,EAAE,KAAK;AACnB,IAAA,SAAS,EAAE,KAAK;AAChB,IAAA,MAAM,EAAE,EAAE;AACV,IAAA,aAAa,EAAE,EAAE;IACjB,WAAW,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE;AACxC,CAAA;;ACvBL;;AAEG;;;;"}
1
+ {"version":3,"file":"keycloakify-angular-email-esbuild.mjs","sources":["../../../projects/angular-email/esbuild/esbuild.ts","../../../projects/angular-email/esbuild/keycloakify-angular-email-esbuild.ts"],"sourcesContent":["import { createCompilerPlugin } from '@angular/build/private';\nimport { Plugin } from 'esbuild';\n\nexport const angularEsbuildPlugin = (cwd: string): Plugin =>\n createCompilerPlugin(\n {\n browserOnlyBuild: false,\n incremental: false,\n sourcemap: true,\n tsconfig: cwd + '/tsconfig.json',\n },\n {\n cacheOptions: {\n basePath: cwd + '/.angular/cache',\n enabled: false,\n path: cwd + '/.angular/cache/angular-email',\n },\n inlineFonts: false,\n inlineStyleLanguage: 'css',\n optimization: true,\n sourcemap: false,\n target: [],\n workspaceRoot: cwd,\n outputNames: { bundles: '', media: '' },\n },\n );\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;AAGa,MAAA,oBAAoB,GAAG,CAAC,GAAW,KAC9C,oBAAoB,CAClB;AACE,IAAA,gBAAgB,EAAE,KAAK;AACvB,IAAA,WAAW,EAAE,KAAK;AAClB,IAAA,SAAS,EAAE,IAAI;IACf,QAAQ,EAAE,GAAG,GAAG,gBAAgB;CACjC,EACD;AACE,IAAA,YAAY,EAAE;QACZ,QAAQ,EAAE,GAAG,GAAG,iBAAiB;AACjC,QAAA,OAAO,EAAE,KAAK;QACd,IAAI,EAAE,GAAG,GAAG,+BAA+B;AAC5C,KAAA;AACD,IAAA,WAAW,EAAE,KAAK;AAClB,IAAA,mBAAmB,EAAE,KAAK;AAC1B,IAAA,YAAY,EAAE,IAAI;AAClB,IAAA,SAAS,EAAE,KAAK;AAChB,IAAA,MAAM,EAAE,EAAE;AACV,IAAA,aAAa,EAAE,GAAG;IAClB,WAAW,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE;AACxC,CAAA;;ACxBL;;AAEG;;;;"}