better-svelte-email 0.3.4 → 0.3.6

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.
@@ -34,6 +34,7 @@ type EmailListProps = {
34
34
  * ```
35
35
  */
36
36
  export declare const emailList: ({ path: emailPath, root }?: EmailListProps) => PreviewData;
37
+ export declare const getEmailComponent: (emailPath: string, file: string) => Promise<any>;
37
38
  /**
38
39
  * SvelteKit form action to render an email component.
39
40
  * Use this with the Preview component to render email templates on demand.
@@ -100,4 +101,5 @@ export declare const sendEmail: ({ customSendEmailFunction, resendApiKey }?: {
100
101
  error: any;
101
102
  }>;
102
103
  };
104
+ export declare function getFiles(dir: string, files?: string[]): string[];
103
105
  export { default as EmailPreview } from './EmailPreview.svelte';
@@ -31,8 +31,9 @@ export const emailList = ({ path: emailPath = '/src/lib/emails', root } = {}) =>
31
31
  try {
32
32
  root = process.cwd();
33
33
  }
34
- catch {
35
- throw new Error('Could not determine the root path of your project. Please pass in the root param manually using process.cwd() or an absolute path');
34
+ catch (err) {
35
+ throw new Error('Could not determine the root path of your project. Please pass in the root param manually using process.cwd() or an absolute path.\nOriginal error: ' +
36
+ err);
36
37
  }
37
38
  }
38
39
  const fullPath = path.join(root, emailPath);
@@ -41,19 +42,25 @@ export const emailList = ({ path: emailPath = '/src/lib/emails', root } = {}) =>
41
42
  console.warn(`Email directory not found: ${fullPath}`);
42
43
  return { files: null, path: emailPath };
43
44
  }
44
- const files = createEmailComponentList(emailPath, getFiles(fullPath));
45
+ // Use the absolute folder path as the root when creating the component list so
46
+ // we can compute correct relative paths on all platforms.
47
+ const files = createEmailComponentList(fullPath, getFiles(fullPath));
45
48
  if (!files.length) {
46
49
  return { files: null, path: emailPath };
47
50
  }
48
51
  return { files, path: emailPath };
49
52
  };
50
- const getEmailComponent = async (emailPath, file) => {
53
+ export const getEmailComponent = async (emailPath, file) => {
54
+ const fileName = `${file}.svelte`;
51
55
  try {
52
56
  // Import the email component dynamically
53
- return (await import(/* @vite-ignore */ `${emailPath}${path.sep}${file}.svelte`)).default;
57
+ const normalizedEmailPath = emailPath.replace(/\\/g, '/').replace(/\/+$/, '');
58
+ const normalizedFile = file.replace(/\\/g, '/').replace(/^\/+/, '');
59
+ const importPath = `${normalizedEmailPath}/${normalizedFile}.svelte`;
60
+ return (await import(/* @vite-ignore */ importPath)).default;
54
61
  }
55
- catch {
56
- throw new Error(`Failed to import email component '${file}'. Make sure the file exists and includes the <Head /> component.`);
62
+ catch (err) {
63
+ throw new Error(`Failed to import email component '${fileName}'. Make sure the file exists and includes the <Head /> component.\nOriginal error: ${err}`);
57
64
  }
58
65
  };
59
66
  /**
@@ -177,12 +184,12 @@ export const sendEmail = ({ customSendEmailFunction, resendApiKey } = {}) => {
177
184
  };
178
185
  };
179
186
  // Recursive function to get files
180
- function getFiles(dir, files = []) {
187
+ export function getFiles(dir, files = []) {
181
188
  // Get an array of all files and directories in the passed directory using fs.readdirSync
182
189
  const fileList = fs.readdirSync(dir);
183
190
  // Create the full path of the file/directory by concatenating the passed directory and file/directory name
184
191
  for (const file of fileList) {
185
- const name = `${dir}${path.sep}${file}`;
192
+ const name = path.join(dir, file);
186
193
  // Check if the current file/directory is a directory using fs.statSync
187
194
  if (fs.statSync(name).isDirectory()) {
188
195
  // If it is a directory, recursively call the getFiles function with the directory path and the files array
@@ -201,9 +208,25 @@ function getFiles(dir, files = []) {
201
208
  function createEmailComponentList(root, paths) {
202
209
  const emailComponentList = [];
203
210
  paths.forEach((filePath) => {
204
- if (filePath.includes(`.svelte`)) {
205
- const fileName = filePath.substring(filePath.indexOf(root) + root.length + 1, filePath.indexOf('.svelte'));
206
- emailComponentList.push(fileName);
211
+ if (filePath.endsWith('.svelte')) {
212
+ // Get the directory name from the full path
213
+ const fileDir = path.dirname(filePath);
214
+ // Get the base name without extension
215
+ const baseName = path.basename(filePath, '.svelte');
216
+ // Normalize paths for cross-platform comparison
217
+ const rootNormalized = path.normalize(root);
218
+ const fileDirNormalized = path.normalize(fileDir);
219
+ // Find where root appears in the full directory path
220
+ const rootIndex = fileDirNormalized.indexOf(rootNormalized);
221
+ if (rootIndex !== -1) {
222
+ // Get everything after the root path
223
+ const afterRoot = fileDirNormalized.substring(rootIndex + rootNormalized.length);
224
+ // Combine with the base name using path.join for proper separators
225
+ const relativePath = afterRoot ? path.join(afterRoot, baseName) : baseName;
226
+ // Remove leading path separators
227
+ const cleanPath = relativePath.replace(/^[/\\]+/, '');
228
+ emailComponentList.push(cleanPath);
229
+ }
207
230
  }
208
231
  });
209
232
  return emailComponentList;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "better-svelte-email",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "author": "Konixy",
5
5
  "repository": {
6
6
  "type": "git",