-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
97 lines (86 loc) · 2.84 KB
/
server.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
// Not a traditional server, these functions just provide a way to save current session to github
//const DEBUG_LEVEL = 1;
const GITHUB_API_URL = "https://api.github.com";
const GITHUB_REPO = "PubInv/nano-cap-table"; //CHANGE THIS to your github username
const GITHUB_FILE_PATH = "cap_table.json";
/**
function console.debug(message, level = 0) {
if (DEBUG_LEVEL <= level) console.trace(message);
}
*/
// Update page details function that sets repository link and table name
function updatePageDetails(name) {
$("#pageTitle").text(name + " - Nano Cap Table");
$("#repoLink")
.attr("href", `https://github.com/${GITHUB_REPO}`)
.text(`Visit ${name}`);
}
// Fetch cap_table.json from GitHub
async function fetchFromGitHub() {
try {
// Assuming GITHUB_FILE_URL directly points to the raw user content URL of the file in the GitHub repository
const GITHUB_FILE_URL = `https://raw.githubusercontent.com/${GITHUB_REPO}/main/${GITHUB_FILE_PATH}`;
const response = await fetch(GITHUB_FILE_URL);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.text();
const obj = JSON.parse(data);
const capTable = new NanoCapTable(obj.name);
capTable.table = obj.table;
console.debug("data" + data);
console.debug(capTable);
updatePageDetails(capTable.name);
return capTable;
} catch (error) {
console.error("Error fetching from GitHub:", error);
}
}
// Save cap_table.json to GitHub
async function saveToGitHub(capTable) {
const content = btoa(unescape(encodeURIComponent(JSON.stringify(capTable))));
const message = "Update cap table";
GITHUB_TOKEN = window.prompt("Enter a valid github token here");
try {
const response = await fetch(
`${GITHUB_API_URL}/repos/${GITHUB_REPO}/contents/${GITHUB_FILE_PATH}`,
{
headers: {
Authorization: `token ${GITHUB_TOKEN}`,
Accept: "application/vnd.github.v3+json",
},
}
);
if (!response.ok) {
throw new Error(`Failed to fetch file details: ${response.statusText}`);
}
const data = await response.json();
const sha = data.sha;
const updateResponse = await fetch(
`${GITHUB_API_URL}/repos/${GITHUB_REPO}/contents/${GITHUB_FILE_PATH}`,
{
method: "PUT",
headers: {
Authorization: `token ${GITHUB_TOKEN}`,
"Content-Type": "application/json",
},
body: JSON.stringify(
{
message: message,
content: content,
sha: sha,
},
null,
2
),
}
);
if (!updateResponse.ok) {
throw new Error(`Failed to update file: ${updateResponse.statusText}`);
}
alert("File updated successfully");
} catch (error) {
console.error(error);
alert(`Error saving to GitHub: ${error.message}`);
}
}