Skip to content

Update

Update #571

Workflow file for this run

name: Update
on:
workflow_dispatch:
schedule:
- cron: "0 0 * * *"
jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Update files
uses: actions/github-script@v7
with:
script: |
const { createWriteStream } = require("fs");
const { mkdir, readFile } = require("fs/promises");
const { dirname } = require("path");
const { Readable } = require("stream");
const { pipeline } = require("stream/promises");
async function handleRepo(owner, repo, ref, path) {
const content = await github.rest.repos.getContent({
owner,
repo,
path,
ref,
});
const response = await fetch(content.data.download_url);
return response.body;
}
async function handleRelease(owner, repo, assetName) {
const release = await github.rest.repos.getLatestRelease({
owner,
repo,
});
const asset = release.data.assets.find((asset) => asset.name === assetName);
const response = await fetch(asset.browser_download_url);
return response.body;
}
async function handleGist(owner, gist_id, fileName) {
// const gist = await github.rest.gists.get({ gist_id });
// const response = await fetch(gist.data.files[fileName].raw_url);
const response = await fetch(
`https://gist.githubusercontent.com/${owner}/${gist_id}/raw/${fileName}`
);
return response.body;
}
async function* dirIter(owner, repo, ref, source, destination) {
const content = await github.rest.repos.getContent({
owner,
repo,
path: source.replace(/^\/+|\/+$/g, ""),
ref,
});
for (const { type, name } of content.data) {
if (type === "file") {
yield {
newSource: `${source}${name}`,
newDestination: `${destination}${name}`,
};
} else if (type === "dir") {
for await (const { newSource, newDestination } of dirIter(
owner,
repo,
ref,
`${source}${name}/`,
`${destination}${name}/`
)) {
yield { newSource, newDestination };
}
}
}
}
async function* updateIter(json = "update.json") {
const text = await readFile(json, "utf-8");
const data = JSON.parse(text);
for (const { owner, repo, ref, paths } of data) {
for (const { source, destination } of paths) {
if (source.endsWith("/") && destination.endsWith("/")) {
await io.rmRF(destination);
for await (const { newSource, newDestination } of dirIter(
owner,
repo,
ref,
source,
destination
)) {
yield {
owner,
repo,
ref,
source: newSource,
destination: newDestination,
};
}
} else {
yield {
owner,
repo,
ref,
source,
destination,
};
}
}
}
}
for await (const { owner, repo, ref, source, destination } of updateIter("portable_config/update.json")) {
let body = null;
if (ref === "gists") {
body = await handleGist(owner, repo, source);
} else if (ref === "releases") {
body = await handleRelease(owner, repo, source);
} else {
body = await handleRepo(owner, repo, ref, source);
}
await mkdir(dirname(destination), { recursive: true });
await pipeline(Readable.fromWeb(body), createWriteStream(destination));
}
- name: Commit Changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Automated Update