vite-plugin-dileep-git
v0.0.1
Published
Create a Git commit through Vite dev server
Downloads
5
Readme
vite-plugin-dileep-git
The purpose of vite-plugin-dileep-git
is to help dileep canvas wrapper and helper create a code and image snapshot at the same time when working on creative coding sketches. Having both snapshots makes it easy to archive and retrieve the work from a particular moment. It may also be used more generally by leveraging the server-client communication.
⚠️ This module is early in its development. Expect breaking changes to come.
Install
npm i -D vite-plugin-dileep-git
How it works
When the plugin is run, it first checks whether git
is available on the machine and then checks for whether the project directory is already a git repo or not. If it's not, it will run git init
to create a new one.
When dileep:git
message is sent to the plugin from a client, the plugin commits to the Git and sends back dileep:git-success
message to the client with the commit hash. dileep then uses this info to export an image with the hash. The plugin sends dileep:log
message when git commit is successful, and it sends dileep:warn
for errors.
How to use
If you're using dileep with
npm create dileep
, this plugin is already set up for you. No need to do anything below.
In your code:
// attach it to a keyboard listener or button, etc.
if (import.meta.hot) {
import.meta.hot.send("dileep:git", {
// you can include extra data
id: 1234,
});
}
if (import.meta.hot) {
import.meta.hot.on("dileep:git-success", (data) => {
// do something with the git commit hash
saveFile(`${data.hash}.png`);
// you get back extra data you sent earlier
console.log(data.id);
});
import.meta.hot.on("dileep:log", (data) => {
console.log(data.msg);
});
import.meta.hot.on("dileep:warn", (data) => {
console.warn(data.msg);
});
}
Minimum Example
If you want to use this plugin in your own setup, here is a bare minimum example:
In vite.config.js
:
import { defineConfig } from "vite";
import { dileepGit } from "vite-plugin-dileep-git";
export default defineConfig({
plugins: [dileepGit()],
// ..
});
In your module code:
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 200;
document.body.appendChild(canvas);
const ctx = canvas.getContext("2d");
ctx.fillStyle = `gray`;
ctx.fillRect(0, 0, 200, 200);
window.addEventListener("keydown", (ev) => {
if (ev.key === "k") {
if (import.meta.hot) {
import.meta.hot.send("dileep:git");
}
}
});
if (import.meta.hot) {
import.meta.hot.on("dileep:git-success", (data) =>
// do something with hash
console.log(`commit hash:${data.hash}`)
// if you have some function to save a canvas...
saveCanvas(canvas, { filename: `${data.hash}.png` })
);
import.meta.hot.on("dileep:log", (data) => console.log(data.msg));
import.meta.hot.on("dileep:warn", (data) => console.warn(data.msg));
}
export {};
Default Options
dileepGit({
// console logging in browser
log: true,
});
License
MIT