<commit_before>import chalk from 'chalk';
import ms from 'ms';
import plural from 'pluralize';
import table from 'text-table';
import Now from '../../util';
import Client from '../../util/client.ts';
import getAliases from '../../util/alias/get-aliases';
import getScope from '../../util/get-scope.ts';
import stamp from '../../util/output/stamp.ts';
import strlen from '../../util/strlen.ts';
import wait from '../../util/output/wait';

export default async function ls(ctx, opts, args, output) {
  const { authConfig: { token }, config } = ctx;
  const { currentTeam } = config;
  const { apiUrl } = ctx;
  const { '--debug': debugEnabled } = opts;
  const client = new Client({
    apiUrl,
    token,
    currentTeam,
    debug: debugEnabled
  });
  let contextName = null;

  try {
    ({ contextName } = await getScope(client));
  } catch (err) {
    if (err.code === 'NOT_AUTHORIZED' || err.code === 'TEAM_DELETED') {
      output.error(err.message);
      return 1;
    }

    throw err;
  }

  // $FlowFixMe
  const now = new Now({ apiUrl, token, debug: debugEnabled, currentTeam });
  const lsStamp = stamp();
  let cancelWait;

  if (args.length > 1) {
    output.error(
      `Invalid number of arguments. Usage: ${chalk.cyan(
        '`now alias ls [alias]`'
      )}`
    );
    return 1;
  }

  if (!opts['--json']) {
    cancelWait = wait(
      args[0]
        ? `Fetching alias details for "${args[0]}" under ${chalk.bold(
            contextName
          )}`
        : `Fetching aliases under ${chalk.bold(contextName)}`
    );
  }

  const aliases = await getAliases(now);
  if (cancelWait) cancelWait();

  if (args[0]) {
    const alias = aliases.find(
      item => item.uid === args[0] || item.alias === args[0]
    );
    if (!alias) {
      output.error(`Could not match path alias for: ${args[0]}`);
      now.close();
      return 1;
    }

    if (opts['--json']) {
      output.print(JSON.stringify({ rules: alias.rules }, null, 2));
    } else {
      const rules = alias.rules || [];
      output.log(
        `${rules.length} path alias ${plural(
          'rule',
          rules.length
        )} found under ${chalk.bold(contextName)} ${lsStamp()}`
      );
      output.print(`${printPathAliasTable(rules)}\n`);
    }
  } else {
    aliases.sort((a, b) => new Date(b.created) - new Date(a.created));
    output.log(
      `${plural('alias', aliases.length, true)} found under ${chalk.bold(
        contextName
      )} ${lsStamp()}`
    );
    console.log(printAliasTable(aliases));
  }

  now.close();
  return 0;
}

function printAliasTable(aliases) {
  return `${table(
    [
      ['source', 'url', 'age'].map(h => chalk.gray(h)),
      ...aliases.map(a => [
        a.rules && a.rules.length
          ? chalk.cyan(`[${plural('rule', a.rules.length, true)}]`)
          : // for legacy reasons, we might have situations
            // where the deployment was deleted and the alias
            // not collected appropriately, and we need to handle it
            a.deployment && a.deployment.url
            ? a.deployment.url
            : chalk.gray('–'),
        a.alias,
        ms(Date.now() - new Date(a.created))
      ])
    ],
    {
      align: ['l', 'l', 'r'],
      hsep: ' '.repeat(4),
      stringLength: strlen
    }
  ).replace(/^/gm, '  ')}\n\n`;<commit_msg>[now-cli] Output `--json` to stdout for `now alias ls <url>` (#2922)<commit_after>import chalk from 'chalk';
import ms from 'ms';
import plural from 'pluralize';
import table from 'text-table';
import Now from '../../util';
import Client from '../../util/client.ts';
import getAliases from '../../util/alias/get-aliases';
import getScope from '../../util/get-scope.ts';
import stamp from '../../util/output/stamp.ts';
import strlen from '../../util/strlen.ts';
import wait from '../../util/output/wait';

export default async function ls(ctx, opts, args, output) {
  const {
    authConfig: { token },
    config
  } = ctx;
  const { currentTeam } = config;
  const { apiUrl } = ctx;
  const { '--debug': debugEnabled } = opts;
  const client = new Client({
    apiUrl,
    token,
    currentTeam,
    debug: debugEnabled
  });
  let contextName = null;

  try {
    ({ contextName } = await getScope(client));
  } catch (err) {
    if (err.code === 'NOT_AUTHORIZED' || err.code === 'TEAM_DELETED') {
      output.error(err.message);
      return 1;
    }

    throw err;
  }

  // $FlowFixMe
  const now = new Now({ apiUrl, token, debug: debugEnabled, currentTeam });
  const lsStamp = stamp();
  let cancelWait;

  if (args.length > 1) {
    output.error(
      `Invalid number of arguments. Usage: ${chalk.cyan(
        '`now alias ls [alias]`'
      )}`
    );
    return 1;
  }

  cancelWait = wait(
    args[0]
      ? `Fetching alias details for "${args[0]}" under ${chalk.bold(
          contextName
        )}`
      : `Fetching aliases under ${chalk.bold(contextName)}`
  );

  const aliases = await getAliases(now);
  if (cancelWait) cancelWait();

  if (args[0]) {
    const alias = aliases.find(
      item => item.uid === args[0] || item.alias === args[0]
    );
    if (!alias) {
      output.error(`Could not match path alias for: ${args[0]}`);
      now.close();
      return 1;
    }

    if (opts['--json']) {
      console.log(JSON.stringify({ rules: alias.rules }, null, 2));
    } else {
      const rules = alias.rules || [];
      output.log(
        `${rules.length} path alias ${plural(
          'rule',
          rules.length
        )} found under ${chalk.bold(contextName)} ${lsStamp()}`
      );
      output.print(`${printPathAliasTable(rules)}\n`);
    }
  } else {
    aliases.sort((a, b) => new Date(b.created) - new Date(a.created));
    output.log(
      `${plural('alias', aliases.length, true)} found under ${chalk.bold(
        contextName
      )} ${lsStamp()}`
    );
    console.log(printAliasTable(aliases));
  }

  now.close();
  return 0;
}

function printAliasTable(aliases) {
  return `${table(
    [
      ['source', 'url', 'age'].map(h => chalk.gray(h)),
      ...aliases.map(a => [
        a.rules && a.rules.length
          ? chalk.cyan(`[${plural('rule', a.rules.length, true)}]`)
          : // for legacy reasons, we might have situations
          // where the deployment was deleted and the alias
          // not collected appropriately, and we need to handle it
          a.deployment && a.deployment.url
          ? a.deployment.url
          : chalk.gray('–'),
        a.alias,
        ms(Date.now() - new Date(a.created))
      ])
    ],
    {
      align: ['l', 'l', 'r'],
      hsep: ' '.repeat(4),
      stringLength: strlen
    }
  ).replace(/^/gm, '  ')}\n\n`;
{'fraction_non_alphanumeric': 0.17742663656884877, 'fraction_numerical': 0.004514672686230248, 'mean_word_length': 2.3296593186372747, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4926976', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2160, 'n_words': 549}