feat: use buffers

This commit is contained in:
TrèsAbhi committed 2024-10-21 18:32:52 -05:00
1 parent c8bb1f190a
commit c98c19dc36
26 files changed
+134 -269

No files matched your search

@@ -1,64 +0,0 @@
import { assertSecret } from '@blitzkit/core';
import { GithubChangeBlob, createBlob } from './createBlob';
import { octokit } from './octokit';
export class AssetCommit {
constructor(public message: string) {}
private blobs: GithubChangeBlob[] = [];
async add(path: string, content: string, encoding: 'utf-8' | 'base64') {
console.log(`Blob: adding ${path}...`);
const blob = await createBlob(
'tresabhi',
assertSecret(process.env.PUBLIC_ASSET_REPO),
{
content,
encoding,
path,
},
);
this.blobs.push(blob!);
}
async push() {
if (this.blobs.length === 0) return;
const latestCommitSha = (
await octokit.git.getRef({
owner: 'tresabhi',
repo: assertSecret(process.env.PUBLIC_ASSET_REPO),
ref: `heads/${assertSecret(process.env.PUBLIC_ASSET_BRANCH)}`,
})
).data.object.sha;
const treeSha = (
await octokit.git.getCommit({
owner: 'tresabhi',
repo: assertSecret(process.env.PUBLIC_ASSET_REPO),
commit_sha: latestCommitSha,
})
).data.tree.sha;
const blobs: GithubChangeBlob[] = [];
const { data: treeData } = await octokit.git.createTree({
owner: 'tresabhi',
repo: assertSecret(process.env.PUBLIC_ASSET_REPO),
base_tree: treeSha,
tree: blobs,
});
const { data: newCommitData } = await octokit.git.createCommit({
owner: 'tresabhi',
repo: assertSecret(process.env.PUBLIC_ASSET_REPO),
message: this.message,
tree: treeData.sha,
parents: [latestCommitSha],
});
await octokit.git.updateRef({
owner: 'tresabhi',
repo: assertSecret(process.env.PUBLIC_ASSET_REPO),
ref: `heads/${assertSecret(process.env.PUBLIC_ASSET_BRANCH)}`,
sha: newCommitData.sha,
});
}
}
@@ -7,7 +7,6 @@ export async function commitAssets(message: string, changes: FileChange[]) {
if (changes.length === 0) return;
await commitMultipleFiles(
'tresabhi',
assertSecret(process.env.PUBLIC_ASSET_REPO),
assertSecret(process.env.PUBLIC_ASSET_BRANCH),
`${message} - ${new Date().toDateString()}`,
@@ -4,17 +4,16 @@ import { octokit } from './octokit';
export interface FileChange {
path: string;
content: string;
encoding: 'utf-8' | 'base64';
content: Buffer;
}
export async function commitMultipleFiles(
owner: string,
repo: string,
repoRaw: string,
branch: string,
message: string,
changes: FileChange[],
) {
const [owner, repo] = repoRaw.split('/');
const latestCommitSha = (
await octokit.git.getRef({
owner,
@@ -20,8 +20,8 @@ export const createBlob = throttle(
await octokit.git.createBlob({
owner,
repo,
content: change.content,
encoding: change.encoding,
content: change.content.toString('base64'),
encoding: 'base64',
})
).data;
@@ -34,7 +34,6 @@ export const createBlob = throttle(
} catch (error) {
console.warn(
`Failed blob ${change.path}; retrying in ${TIME_PER_BLOB}ms...`,
// error,
);
await new Promise((resolve) => setTimeout(resolve, TIME_PER_BLOB));