feat(cli,release): add self-upgrade command and automate CI, release, and npm publishing workflows
This commit is contained in:
79
ts/cli.ts
79
ts/cli.ts
@@ -72,6 +72,10 @@ export async function runCli(): Promise<void> {
|
||||
await handleStatusCommand(onebox);
|
||||
break;
|
||||
|
||||
case 'upgrade':
|
||||
await handleUpgradeCommand();
|
||||
break;
|
||||
|
||||
default:
|
||||
logger.error(`Unknown command: ${command}`);
|
||||
printHelp();
|
||||
@@ -386,6 +390,78 @@ async function handleStatusCommand(onebox: Onebox) {
|
||||
console.log(JSON.stringify(status, null, 2));
|
||||
}
|
||||
|
||||
// Upgrade command - self-update onebox to latest version
|
||||
async function handleUpgradeCommand(): Promise<void> {
|
||||
// Check if running as root
|
||||
if (Deno.uid() !== 0) {
|
||||
logger.error('This command must be run as root to upgrade Onebox.');
|
||||
logger.info('Try: sudo onebox upgrade');
|
||||
Deno.exit(1);
|
||||
}
|
||||
|
||||
logger.info('Checking for updates...');
|
||||
|
||||
try {
|
||||
// Get current version
|
||||
const currentVersion = projectInfo.version;
|
||||
|
||||
// Fetch latest version from Gitea API
|
||||
const apiUrl = 'https://code.foss.global/api/v1/repos/serve.zone/onebox/releases/latest';
|
||||
const curlCmd = new Deno.Command('curl', {
|
||||
args: ['-sSL', apiUrl],
|
||||
stdout: 'piped',
|
||||
stderr: 'piped',
|
||||
});
|
||||
const curlResult = await curlCmd.output();
|
||||
const response = new TextDecoder().decode(curlResult.stdout);
|
||||
const release = JSON.parse(response);
|
||||
const latestVersion = release.tag_name as string; // e.g., "v1.11.0"
|
||||
|
||||
// Normalize versions for comparison (ensure both have "v" prefix)
|
||||
const normalizedCurrent = currentVersion.startsWith('v')
|
||||
? currentVersion
|
||||
: `v${currentVersion}`;
|
||||
const normalizedLatest = latestVersion.startsWith('v')
|
||||
? latestVersion
|
||||
: `v${latestVersion}`;
|
||||
|
||||
console.log(` Current version: ${normalizedCurrent}`);
|
||||
console.log(` Latest version: ${normalizedLatest}`);
|
||||
console.log('');
|
||||
|
||||
// Compare normalized versions
|
||||
if (normalizedCurrent === normalizedLatest) {
|
||||
logger.success('Already up to date!');
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info(`New version available: ${latestVersion}`);
|
||||
logger.info('Downloading and installing...');
|
||||
console.log('');
|
||||
|
||||
// Download and run the install script
|
||||
const installUrl = 'https://code.foss.global/serve.zone/onebox/raw/branch/main/install.sh';
|
||||
const installCmd = new Deno.Command('bash', {
|
||||
args: ['-c', `curl -sSL ${installUrl} | bash`],
|
||||
stdin: 'inherit',
|
||||
stdout: 'inherit',
|
||||
stderr: 'inherit',
|
||||
});
|
||||
const installResult = await installCmd.output();
|
||||
|
||||
if (!installResult.success) {
|
||||
logger.error('Upgrade failed');
|
||||
Deno.exit(1);
|
||||
}
|
||||
|
||||
console.log('');
|
||||
logger.success(`Upgraded to ${latestVersion}`);
|
||||
} catch (error) {
|
||||
logger.error(`Upgrade failed: ${getErrorMessage(error)}`);
|
||||
Deno.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Helpers
|
||||
function getArg(args: string[], flag: string): string {
|
||||
const arg = args.find((a) => a.startsWith(`${flag}=`));
|
||||
@@ -441,6 +517,9 @@ Commands:
|
||||
|
||||
status
|
||||
|
||||
upgrade
|
||||
Upgrade Onebox to the latest version (requires root)
|
||||
|
||||
Options:
|
||||
--help, -h Show this help message
|
||||
--version, -v Show version
|
||||
|
||||
Reference in New Issue
Block a user