-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.ts
More file actions
48 lines (44 loc) · 1.37 KB
/
utils.ts
File metadata and controls
48 lines (44 loc) · 1.37 KB
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
import {dirname, join} from 'node:path'
import {readdirSync} from 'node:fs'
import {fileURLToPath} from "node:url";
/**
* Get the directories names
*
* Properly works only in Node.js environment
* @param directory
*/
export function getDirectoriesNames(directory: string) {
return readdirSync(directory, {withFileTypes: true})
.filter(dir => dir.isDirectory()).map(({name}) => name)
}
/**
* Get the file path by type
*
* Properly works only in Node.js environment
* @param directory
* @param type
*/
export function getFilePathByType(directory: string, type: string) {
const file = readdirSync(directory, {withFileTypes: true}).filter(file => {
return file.isFile() && file.name.endsWith(type)
}).map(({name}) => name)
return `${directory}/${file}`
}
/**
* Get the email template as JSON by category and template name
*
* Properly works only in Node.js environment
* @param category Category
* @param template Template name
*/
export function getEmailJSON(category: string, template: string) {
return require(getFilePathByType(join(dirname(fileURLToPath(import.meta.url)), 'templates', category, template), '.json'));
}
/**
* Get all email templates as JSON
*
* Properly works only in Node.js environment
*/
export function getAllTemplatesAsJSON() {
return require(getFilePathByType(join(dirname(fileURLToPath(import.meta.url)), 'templates'), '.json'));
}