-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment-approval.js
110 lines (104 loc) · 3.45 KB
/
comment-approval.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const dotenv = await import('dotenv');
dotenv.config();
const { NetlifyAPI } = await import('netlify');
const process = await import('process');
const { default: prompt } = await import('prompt');
const { default: clear } = await import('clear');
const util = await import('util');
const { default: Handlebars } = await import('handlebars');
const fs = await import('fs');
import path from 'path';
import {fileURLToPath} from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const template = Handlebars.compile(
fs.readFileSync('./comment-template.md', 'utf8')
);
const siteId = process.env.NETLIFY_SITE_ID;
const token = process.env.NETLIFY_TOKEN;
const commentPath = `${__dirname}/content/comments`;
async function main() {
const client = new NetlifyAPI(token);
const formSubmissions = await client.listSiteSubmissions({ site_id: siteId });
if (formSubmissions.length === 0) {
console.log('No new comments available');
process.exit(0);
}
const schema = {
properties: {
option: {
pattern: /^[123]{1}$/,
description: 'Please Choose an Option',
message: 'Please enter a valid option',
required: true,
},
},
};
const continueSchema = {
properties: {
continue: {
description: 'Press any key to continue',
},
},
};
prompt.start();
for (const submission of formSubmissions) {
clear();
try {
console.log('Current Comment Submission');
console.log('================');
console.log(`Name: ${submission.data.name}`);
console.log(`Page: ${submission.data.slug}`);
console.log(`Date: ${submission.created_at}`);
console.log(`Gdpr: ${submission.data.gdpr}`);
console.log('Comment:');
console.log(submission.data.comment);
console.log('');
console.log('----------------');
console.log('Select an Option');
console.log('1. Skip comment');
console.log('2. Approve comment');
console.log('3. Remove comment (irreversible)');
const result = await util.promisify(prompt.get)(schema);
const value = Number.parseInt(result.option, 10);
switch (value) {
case 1:
console.log('Current comment was skipped');
break;
case 2:
if (!submission.data.gdpr) {
console.log('Invalid GDPR flag');
break;
}
const contents = template({
name: submission.data.name,
slug: submission.data.slug,
date: submission.created_at,
comment: submission.data.comment,
});
const directory = `${commentPath}${submission.data.slug}`;
if (!fs.existsSync(directory)) {
await util.promisify(fs.mkdir)(directory, { recursive: true });
}
const fileName = `entry-${submission.id}.md`;
const path = `${directory}${fileName}`;
await util.promisify(fs.writeFile)(path, contents);
console.log(`Comment successfully saved in: ${path}`);
break;
case 3:
await client.deleteSubmission({ submission_id: submission.id });
console.log('Comment successfully deleted');
break;
default:
console.log('default');
}
} catch (error) {
console.error(error);
}
await util.promisify(prompt.get)(continueSchema);
}
console.log('');
console.log('No more comments available');
process.exit(0);
}
main();