Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(filter-options): Allow command to continue if no packages are matched #2280

Merged
merged 3 commits into from Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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