diff --git a/ops/gitea_push.js b/ops/gitea_push.js new file mode 100644 index 0000000..565a08e --- /dev/null +++ b/ops/gitea_push.js @@ -0,0 +1,27 @@ +// 批量推送文件到 Gitea: node gitea_push.js : ... +const FS = require("fs"); +const B = "http://localhost:3001/api/v1/repos/admin/mini-erp-fabric-demo/contents/"; +const H = { Authorization: "token 77c6bd2fdab535b2720821d36b4cfb74aa3bf87c", "Content-Type": "application/json" }; + +async function push(lp, rp) { + const content = FS.readFileSync(lp).toString("base64"); + const g = await fetch(B + rp, { headers: H }); + let sha = ""; + if (g.status === 200) sha = (await g.json()).sha || ""; + const body = { content, branch: "main", message: "chore: sync " + rp }; + if (sha) body.sha = sha; + const r = await fetch(B + rp, { method: sha ? "PUT" : "POST", headers: H, body: JSON.stringify(body) }); + const ok = r.status === 200 || r.status === 201; + console.log((ok ? "PUSH_OK " : "PUSH_ERR ") + rp + " " + r.status + (ok ? "" : " " + (await r.text()).slice(0, 120))); + return ok; +} + +(async () => { + let fail = 0; + for (const arg of process.argv.slice(2)) { + const i = arg.indexOf(":"); + const lp = arg.slice(0, i), rp = arg.slice(i + 1); + if (!(await push(lp, rp))) fail++; + } + process.exit(fail ? 1 : 0); +})(); \ No newline at end of file