@gordon1210/depup
Version:
a dependency upgrade tool for node projects
70 lines (69 loc) • 2.28 kB
JavaScript
import fs from "fs/promises";
import path from "path";
import semver from "semver";
import yaml from "yaml";
export function getDisplayVersion(pkg) {
if (pkg.targetVersionType === "patch") {
return pkg.patchVersion ? `~${pkg.patchVersion}` : undefined;
}
if (pkg.targetVersionType === "minor") {
return pkg.minorVersion ? `^${pkg.minorVersion}` : undefined;
}
if (pkg.targetVersionType === "latest") {
return !semver.prerelease(pkg.latestVersion)
? pkg.latestVersion
: undefined;
}
if (pkg.targetVersionType === "prerelease") {
return pkg.prereleaseVersion;
}
return undefined;
}
export async function detectWorkspaces(root) {
const pkgJsonPath = path.join(root, "package.json");
const workspaceYamlPath = path.join(root, "pnpm-workspace.yaml");
try {
const yamlExists = await fs
.stat(workspaceYamlPath)
.then(() => true)
.catch(() => false);
if (yamlExists) {
const raw = await fs.readFile(workspaceYamlPath, "utf8");
const parsed = yaml.parse(raw);
return parsed.packages || [];
}
else {
const content = JSON.parse(await fs.readFile(pkgJsonPath, "utf8"));
if (content.workspaces) {
if (Array.isArray(content.workspaces)) {
return content.workspaces;
}
if (content.workspaces.packages) {
return content.workspaces.packages;
}
}
}
return [];
}
catch {
return [];
}
}
export function progressBar(current, total, width = 20) {
const percent = total > 0 ? current / total : 0;
const filled = Math.round(percent * width);
return ("▌" +
"█".repeat(filled) +
"░".repeat(width - filled) +
`▐ ${current}/${total}`);
}
export function truncateText(text, maxLength, ellipsisPosition = 'end') {
if (text.length <= maxLength) {
return text;
}
if (ellipsisPosition === 'end') {
return text.slice(0, maxLength - 3) + '...';
}
// Add other truncation styles if needed in the future
return text.slice(0, maxLength - 3) + '...';
}