dependenciesGenerator top-level property

FigGenerator dependenciesGenerator
final

Implementation

final FigGenerator dependenciesGenerator = FigGenerator(
  trigger: (String newToken, String oldToken) =>
      newToken == '-g' || newToken == '--global',
  script: (List<String> tokens) {
    if (!tokens.contains('-g') && !tokens.contains('--global')) {
      return ['npm', 'prefix'];
    } else {
      return ['bash', '-c', 'ls -1 `npm root -g`'];
    }
  },
  postProcess: (String out, [List<String>? tokens]) {
    if (tokens != null &&
        (tokens.contains('-g') || tokens.contains('--global'))) {
      return out.split('\n').map((name) {
        return FigSuggestion(
          name: name,
          icon: '📦',
          description: 'global dependency',
        );
      }).toList();
    }

    // Local dependencies logic
    // The script returned npm prefix, so out is the prefix path.
    // We need to read package.json.
    // Since postProcess in Dart spec usually processes the output of the script,
    // and we can't easily chain commands in 'script' return value if it's a list,
    // we might need a different approach for local dependencies if we want to follow TS exactly.
    // However, TS uses `custom` which can execute multiple commands.
    // Dart `script` is limited to one command.
    // So we can try to chain them in one shell command:
    // `cat $(npm prefix)/package.json`

    // BUT, the generator definition above uses 'npm prefix' for local.
    // Let's change the script to cat the file directly using subshell.
    return [];
  },
);