scripts/mkplugin.js
2024-09-29 10:45:07 -04:00

155 lines
4.5 KiB
JavaScript

import { input, select } from "@inquirer/prompts";
import yoctoSpinner from "yocto-spinner";
import fs from "fs";
import path from "path";
import spawn from "nano-spawn";
import clc from "cli-color";
import { config } from "./config.js";
process.on("uncaughtException", (err) => {
console.error("Oh well, since you killed me... goodbye");
process.exit(1);
});
const metadata = {
name: await input({
message: "Name"
}),
description: await input({
message: "Description"
})
};
const extension = await select({
message: "Use React?",
choices: [
{
name: "No",
value: "ts"
},
{
name: "Yes",
value: "tsx"
}
]
});
const nin0gitPublishStatus = await select({
message: "Publish?",
choices: [
{
name: "Yes (public)",
value: "public",
description: "Create a public repository on nin0git"
},
{
name: "Yes (private)",
value: "private",
description: "Create a private repository on nin0git"
},
{
name: "No",
value: "no",
description: "Don't create a repository"
}
]
});
const cwd = path.join(
process.env.HOME || process.env.USERPROFILE,
"Dev",
"Vencord",
"src",
"userplugins",
metadata.name.toLowerCase()
);
const gitSpinner = yoctoSpinner({ text: "Initialising Git repo..." }).start();
fs.mkdirSync(cwd, { recursive: true });
await spawn("git", ["init"], { cwd });
gitSpinner.success("Initialised Git repo");
const fileSpinner = yoctoSpinner({ text: "Creating plugin files..." }).start();
fs.writeFileSync(
path.join(cwd, `index.${extension}`),
`import { Devs } from "@utils/constants";
import definePlugin from "@utils/types";
export default definePlugin({
name: "${metadata.name}",
description: "${metadata.description}",
authors: [Devs.nin0dev],
});
`
);
fs.writeFileSync(
path.join(cwd, "README.md"),
fs
.readFileSync(
path.join(
process.env.HOME || process.env.USERPROFILE,
"scripts",
"mkplugin",
"README-template.md"
),
"utf-8"
)
.replace(/%NAME%/g, metadata.name)
.replace(/%DESCRIPTION%/g, metadata.description)
);
fs.writeFileSync(
path.join(cwd, "LICENSE"),
fs
.readFileSync(
path.join(
process.env.HOME || process.env.USERPROFILE,
"scripts",
"mkplugin",
"LICENSE-template"
),
"utf-8"
)
.replace(/%NAME%/g, metadata.name)
);
fileSpinner.text = "Committing files...";
await spawn("git", ["config", "--local", "commit.gpgsign", "false"], { cwd });
await spawn("git", ["add", "*"], { cwd });
await spawn("git", ["commit", "-m", "Initial commit"], { cwd });
await spawn("git", ["config", "--local", "commit.gpgsign", "true"], { cwd });
fileSpinner.success("Created plugin files and committed them");
const remoteURL = `https://git.nin0.dev/userplugins/${metadata.name.toLowerCase()}`;
if (nin0gitPublishStatus !== "no") {
const remoteSpinner = yoctoSpinner({ text: "Adding Git remote..." }).start();
await spawn("git", ["remote", "add", "origin", remoteURL], { cwd });
remoteSpinner.success("Added Git remote");
const createRepoSpinner = yoctoSpinner({ text: "Creating repository on nin0git..." }).start();
const response = await fetch("https://git.nin0.dev/api/v1/orgs/userplugins/repos", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${config.nin0gitToken}`
},
body: JSON.stringify({
name: metadata.name.toLowerCase(),
description: metadata.description,
private: nin0gitPublishStatus === "private"
})
});
if (!response.ok) {
createRepoSpinner.fail("Failed to create repository on nin0git");
console.error("Error:", await response.text());
process.exit(1);
}
createRepoSpinner.success("Created repository on nin0git");
const pushSpinner = yoctoSpinner({ text: "Pushing to repo..." }).start();
await spawn("git", ["push", "origin", "main"], { cwd });
pushSpinner.success("Pushed to repo, you can find it at " + remoteURL);
}
console.log(clc.greenBright.underline.bold("Successfully created plugin " + metadata.name + "!"));
setTimeout(async () => await spawn("code", ["."], { cwd }), 600);