generateDocs top-level property

FigGenerator generateDocs
final

Implementation

final FigGenerator generateDocs = FigGenerator(
  scriptTimeout: 12000,
  trigger: ".",
  getQueryTerm: ".",
  // Using custom because script function is not directly supported in serialized spec,
  // but if this is runtime Dart, we can use custom to emulate the logic.
  // However, the original TS uses script returning a command.
  // We'll implement this using `custom` which gives us full control.
  custom: (tokens, executeCommand, context) async {
    if (executeCommand == null) return [];

    final allowedOptions = {"--private", "--builtin", "--unstable"};
    // Slice to the second last element: `--json` conflicts with `scope`
    // tokens includes the executable? Usually tokens are arguments.
    // TS script: (tokens) => string[]
    // tokens in TS Fig.Generator.script are the tokens of the CLI command so far.

    // Logic from TS:
    final commandTokens = tokens
        .sublist(0, tokens.length - 1)
        .where((token) =>
            !(token.startsWith("-") && !allowedOptions.contains(token)) &&
            !token.startsWith(r"$") &&
            !token.startsWith("("))
        .toList();

    commandTokens.add("--json");

    // Execute the command
    // We assume the first token is 'deno' or we need to construct the full command.
    // The TS script returns `command`. If it's `['deno', 'doc', ...]` it works.
    // But tokens usually start after the subcommand if it's a subcommand generator?
    // Wait, `generateDocs` is likely used on `deno doc <args>`.
    // The tokens passed to script usually include the full command line or relevant part.
    // If we assume `commandTokens` is the full command to run:

    // We need to run it.
    final output = await executeCommand(ExecuteCommandInput(
      command: commandTokens.first,
      args: commandTokens.sublist(1),
    ));

    if (output.status != 0) return [];

    final out = output.stdout;
    final docNodes =
        (jsonDecode(out) as List).map((e) => DocNode.fromJson(e)).toList();

    // The final segment is being typed, ignore it
    final finalToken = tokens.last;
    final path =
        finalToken.split(".").sublist(0, finalToken.split(".").length - 1);

    final nodes = findChildNodes(docNodes, path);
    final filtered =
        filterNodes(nodes, showPrivateNodes: tokens.contains("--private"));
    final unique = getUniqueNamed(filtered);

    return unique.map((node) => convertNodeToSuggestion(node)).toList();
  },
);