@keycloakify/angular-email 0.0.1-rc.6 → 0.0.1-rc.8

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/bin/build.js ADDED
@@ -0,0 +1,153 @@
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: ['@maizzle/framework'],
24
+ format: 'esm',
25
+ target: 'node20',
26
+ plugins: [angularEsbuildPlugin(cwd)],
27
+ metafile: true,
28
+ });
29
+ // collect map of entrypoints -> output files
30
+ // esbuild return paths relative to the CWD
31
+ // https://github.com/evanw/esbuild/issues/338
32
+ return Object.entries(result.metafile.outputs).reduce((acc, [filePath, meta]) => {
33
+ if (meta.entryPoint) {
34
+ // Absolute pathes doesn't work on windows.
35
+ // Have to use `pathToFileURL` to convert it to url
36
+ acc[resolve(cwd, meta.entryPoint)] = pathToFileURL(resolve(cwd, filePath)).toString();
37
+ }
38
+ return acc;
39
+ }, {});
40
+ }
41
+ async function getTemplates(dirPath, filterTemplate) {
42
+ try {
43
+ const recursiveGetFiles = async (path) => {
44
+ const items = await readdir(path, { withFileTypes: true });
45
+ const dirents = [];
46
+ for (const item of items) {
47
+ if (item.isFile())
48
+ dirents.push(item);
49
+ else if (item.isDirectory()) {
50
+ dirents.push(...(await recursiveGetFiles(join(item.parentPath, item.name))));
51
+ }
52
+ }
53
+ return dirents;
54
+ };
55
+ // Read all items in the directory
56
+ const items = (await recursiveGetFiles(dirPath))
57
+ .map((file) => join(file.parentPath, file.name))
58
+ .filter((filePath) => {
59
+ if (!filterTemplate)
60
+ return true;
61
+ return filterTemplate(filePath);
62
+ });
63
+ return items;
64
+ }
65
+ catch (err) {
66
+ console.error(`Error scanning directory: ${err.message}`);
67
+ throw err;
68
+ }
69
+ }
70
+ const build = async (emailFilesPath, outdir = 'dist/emails') => {
71
+ if (!emailFilesPath) {
72
+ console.error('emailFilesPath is required!');
73
+ exit(1);
74
+ }
75
+ try {
76
+ console.log(`building emails to ${outdir}...`);
77
+ const tmp = join(cwd(), outdir, '.tmp');
78
+ await rm(outdir, { recursive: true, force: true });
79
+ const tpls = await getTemplates(resolve(cwd(), emailFilesPath), (path) => path.endsWith('.ts'));
80
+ if (!tpls.length) {
81
+ console.error('no templates found!');
82
+ exit(1);
83
+ }
84
+ const entryPoints = [...tpls];
85
+ const bundled = await bundle(entryPoints, cwd(), tmp);
86
+ const promises = tpls.map(async (file) => {
87
+ const module = await import(bundled[file]);
88
+ console.log(`- ${file}`);
89
+ return Object.assign(Object.assign({}, module), { file });
90
+ });
91
+ const modules = await Promise.all(promises);
92
+ modules.forEach(async (module) => {
93
+ const html = await module.renderToHtml();
94
+ await writeFile(`${outdir}/${basename(module.file).split('.')[0]}.html`, html);
95
+ });
96
+ await rm(tmp, { recursive: true, force: true });
97
+ console.log('\x1b[36m%s\x1b[0m', `build complete!`);
98
+ exit(0);
99
+ }
100
+ catch (error) {
101
+ console.error(error);
102
+ exit(1);
103
+ }
104
+ };
105
+ const program = termost({
106
+ name: 'keycloakify-angular-email',
107
+ description: 'Keycloakify CLI',
108
+ version: pkg.version,
109
+ onException: (error) => {
110
+ console.error(error);
111
+ process.exit(1);
112
+ },
113
+ });
114
+ program.option({
115
+ key: 'emailFilesPath',
116
+ name: { long: 'emailFilesPath', short: 'p' },
117
+ description: '',
118
+ defaultValue: '',
119
+ });
120
+ program.option({
121
+ key: 'outDir',
122
+ name: { long: 'outDir', short: 'o' },
123
+ description: '',
124
+ defaultValue: 'dist/emails',
125
+ });
126
+ program
127
+ .command({
128
+ name: 'build',
129
+ description: 'build angular components to html',
130
+ })
131
+ .input({
132
+ key: 'emailFilesPath',
133
+ type: 'text',
134
+ defaultValue: undefined,
135
+ label: "what's the path of your email components files?",
136
+ skip(context) {
137
+ return Boolean(context.emailFilesPath);
138
+ },
139
+ })
140
+ .input({
141
+ key: 'outDir',
142
+ type: 'text',
143
+ defaultValue: 'dist/emails',
144
+ label: 'where do you want to output html files?',
145
+ skip(context) {
146
+ return Boolean(context.outDir);
147
+ },
148
+ })
149
+ .task({
150
+ handler: async (context) => {
151
+ await build(context.emailFilesPath, context.outDir);
152
+ },
153
+ });
@@ -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;;;;"}