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']) { async function appendProjectDependencyPaths(rootPaths, matchPattern, modes = ['npm', 'bower']) {
let matchFn; let matchFn;
if (matchPattern) { 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 = []; const depProjectPaths = [];
await aForEach(rootPaths, async targetPath => { await aForEach(rootPaths, async targetPath => {

View file

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

View file

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