import * as path from 'node:path';
import { listInstalledPackageNames, resolvePackagesNodeModulesRoot } from '../utils/package-store.js';

export interface IListCommandOptions {
  readonly cwd?: string;
  readonly packagesDir?: string;
}

export interface IListCommandResult {
  readonly packages: string[];
}

export async function listCommand(options: IListCommandOptions = {}): Promise<IListCommandResult> {
  const cwd = path.resolve(options.cwd ?? process.cwd());
  const nodeModulesRoot = resolvePackagesNodeModulesRoot(cwd, options.packagesDir);
  return {
    packages: listInstalledPackageNames(nodeModulesRoot),
  };
}
