]> git.r.bdr.sh - rbdr/blog/blobdiff - lib/remotes/git.js
Add rudimentary sync support
[rbdr/blog] / lib / remotes / git.js
diff --git a/lib/remotes/git.js b/lib/remotes/git.js
new file mode 100644 (file)
index 0000000..d8e0cfd
--- /dev/null
@@ -0,0 +1,30 @@
+const { exec } = require('child_process');
+const { debuglog, promisify } = require('util');
+
+const internals = {
+  // Promisified functions
+  exec: promisify(exec),
+
+  debuglog: debuglog('blog'),
+};
+
+module.exports = {
+  canHandle() {
+    // For the future: actually check if it's a valid git url
+    return true;
+  },
+
+  async syncUp(remote, blogDirectory) {
+    await internals.exec(`cd ${blogDirectory} && git init`);
+    await internals.exec(`cd ${blogDirectory} && git add .`);
+    await internals.exec(`cd ${blogDirectory} && git commit --allow-empty -m blog-sync-up-${Date.now()}`);
+    await internals.exec(`cd ${blogDirectory} && git push ${remote} main --force`);
+  },
+
+  async syncDown(remote, blogDirectory) {
+    await internals.exec(`cd ${blogDirectory} && git init`);
+    await internals.exec(`cd ${blogDirectory} && git checkout .`);
+    await internals.exec(`cd ${blogDirectory} && git clean . -f`);
+    await internals.exec(`cd ${blogDirectory} && git pull ${remote} main`);
+  }
+}