fix(providence-analytics): simplified target-dependencies api

This commit is contained in:
Thijs Louisse 2020-08-04 18:02:41 +02:00
parent 8e86e0f730
commit cc251ed46c
3 changed files with 24 additions and 16 deletions

View file

@ -113,7 +113,13 @@ function targetDefault() {
async function appendProjectDependencyPaths(rootPaths, matchPattern, modes = ['npm', 'bower']) {
let matchFn;
if (matchPattern) {
matchFn = (_, d) => new RegExp(matchPattern).test(d);
if (matchPattern.startsWith('/') && matchPattern.endsWith('/')) {
matchFn = (_, d) => new RegExp(matchPattern.slice(1, -1)).test(d);
} else {
LogService.error(
`[appendProjectDependencyPaths] Please provide a matchPattern enclosed by '/'. Found: ${matchPattern}`,
);
}
}
const depProjectPaths = [];
await aForEach(rootPaths, async targetPath => {

View file

@ -99,10 +99,10 @@ async function cli({ cwd } = {}) {
* @type {string[]}
*/
let totalSearchTargets;
if (commander.includeTargetDeps) {
if (commander.targetDependencies !== undefined) {
totalSearchTargets = await cliHelpers.appendProjectDependencyPaths(
searchTargetPaths,
commander.targetDepsFilter,
commander.targetDependencies,
);
} else {
totalSearchTargets = searchTargetPaths;
@ -195,14 +195,12 @@ async function cli({ cwd } = {}) {
)
.option('--write-log-file', `Writes all logs to 'providence.log' file`)
.option(
'--include-target-deps',
'--target-dependencies [target-dependencies]',
`For all search targets, will include all its dependencies
(node_modules and bower_components). When --target-deps-filter is applied, a subset
will be applied that matches the filter condition`,
)
.option(
'--target-deps-filter [target-deps-filter]',
`Regex condition to be applied to dependencies of search targets.`,
(node_modules and bower_components). When --target-dependencies is applied
without argument, it will act as boolean and include all dependencies.
When a regex is supplied like --target-dependencies /^my-brand-/, it will filter
all packages that comply with the regex`,
);
commander

View file

@ -1,6 +1,7 @@
const sinon = require('sinon');
const pathLib = require('path');
const { expect } = require('chai');
const commander = require('commander');
const {
mockProject,
restoreMockedProjects,
@ -27,6 +28,9 @@ const {
appendProjectDependencyPaths,
} = cliHelpersModule;
// Prevent (node:2860) MaxListenersExceededWarning
commander.setMaxListeners(100);
const queryResults = [];
const rootDir = pathLib.resolve(__dirname, '../../');
@ -260,14 +264,14 @@ describe('Providence CLI', () => {
expect(providenceStub.args[0][1].writeLogFile).to.equal(true);
});
it('--include-target-deps"', async () => {
it('--target-dependencies"', async () => {
await runCli(`${analyzeCmd}`, rootDir);
expect(appendProjectDependencyPathsStub.called).to.be.false;
appendProjectDependencyPathsStub.resetHistory();
providenceStub.resetHistory();
await runCli(`${analyzeCmd} --include-target-deps`, rootDir);
await runCli(`${analyzeCmd} --target-dependencies`, rootDir);
expect(appendProjectDependencyPathsStub.called).to.be.true;
expect(providenceStub.args[0][1].targetProjectPaths).to.eql([
'/mocked/path/example-project',
@ -276,9 +280,9 @@ describe('Providence CLI', () => {
]);
});
it('--target-deps-filter"', async () => {
await runCli(`${analyzeCmd} --include-target-deps --target-deps-filter ^mock-`, rootDir);
expect(appendProjectDependencyPathsStub.args[0][1]).to.equal('^mock-');
it('--target-dependencies /^with-regex/"', async () => {
await runCli(`${analyzeCmd} --target-dependencies /^mock-/`, rootDir);
expect(appendProjectDependencyPathsStub.args[0][1]).to.equal('/^mock-/');
});
});
@ -464,7 +468,7 @@ describe('CLI helpers', () => {
});
it('allows a regex filter', async () => {
const result = await appendProjectDependencyPaths(['/mocked/path/example-project'], 'b$');
const result = await appendProjectDependencyPaths(['/mocked/path/example-project'], '/b$/');
expect(result).to.eql([
'/mocked/path/example-project/bower_components/dependency-b',
'/mocked/path/example-project',