feat(cli): add update command to check and update globally installed @git.zone packages
This commit is contained in:
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.`);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user