feat(cli): add update command to check and update globally installed @git.zone packages
This commit is contained in:
193
ts/mod_update/classes.packagemanager.ts
Normal file
193
ts/mod_update/classes.packagemanager.ts
Normal file
@@ -0,0 +1,193 @@
|
||||
import * as plugins from './mod.plugins.js';
|
||||
|
||||
export type TPackageManager = 'npm' | 'yarn' | 'pnpm';
|
||||
|
||||
export interface IInstalledPackage {
|
||||
name: string;
|
||||
version: string;
|
||||
packageManager: TPackageManager;
|
||||
}
|
||||
|
||||
export interface IPackageUpdateInfo {
|
||||
name: string;
|
||||
currentVersion: string;
|
||||
latestVersion: string;
|
||||
packageManager: TPackageManager;
|
||||
needsUpdate: boolean;
|
||||
}
|
||||
|
||||
export class PackageManagerUtil {
|
||||
private shell = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash',
|
||||
});
|
||||
|
||||
/**
|
||||
* Check if a package manager is available on the system
|
||||
*/
|
||||
public async isAvailable(pm: TPackageManager): Promise<boolean> {
|
||||
try {
|
||||
const result = await this.shell.execSilent(`which ${pm} >/dev/null 2>&1 && echo "found"`);
|
||||
return result.exitCode === 0 && result.stdout.includes('found');
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all globally installed @git.zone packages for a package manager
|
||||
*/
|
||||
public async getInstalledPackages(pm: TPackageManager): Promise<IInstalledPackage[]> {
|
||||
const packages: IInstalledPackage[] = [];
|
||||
|
||||
try {
|
||||
let result;
|
||||
switch (pm) {
|
||||
case 'npm':
|
||||
result = await this.shell.execSilent('npm list -g --depth=0 --json 2>/dev/null || true');
|
||||
break;
|
||||
case 'yarn':
|
||||
result = await this.shell.execSilent('yarn global list --depth=0 --json 2>/dev/null || true');
|
||||
break;
|
||||
case 'pnpm':
|
||||
result = await this.shell.execSilent('pnpm list -g --depth=0 --json 2>/dev/null || true');
|
||||
break;
|
||||
}
|
||||
|
||||
const output = result.stdout.trim();
|
||||
if (!output) {
|
||||
return packages;
|
||||
}
|
||||
|
||||
if (pm === 'npm') {
|
||||
try {
|
||||
const data = JSON.parse(output);
|
||||
const deps = data.dependencies || {};
|
||||
for (const [name, info] of Object.entries(deps)) {
|
||||
if (name.startsWith('@git.zone/')) {
|
||||
packages.push({
|
||||
name,
|
||||
version: (info as any).version || 'unknown',
|
||||
packageManager: pm,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// JSON parse failed
|
||||
}
|
||||
} else if (pm === 'pnpm') {
|
||||
// pnpm returns an array of objects
|
||||
try {
|
||||
const data = JSON.parse(output);
|
||||
// Handle array format from pnpm
|
||||
const dataArray = Array.isArray(data) ? data : [data];
|
||||
for (const item of dataArray) {
|
||||
const deps = item.dependencies || {};
|
||||
for (const [name, info] of Object.entries(deps)) {
|
||||
if (name.startsWith('@git.zone/')) {
|
||||
packages.push({
|
||||
name,
|
||||
version: (info as any).version || 'unknown',
|
||||
packageManager: pm,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// JSON parse failed
|
||||
}
|
||||
} else if (pm === 'yarn') {
|
||||
// Yarn global list --json outputs multiple JSON lines
|
||||
const lines = output.split('\n').filter(l => l.trim());
|
||||
for (const line of lines) {
|
||||
try {
|
||||
const data = JSON.parse(line);
|
||||
if (data.type === 'tree' && data.data && data.data.trees) {
|
||||
for (const tree of data.data.trees) {
|
||||
const name = tree.name?.split('@')[0] || '';
|
||||
if (name.startsWith('@git.zone/')) {
|
||||
const version = tree.name?.split('@').pop() || 'unknown';
|
||||
packages.push({
|
||||
name,
|
||||
version,
|
||||
packageManager: pm,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Skip invalid JSON lines
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Command failed, return empty array
|
||||
}
|
||||
|
||||
return packages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latest version of a package from npm registry
|
||||
*/
|
||||
public async getLatestVersion(packageName: string): Promise<string | null> {
|
||||
try {
|
||||
const result = await this.shell.execSilent(`npm view ${packageName} version 2>/dev/null`);
|
||||
if (result.exitCode === 0 && result.stdout.trim()) {
|
||||
return result.stdout.trim();
|
||||
}
|
||||
} catch {
|
||||
// Command failed
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute an update for a package using the specified package manager
|
||||
*/
|
||||
public async executeUpdate(pm: TPackageManager, packageName: string): Promise<boolean> {
|
||||
let command: string;
|
||||
|
||||
switch (pm) {
|
||||
case 'npm':
|
||||
command = `npm install -g ${packageName}@latest`;
|
||||
break;
|
||||
case 'yarn':
|
||||
command = `yarn global add ${packageName}@latest`;
|
||||
break;
|
||||
case 'pnpm':
|
||||
command = `pnpm add -g ${packageName}@latest`;
|
||||
break;
|
||||
}
|
||||
|
||||
console.log(` Updating ${packageName} via ${pm}...`);
|
||||
|
||||
try {
|
||||
const result = await this.shell.exec(command);
|
||||
return result.exitCode === 0;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two semver versions
|
||||
* Returns true if latest > current
|
||||
*/
|
||||
public isNewerVersion(current: string, latest: string): boolean {
|
||||
const cleanVersion = (v: string) => v.replace(/^[^\d]*/, '');
|
||||
const currentClean = cleanVersion(current);
|
||||
const latestClean = cleanVersion(latest);
|
||||
|
||||
const currentParts = currentClean.split('.').map(n => parseInt(n, 10) || 0);
|
||||
const latestParts = latestClean.split('.').map(n => parseInt(n, 10) || 0);
|
||||
|
||||
for (let i = 0; i < Math.max(currentParts.length, latestParts.length); i++) {
|
||||
const curr = currentParts[i] || 0;
|
||||
const lat = latestParts[i] || 0;
|
||||
if (lat > curr) return true;
|
||||
if (lat < curr) return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
135
ts/mod_update/index.ts
Normal file
135
ts/mod_update/index.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import * as plugins from './mod.plugins.js';
|
||||
import { PackageManagerUtil, type TPackageManager, type IPackageUpdateInfo } from './classes.packagemanager.js';
|
||||
|
||||
const GITZONE_PACKAGES = [
|
||||
'@git.zone/cli',
|
||||
'@git.zone/tsdoc',
|
||||
'@git.zone/tsbuild',
|
||||
'@git.zone/tstest',
|
||||
'@git.zone/tspublish',
|
||||
'@git.zone/tsbundle',
|
||||
'@git.zone/tsdocker',
|
||||
'@git.zone/tsview',
|
||||
'@git.zone/tswatch',
|
||||
];
|
||||
|
||||
export interface IUpdateOptions {
|
||||
yes?: boolean;
|
||||
}
|
||||
|
||||
export const run = async (options: IUpdateOptions = {}): Promise<void> => {
|
||||
const pmUtil = new PackageManagerUtil();
|
||||
|
||||
console.log('Scanning for installed @git.zone packages...\n');
|
||||
|
||||
// Check which package managers are available
|
||||
const availablePMs: TPackageManager[] = [];
|
||||
for (const pm of ['npm', 'yarn', 'pnpm'] as TPackageManager[]) {
|
||||
if (await pmUtil.isAvailable(pm)) {
|
||||
availablePMs.push(pm);
|
||||
}
|
||||
}
|
||||
|
||||
if (availablePMs.length === 0) {
|
||||
console.log('No package managers found (npm, yarn, pnpm).');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Found package managers: ${availablePMs.join(', ')}\n`);
|
||||
|
||||
// Collect all installed @git.zone packages from all package managers
|
||||
const allPackages: IPackageUpdateInfo[] = [];
|
||||
|
||||
for (const pm of availablePMs) {
|
||||
const installed = await pmUtil.getInstalledPackages(pm);
|
||||
for (const pkg of installed) {
|
||||
// Only include packages from our predefined list
|
||||
if (GITZONE_PACKAGES.includes(pkg.name)) {
|
||||
const latestVersion = await pmUtil.getLatestVersion(pkg.name);
|
||||
if (latestVersion) {
|
||||
allPackages.push({
|
||||
name: pkg.name,
|
||||
currentVersion: pkg.version,
|
||||
latestVersion,
|
||||
packageManager: pm,
|
||||
needsUpdate: pmUtil.isNewerVersion(pkg.version, latestVersion),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (allPackages.length === 0) {
|
||||
console.log('No @git.zone packages found installed globally.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Display package table
|
||||
console.log('Installed @git.zone packages:\n');
|
||||
console.log(' Package Current Latest PM Status');
|
||||
console.log(' ─────────────────────────────────────────────────────────────────────');
|
||||
|
||||
for (const pkg of allPackages) {
|
||||
const name = pkg.name.padEnd(28);
|
||||
const current = pkg.currentVersion.padEnd(12);
|
||||
const latest = pkg.latestVersion.padEnd(12);
|
||||
const pm = pkg.packageManager.padEnd(8);
|
||||
const status = pkg.needsUpdate ? '⬆️ Update available' : '✓ Up to date';
|
||||
console.log(` ${name}${current}${latest}${pm}${status}`);
|
||||
}
|
||||
|
||||
console.log('');
|
||||
|
||||
// Filter packages that need updates
|
||||
const packagesToUpdate = allPackages.filter(p => p.needsUpdate);
|
||||
|
||||
if (packagesToUpdate.length === 0) {
|
||||
console.log('All packages are up to date!');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`Found ${packagesToUpdate.length} package(s) with available updates.\n`);
|
||||
|
||||
// Ask for confirmation unless -y flag is provided
|
||||
let shouldUpdate = options.yes === true;
|
||||
|
||||
if (!shouldUpdate) {
|
||||
const smartinteractInstance = new plugins.smartinteract.SmartInteract();
|
||||
const answer = await smartinteractInstance.askQuestion({
|
||||
type: 'confirm',
|
||||
name: 'confirmUpdate',
|
||||
message: 'Do you want to update these packages?',
|
||||
default: true,
|
||||
});
|
||||
shouldUpdate = answer.value === true;
|
||||
}
|
||||
|
||||
if (!shouldUpdate) {
|
||||
console.log('Update cancelled.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Execute updates
|
||||
console.log('\nUpdating packages...\n');
|
||||
|
||||
let successCount = 0;
|
||||
let failCount = 0;
|
||||
|
||||
for (const pkg of packagesToUpdate) {
|
||||
const success = await pmUtil.executeUpdate(pkg.packageManager, pkg.name);
|
||||
if (success) {
|
||||
console.log(` ✓ ${pkg.name} updated successfully`);
|
||||
successCount++;
|
||||
} else {
|
||||
console.log(` ✗ ${pkg.name} update failed`);
|
||||
failCount++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log('');
|
||||
if (failCount === 0) {
|
||||
console.log(`All ${successCount} package(s) updated successfully!`);
|
||||
} else {
|
||||
console.log(`Updated ${successCount} package(s), ${failCount} failed.`);
|
||||
}
|
||||
};
|
||||
4
ts/mod_update/mod.plugins.ts
Normal file
4
ts/mod_update/mod.plugins.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import * as smartinteract from '@push.rocks/smartinteract';
|
||||
import * as smartshell from '@push.rocks/smartshell';
|
||||
|
||||
export { smartinteract, smartshell };
|
||||
Reference in New Issue
Block a user