-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (56 loc) · 1.97 KB
/
index.js
File metadata and controls
65 lines (56 loc) · 1.97 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const core = require('@actions/core');
const mime = require('mime');
const { email } = require('@cinotify/js');
const { readFileSync, lstatSync, readdirSync, statSync } = require('fs');
const { join, relative, basename } = require('path');
try {
const to = core.getInput('to');
const subject = core.getInput('subject');
const body = core.getInput('body');
const payload = {to, subject, body};
const attachmentPath = core.getInput('attachment');
if (attachmentPath) {
const isDirectory = lstatSync(attachmentPath).isDirectory();
if (isDirectory) {
const files = getFilesFromDirectory(attachmentPath);
files.forEach(filePath => {
const fileContent = readFileSync(filePath);
const attachment = {
filename: basename(relative(attachmentPath, filePath)),
type: mime.getType(filePath),
content: fileContent.toString('base64')
}
payload.attachments.push(attachment);
});
} else {
const file = readFileSync(attachmentPath);
const attachment = {
filename: basename(attachmentPath),
type: mime.getType(attachmentPath),
content: file.toString('base64')
}
payload.attachments = [attachment];
}
}
email(payload).then(() => {
core.setOutput("status", "ok")
}).catch(err => {
core.setFailed(err.message)
})
} catch (error) {
core.setFailed(error.message);
}
function getFilesFromDirectory(dir) {
const files = readdirSync(dir);
let fileList = [];
files.forEach(file => {
const filePath = join(dir, file);
const stats = statSync(filePath);
if (stats.isDirectory()) {
fileList = fileList.concat(getFilesFromDirectory(filePath));
} else {
fileList.push(filePath);
}
});
return fileList;
}