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
import { API_URL } from "https://esm.town/v/std/API_URL?v=5";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=42";
import { Octokit } from "npm:@octokit/rest";
import { DateTime } from "npm:luxon";
async function getLatestCommit(ghUser: string, ghRepo: string, branch: string, client: Octokit)
{
const { data: refData } = await client.rest.git.getRef({
owner: ghUser,
repo: ghRepo,
ref: `heads/${branch}`,
});
return refData.object.sha;
}
async function createNewTree(
ghUser: string,
ghRepo: string,
tree: {
path?: string;
mode?: "100644" | "100755" | "040000" | "160000" | "120000";
type?: "tree" | "commit" | "blob";
sha?: string;
content?: string;
}[],
commitSHA: string,
client: Octokit,
) {
const {
data: { sha: currentTreeSHA },
} = await client.rest.git.createTree({
owner: ghUser,
repo: ghRepo,
tree,
base_tree: commitSHA,
parents: [commitSHA],
});
return currentTreeSHA;
}
async function createNewCommit(
ghUser: string,
ghRepo: string,
commitSHA: string,
currentTreeSHA: string,
message: string,
client: Octokit,
) {
const {
data: { sha: newCommitSHA },
} = await client.rest.git.createCommit({
owner: ghUser,
repo: ghRepo,
tree: currentTreeSHA,
message: message,
parents: [commitSHA],
});
return newCommitSHA;
}
async function updateBranchRef(
owner: string,
repo: string,
newCommitSHA: string,
branch: string = "main",
client: Octokit,
) {
const result = await client.rest.git.updateRef({
owner,
repo,
ref: `heads/${branch}`,
sha: newCommitSHA,
});
return result;
}
type Options = { branch?: string; prefix?: string; message?: string; ts?: boolean };
/**
* Commits a value or an array of values to a GitHub repository.
*
* @param {string} repo - The GitHub repository in the format of {owner}/{repo}.
* @param {string | string[]} val - A single value or array of values in the format of {user}/{val}.
* @param {string} ghToken - The GitHub token for authentication.
* @param {Options} [options] - Options to configure the commit.
* @param {string} [options.branch="main"] - The branch to commit to.
* @param {string} [options.prefix=""] - Prefix to be added to the file path.
* @param {string} [options.message="<current date and time> UTC"] - The commit message.
* @param {boolean} [options.ts=true] - Whether to use TypeScript or JavaScript.
* @returns {Promise<any>} - The result of the commit.
*/
export async function valToGH(
repo: string, // owner/repo
val: string | string[], // user/val
ghToken: string,
options?: Options,
) {
const client = new Octokit({ auth: ghToken });
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
April 11, 2024
Is this meant to be like this or are you going to update this?
no that's an oversight, it was written by my readme Writer
lol!
this is awesome, I will poke around. I've been dying to be able to just do
git clone https://git.val.town/@jamiedubs
so i can do local-ish val shenanigansYou can also use @pomdtr's vscode extension for that!
omg a cli too! @pomdtr ftw