UNPKG

@tricoteuses/assemblee

Version:

Retrieve, clean up & handle French Assemblée nationale's open data

151 lines (150 loc) 2.92 kB
import e from "fs-extra"; import t from "node:path"; import { execSync as n } from "node:child_process"; //#region src/git.ts var r = 50 * 1024 * 1024; function i(r) { e.existsSync(t.join(r, ".git")) || (e.ensureDirSync(r), n("git init", { cwd: r, env: process.env, encoding: "utf-8", stdio: [ "ignore", "ignore", "pipe" ] })); } function a(e, t) { i(e), n("git add .", { cwd: e, env: process.env, encoding: "utf-8", stdio: [ "ignore", "ignore", "pipe" ], maxBuffer: r }); try { return n(`git commit -m "${t}" --quiet`, { cwd: e, env: process.env, encoding: "utf-8", stdio: [ "ignore", "pipe", "pipe" ] }), !0; } catch (e) { let t = e; if (t.stdout === null || !/nothing to commit|rien à valider/.test(String(t.stdout ?? ""))) throw console.error(t.output), e; return !1; } } function o(e, t, r) { let i = 0; if (a(e, t)) for (let t of r || []) try { n(`git push ${t} master`, { cwd: e, env: process.env, encoding: "utf-8", stdio: [ "ignore", "ignore", "pipe" ] }); } catch (e) { let t = e; console.error(t.output), i = t.status ?? 1; } else i = 10; return i; } function s(e) { return n("git reset --hard origin/master", { cwd: e, env: process.env, encoding: "utf-8", stdio: [ "ignore", "ignore", "pipe" ] }), n("git pull --rebase", { cwd: e, env: process.env, encoding: "utf-8", stdio: [ "ignore", "ignore", "pipe" ] }), !0; } function c(e, t, r) { e !== void 0 && n(`git clone ${e}/${t}.git`, { cwd: r, env: process.env, encoding: "utf-8", stdio: [ "ignore", "ignore", "pipe" ] }); } function l(e, t, i) { try { i && console.log(`git -C ${e} ${t}`); let a = n(`git ${t}`, { cwd: e, maxBuffer: r }).toString().trim(); return i && console.log(a), a; } catch (e) { let t = e; for (let e of ["stdout", "stderr"]) console.error(`${e}: ${String(t[e] ?? "")}`); throw e; } } function u(e, t, i) { try { i && console.log(`git -C ${e} ${t}`); let a = n(`git ${t}`, { cwd: e, stdio: [ "ignore", "pipe", "pipe" ], maxBuffer: r }).toString().trim(); return i && console.log(a), !0; } catch (e) { if (e.status != 0) return !1; throw e; } } function d(e, t, n = {}) { let { diffFilter: r } = n; try { let n = l(e, `diff --name-status --diff-filter=${r ?? "AMR"} ${t}`, !1), i = /* @__PURE__ */ new Map(); for (let e of n.split("\n")) { if (e.trim().length === 0) continue; let t = e.split(" "); if (t.length >= 2) { let e = t[0].charAt(0), n = t[1]; i.set(n, e); } } return i; } catch { return console.error(`Error getting changed files since commit ${t}`), /* @__PURE__ */ new Map(); } } //#endregion export { c as clone, a as commit, o as commitAndPush, d as getChangedFilesSinceCommit, i as initRepo, s as resetAndPull, l as run, u as test };