@tricoteuses/assemblee
Version:
Retrieve, clean up & handle French Assemblée nationale's open data
162 lines (161 loc) • 2.9 kB
JavaScript
import e from "fs-extra";
import t from "node:path";
import { execFileSync as n } from "node:child_process";
//#region src/git.ts
var r = 50 * 1024 * 1024;
function i(e) {
return Array.isArray(e) ? e : e.trim().split(/\s+/).filter(Boolean);
}
function a(e, { cwd: t, encoding: r = "utf-8", maxBuffer: a, stdio: o }) {
return n("git", i(e), {
cwd: t,
env: process.env,
encoding: r,
maxBuffer: a,
stdio: o
});
}
function o(n) {
e.existsSync(t.join(n, ".git")) || (e.ensureDirSync(n), a(["init"], {
cwd: n,
stdio: [
"ignore",
"ignore",
"pipe"
]
}));
}
function s(e, t) {
o(e), a(["add", "."], {
cwd: e,
stdio: [
"ignore",
"ignore",
"pipe"
],
maxBuffer: r
});
try {
return a([
"commit",
"-m",
t,
"--quiet"
], {
cwd: e,
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 c(e, t, n) {
let r = 0;
if (s(e, t)) for (let t of n || []) try {
a([
"push",
t,
"master"
], {
cwd: e,
stdio: [
"ignore",
"ignore",
"pipe"
]
});
} catch (e) {
let t = e;
console.error(t.output), r = t.status ?? 1;
}
else r = 10;
return r;
}
function l(e) {
return a([
"reset",
"--hard",
"origin/master"
], {
cwd: e,
stdio: [
"ignore",
"ignore",
"pipe"
]
}), a(["pull", "--rebase"], {
cwd: e,
stdio: [
"ignore",
"ignore",
"pipe"
]
}), !0;
}
function u(e, t, n) {
e !== void 0 && a(["clone", `${e}/${t}.git`], {
cwd: n,
stdio: [
"ignore",
"ignore",
"pipe"
]
});
}
function d(e, t, n) {
try {
n && console.log(`git -C ${e} ${t}`);
let i = a(t, {
cwd: e,
maxBuffer: r
}).trim();
return n && console.log(i), i;
} catch (e) {
let t = e;
for (let e of ["stdout", "stderr"]) console.error(`${e}: ${String(t[e] ?? "")}`);
throw e;
}
}
function f(e, t, n) {
try {
n && console.log(`git -C ${e} ${t}`);
let i = a(t, {
cwd: e,
stdio: [
"ignore",
"pipe",
"pipe"
],
maxBuffer: r
}).trim();
return n && console.log(i), !0;
} catch (e) {
if (e.status != 0) return !1;
throw e;
}
}
function p(e, t, n = {}) {
let { diffFilter: r } = n;
try {
let n = d(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 { u as clone, s as commit, c as commitAndPush, p as getChangedFilesSinceCommit, o as initRepo, l as resetAndPull, d as run, f as test };