Skip to content

Commit

Permalink
feat(filter-options): Allow command to continue if no packages are ma…
Browse files Browse the repository at this point in the history
…tched (#2280)
  • Loading branch information
debugmaster authored and evocateur committed Oct 15, 2019
1 parent 5e60213 commit a706023
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
14 changes: 14 additions & 0 deletions core/filter-options/__tests__/get-filtered-packages.test.js
Expand Up @@ -90,6 +90,20 @@ test.each`
expect.hasAssertions();
});

test.each`
argv
${["--scope", "not-a-package", "--continue-if-no-match"]}
${["--ignore", "package-*", "--continue-if-no-match"]}
${["--scope", "package-@(1|2)", "--ignore", "package-{1,2}", "--continue-if-no-match"]}
`("no errors and no packages $argv", async ({ argv }) => {
const packageGraph = await buildGraph(cwd);
const execOpts = { cwd };
const options = parseOptions(...argv);

const result = await getFilteredPackages(packageGraph, execOpts, options);
expect(result).toHaveLength(0);
});

test("--since returns all packages if no tag is found", async () => {
const packageGraph = await buildGraph(cwd);
const execOpts = { cwd };
Expand Down
5 changes: 5 additions & 0 deletions core/filter-options/index.js
Expand Up @@ -49,6 +49,11 @@ function filterOptions(yargs) {
`,
type: "boolean",
},
"continue-if-no-match": {
describe: "Don't fail if no package is matched",
hidden: true,
type: "boolean",
},
};

return yargs.options(opts).group(Object.keys(opts), "Filter Options:");
Expand Down
8 changes: 7 additions & 1 deletion core/filter-options/lib/get-filtered-packages.js
Expand Up @@ -9,7 +9,13 @@ function getFilteredPackages(packageGraph, execOpts, options) {
let chain = Promise.resolve();

chain = chain.then(() =>
filterPackages(packageGraph.rawPackageList, options.scope, options.ignore, options.private)
filterPackages(
packageGraph.rawPackageList,
options.scope,
options.ignore,
options.private,
options.continueIfNoMatch
)
);

if (options.since !== undefined) {
Expand Down
7 changes: 4 additions & 3 deletions utils/filter-packages/filter-packages.js
Expand Up @@ -16,10 +16,11 @@ module.exports = filterPackages;
* @param {Array.<String>} include A list of globs to match the package name against
* @param {Array.<String>} exclude A list of globs to filter the package name against
* @param {Boolean} showPrivate When false, filter out private packages
* @param {Boolean} continueIfNoMatch When true, do not throw if no package is matched
* @return {Array.<Package>} The packages with a name matching the glob
* @throws when a given glob would produce an empty list of packages
* @throws when a given glob would produce an empty list of packages and `continueIfNoMatch` is not set.
*/
function filterPackages(packagesToFilter, include = [], exclude = [], showPrivate) {
function filterPackages(packagesToFilter, include = [], exclude = [], showPrivate, continueIfNoMatch) {
const filtered = new Set(packagesToFilter);
const patterns = [].concat(arrify(include), negate(exclude));

Expand Down Expand Up @@ -49,7 +50,7 @@ function filterPackages(packagesToFilter, include = [], exclude = [], showPrivat
}
}

if (!filtered.size) {
if (!filtered.size && !continueIfNoMatch) {
throw new ValidationError("EFILTER", util.format("No packages remain after filtering", patterns));
}
}
Expand Down

0 comments on commit a706023

Please sign in to comment.