@reliverse/rse
Version:
@reliverse/rse is your all-in-one companion for bootstrapping and improving any kind of projects (especially web apps built with frameworks like Next.js) — whether you're kicking off something new or upgrading an existing app. It is also a little AI-power
83 lines (82 loc) • 2.86 kB
JavaScript
import path from "@reliverse/pathkit";
import fs from "@reliverse/relifso";
import { relinka } from "@reliverse/relinka";
import { simpleGit } from "simple-git";
export async function isDirHasGit(cwd, isDev, projectName, projectPath) {
const effectiveDir = isDev ? path.join(cwd, "tests-runtime", projectName) : projectPath;
try {
if (!await fs.pathExists(effectiveDir)) {
relinka("error", `Directory does not exist: ${effectiveDir}`);
return false;
}
const gitDir = path.join(effectiveDir, ".git");
if (!await fs.pathExists(gitDir)) {
return false;
}
const git = simpleGit({ baseDir: effectiveDir });
return await git.checkIsRepo();
} catch (error) {
if (!(error instanceof Error && error.message.includes("not a git repository"))) {
relinka(
"error",
"Error checking git repository:",
error instanceof Error ? error.message : String(error)
);
}
return false;
}
}
export async function setupGitRemote(cwd, isDev, projectName, projectPath, remoteUrl, remoteName = "origin") {
const effectiveDir = isDev ? path.join(cwd, "tests-runtime", projectName) : projectPath;
try {
if (!await fs.pathExists(effectiveDir)) {
relinka("error", `Directory does not exist: ${effectiveDir}`);
return false;
}
if (!await isDirHasGit(cwd, isDev, projectName, projectPath)) {
relinka(
"error",
"Not a git repository, git should be initialized before setupGitRemote. Something went wrong. Please notify developers."
);
return false;
}
const git = simpleGit({ baseDir: effectiveDir });
const remotes = await git.getRemotes();
if (!remotes.find((remote) => remote.name === remoteName)) {
await git.addRemote(remoteName, remoteUrl);
relinka("verbose", `Remote '${remoteName}' added successfully.`);
} else {
const remoteGetUrl = await git.remote(["get-url", remoteName]);
const existingUrl = remoteGetUrl ? remoteGetUrl.trim() : "";
if (existingUrl !== remoteUrl) {
await git.remote(["set-url", remoteName, remoteUrl]);
relinka("info", `Updated ${remoteName} remote URL to ${remoteUrl}`);
} else {
relinka(
"info",
`Remote '${remoteName}' already exists with correct URL.`
);
}
}
await git.push(remoteName, "main", ["--set-upstream"]);
relinka(
"success",
"Initial commit pushed to remote repository:",
remoteUrl
);
return true;
} catch (error) {
relinka(
"error",
`Failed to setup git remote: ${error instanceof Error ? error.message : String(error)}`
);
relinka(
"info",
`You can setup the remote manually:
cd ${effectiveDir}
git remote add ${remoteName} ${remoteUrl}
git push -u ${remoteName} main`
);
return false;
}
}