@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
49 lines (48 loc) • 1.39 kB
JavaScript
import { normalize } from "@reliverse/pathkit";
import { ensuredir } from "@reliverse/relifso";
import fs from "@reliverse/relifso";
import { relinka } from "@reliverse/relinka";
import { cwd } from "node:process";
export const handleError = (error) => error instanceof Error ? error.message : "Unknown error";
export async function cd(dir) {
try {
await ensuredir(dir);
await fs.access(dir);
process.chdir(dir);
relinka("verbose", `Changed directory to: ${process.cwd()}`);
} catch (error) {
relinka("warn", `Directory does not exist: ${dir}`, handleError(error));
}
}
export function pwd() {
const cwd2 = getCurrentWorkingDirectory();
relinka("verbose", `Current working directory: ${cwd2}`);
}
export async function rm(target) {
try {
await fs.remove(target);
relinka("verbose", `Removed: ${target}`);
} catch (error) {
relinka("error", `Failed to remove: ${target}`, handleError(error));
}
}
export function getCurrentWorkingDirectory(useCache = true) {
let cachedCWD = null;
if (useCache && cachedCWD) {
return cachedCWD;
}
try {
const currentDirectory = normalize(cwd());
if (useCache) {
cachedCWD = currentDirectory;
}
return currentDirectory;
} catch (error) {
relinka(
"error",
"Error getting current working directory:",
handleError(error)
);
throw error;
}
}