-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprocessMarkdown.js
More file actions
84 lines (71 loc) · 2.15 KB
/
Copy pathprocessMarkdown.js
File metadata and controls
84 lines (71 loc) · 2.15 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const https = require('https');
const fs = require('fs');
const { execSync } = require('child_process');
const API_KEY = process.env.API_KEY;
// const ENDPOINT_URL = new URL(process.env.ENDPOINT_URL);
const getLatestCommitId = () => {
return execSync('git rev-parse HEAD').toString().trim();
};
const getModifiedMarkdownFiles = () => {
try {
const lastCommit = getLatestCommitId()
console.log(lastCommit)
const child_process = require('child_process');
const modifiedFiles = child_process.execSync('git diff --name-only HEAD').toString().split('\n');
const gitLog = child_process.execSync('git log')
console.log("this is the git log", gitLog)
return modifiedFiles
} catch(e) {
return []
}
};
const getFilesFromCommit = (commitId) => {
const output = execSync(`git diff-tree --no-commit-id --name-only -r ${commitId}`).toString();
return output.split('\n').filter(file => file.endsWith('.md') && file.trim() !== '');
};
const sendRequest = (file, content) => {
return new Promise((resolve, reject) => {
const data = JSON.stringify({ content, filepath: file });
const options = {
hostname: ENDPOINT_URL.hostname,
port: ENDPOINT_URL.port,
path: ENDPOINT_URL.pathname,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': API_KEY,
'Content-Length': Buffer.byteLength(data)
}
};
const req = https.request(options, res => {
res.setEncoding('utf8');
res.on('data', d => {
process.stdout.write(d);
});
res.on('end', resolve);
});
req.on('error', error => {
console.error(error);
reject(error);
});
req.write(data);
req.end();
});
};
const main = async () => {
const latestCommitId = getLatestCommitId();
const files = getFilesFromCommit(latestCommitId);
console.log(latestCommitId)
console.log(files)
for (const file of files) {
if (file) {
const content = fs.readFileSync(file, 'utf8');
try {
// await sendRequest(file, content);
} catch (error) {
console.error(`Error processing file ${file}:`, error);
}
}
}
};
main();