1.1.1Updated a month ago
const TOKEN = 'test';
import { HOST_URL } from "../../config.ts";
import { titleize } from "../../../utils/titleize.ts";
import type { I_CommandCode } from "../command_runner.ts";
import { Option } from "../options.ts";
import { Command } from "./enum.ts";

import dayjs from 'npm:dayjs'; 

export const status_command = {
  command: Command.status,
  aliases: ['get'],
  description: 'Used to get and  the status of this package on Viapak',
  more_info: [`Usage: viapak status <PackageName> [deprecate|exalt] [--full]`],
  help_text: [],
  optional_options: [Option.reason, Option.full],
  execute: async ({ args, options }) => {
    if(!args[0]) {
      return "Test";
    }

    const [name, _] = args[0].split('@');

    const package_status = await (await fetch(new URL(`/api/v1/packages/${name}`, HOST_URL), {
      headers: new Headers({
        'Authorization': `Bearer ${TOKEN}`,
      })
    })).json();

    if(!options.full) {
      const date = package_status.events?.reverse?.()?.[0]?.timestamp;
      if(date) package_status.date_modified = date;

      delete package_status.events;
    }

    console.log("");
    console.log(`Info about package '${name}':`);
    console.log("");
    for(let [key, value] of Object.entries(package_status)) {
      if(key === 'events') continue;

      if(key == 'versions') {
        if(!options.full) continue;

        if(Array.isArray(value) && value.length) {
          value = `[${value.join(', ')}]`;
        } else {
          value = '[]'
        }
      }

      if(key == 'date_modified') value = dayjs(value as string).format('YYYY-MM-DD HH:mm:ss');

      console.log(`  ${titleize(key).padEnd(18, ' ')} ${value}`)
    };

    if(Array.isArray(package_status.events)) {
      console.log("");
      console.log(`Events:`);

      if(!package_status.events.length) {
        console.log("  no recorded events");
      } else {
        const longest_event = Math.max(...package_status.events.map((e: { event: string }) => e.event.length));
  
        for(const event of package_status.events) {
          console.log(`  ${event.event.padEnd(longest_event + 3, ' ')} ${dayjs(event.timestamp).format('YYYY-MM-DD HH:mm:ss')}`)
        }
      }
    }
    console.log("");
  }
} as I_CommandCode;