Skip to content

Update

Update #347

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 { readFile, mkdir } = require("fs/promises");
const path = require("path");
const { Readable } = require("stream");
const { finished } = require("stream/promises");
const updateListPath = "portable_config/update.json";
async function getUpdateList(path) {
const json = await readFile(path, "utf8");
const data = JSON.parse(json);
return data;
}
async function getFlattedList(list) {
const results = [];
for (const { repository, branch, files } of list) {
for (const { source, target } of files) {
results.push({ repository, branch, source, target });
}
}
return results;
}
async function getDownloadList(list) {
const results = [];
for (const { repository, branch, source, target } of list) {
const isDir = source.endsWith("/") && target.endsWith("/");
if (isDir) {
const tasks = await resolveDir(repository, branch, source, target);
results.push(...tasks);
} else {
const sourceUrl = await resolveSourceUrl(repository, branch, source);
const task = { source: sourceUrl, target };
results.push(task);
}
}
return results;
}
async function resolveDir(repo, branch, directory, outputDir) {
const url = `https://api.github.com/repos/${repo}/contents/${directory}`;
const response = await fetch(url, {
headers: {
Accept: "application/vnd.github.v3+json",
"User-Agent": "github-script-action",
},
});
const items = await response.json();
const downloadTasks = [];
for (const item of items) {
const outputPath = path.join(outputDir, item.path.replace(directory, ""));
if (item.type === "dir") {
const tasks = await resolveDir(repo, branch, item.path, outputPath);
downloadTasks.push(...tasks);
} else {
const task = { source: item.download_url, target: outputPath };
downloadTasks.push(task);
}
}
return downloadTasks;
}
async function resolveSourceUrl(repo, branch, source) {
if (branch === "@gist") {
return `https://gist.github.com/${repo}/raw/${source}`;
}
if (branch === "@releases") {
return `https://github.com/${repo}/releases/latest/download/${source}`;
}
return `https://github.com/${repo}/raw/${branch}/${source}`;
}
async function removeDir(updateList) {
for (const { source, target } of updateList) {
const isDir = source.endsWith("/") && target.endsWith("/");
if (isDir) {
await io.rmRF(target);
}
}
}
async function download(sourceUrl, destinationPath) {
const response = await fetch(sourceUrl, {
headers: {
Accept: "application/octet-stream",
"User-Agent": "github-script-action",
},
});
await mkdir(path.dirname(destinationPath), { recursive: true });
const writeStream = createWriteStream(destinationPath);
await finished(Readable.fromWeb(response.body).pipe(writeStream));
}
async function downloadList(list) {
for (const { source, target } of list) {
await download(source, target);
}
}
async function main() {
const update = await getUpdateList(updateListPath);
const flatted = await getFlattedList(update);
const download = await getDownloadList(flatted);
await removeDir(flatted);
await downloadList(download);
}
main();
- name: Commit Changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Automated Update