tokensGenerators top-level property

FigGenerator tokensGenerators
final

Implementation

final tokensGenerators = FigGenerator(
  cache: FigCache(strategy: "stale-while-revalidate"),
  custom: (List<String> tokens, ExecuteCommandFunction? executeShellCommand,
      FigGeneratorContext? context) async {
    if (executeShellCommand == null) return [];
    final teamOptionIndex =
        tokens.indexWhere((value) => value.startsWith("--team"));
    if (teamOptionIndex == -1) return [];

    String teamName;
    if (tokens[teamOptionIndex].contains("=")) {
      teamName = tokens[teamOptionIndex].split("=")[1];
    } else {
      // Need to check bounds, but TS code assumes it exists
      if (teamOptionIndex + 1 < tokens.length) {
        teamName = tokens[teamOptionIndex + 1];
      } else {
        return [];
      }
    }

    final out = await executeShellCommand(ExecuteCommandInput(
      command: "fig",
      args: [
        "user",
        "tokens",
        "list",
        "--team",
        teamName,
        "--format",
        "json",
      ],
    ));

    final json = jsonDecode(out.stdout) as List;
    return json.map((token) {
      final namespace = token['namespace']['username'];
      final description = token['description'];
      return FigSuggestion(
        name: token['name'],
        description:
            "Team: $namespace.${description != null ? " " + description : ""}",
      );
    }).toList();
  },
);