import { Command } from '@oclif/core';
import { fetchLatestReleaseInfo } from '@mb-it-org/shared';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
async function fetchLatestRelease() {
    try {
        const data = await fetchLatestReleaseInfo();
        return {
            latest: data.version,
            releaseUrl: data.releaseUrl,
        };
    }
    catch {
        return { latest: null };
    }
}
export default class Version extends Command {
    static description = 'Show CLI version and check for updates';
    static examples = ['mbs version'];
    async run() {
        await this.parse(Version);
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const pkg = require('../../package.json');
        const current = pkg.version ?? 'unknown';
        const { latest, releaseUrl } = await fetchLatestRelease();
        if (latest === null) {
            this.log(JSON.stringify({
                ok: true,
                data: {
                    current,
                    latest: null,
                    updateAvailable: null,
                },
            }));
            return;
        }
        const updateAvailable = latest !== current;
        if (updateAvailable) {
            this.log(JSON.stringify({
                ok: true,
                data: {
                    current,
                    latest,
                    updateAvailable: true,
                    releaseUrl,
                    hint: `新版本可用: v${latest}，访问 releaseUrl 下载更新`,
                },
            }));
        }
        else {
            this.log(JSON.stringify({
                ok: true,
                data: {
                    current,
                    latest,
                    updateAvailable: false,
                },
            }));
        }
    }
}
//# sourceMappingURL=version.js.map