From 65a2794c1d9c316cc8c53c6fd08872dd94af584c Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 3 Nov 2016 11:46:34 +0000 Subject: [PATCH] Add name lookups to ls command - fixes #1599 (#1643) --- src/cli/commands/ls.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/cli/commands/ls.js b/src/cli/commands/ls.js index bb590bde35..f2174cce4a 100644 --- a/src/cli/commands/ls.js +++ b/src/cli/commands/ls.js @@ -5,13 +5,13 @@ import type Config from '../../config.js'; import type PackageResolver from '../../package-resolver.js'; import type PackageLinker from '../../package-linker.js'; import type {Trees} from '../../reporters/types.js'; +import {MessageError} from '../../errors.js'; import {Install} from './install.js'; import Lockfile from '../../lockfile/wrapper.js'; const invariant = require('invariant'); export const requireLockfile = true; -export const noArguments = true; function buildCount(trees: ?Trees): number { if (!trees || !trees.length) { @@ -136,6 +136,31 @@ export async function run( const [depRequests, patterns] = await install.fetchRequestFromCwd(); await install.resolver.init(depRequests, install.flags.flat); + let filteredPatterns: Array = []; + + if (args.length) { + const matchedArgs: Array = []; + + for (const pattern of patterns) { + const pkg = install.resolver.getStrictResolvedPattern(pattern); + + // ignore patterns if their package names have been specified in arguments + if (args.indexOf(pkg.name) >= 0) { + matchedArgs.push(pkg.name); + filteredPatterns.push(pattern); + } + } + + // throw an error if any package names were passed to filter that don't exist + for (const arg of args) { + if (matchedArgs.indexOf(arg) < 0) { + throw new MessageError(reporter.lang('unknownPackage', arg)); + } + } + } else { + filteredPatterns = patterns; + } + const {trees} = await buildTree(install.resolver, install.linker, patterns); reporter.tree('ls', trees); }