diff --git a/.changeset/thirty-scissors-hug.md b/.changeset/thirty-scissors-hug.md new file mode 100644 index 000000000..adefe266f --- /dev/null +++ b/.changeset/thirty-scissors-hug.md @@ -0,0 +1,5 @@ +--- +'providence-analytics': patch +--- + +providence-analytics: enhanced allowlistMode detection diff --git a/packages-node/providence-analytics/src/program/services/InputDataService.js b/packages-node/providence-analytics/src/program/services/InputDataService.js index 6851d71d0..8338816c9 100644 --- a/packages-node/providence-analytics/src/program/services/InputDataService.js +++ b/packages-node/providence-analytics/src/program/services/InputDataService.js @@ -423,6 +423,16 @@ class InputDataService { return { globPattern }; } + /** + * Gets allowlist mode that determines which files to analyze + * @param {PathFromSystemRoot} startPath - local filesystem path + * @returns {'git'|'npm'} + */ + static _determineAllowListMode(startPath) { + const isNodeModule = /^.*\/(node_modules\/@.*|node_modules)\/.*$/.test(startPath); + return isNodeModule ? 'npm' : 'git'; + } + /** * Gets an array of files for given extension * @param {PathFromSystemRoot} startPath - local filesystem path @@ -453,9 +463,7 @@ class InputDataService { let gitIgnorePaths = []; /** @type {string[]} */ let npmPackagePaths = []; - - const hasGitIgnore = getGitignoreFile(startPath); - const allowlistMode = cfg.allowlistMode || (hasGitIgnore ? 'git' : 'npm'); + const allowlistMode = cfg.allowlistMode || this._determineAllowListMode(startPath); if (allowlistMode === 'git') { gitIgnorePaths = getGitIgnorePaths(startPath); diff --git a/packages-node/providence-analytics/test-node/program/services/InputDataService.test.js b/packages-node/providence-analytics/test-node/program/services/InputDataService.test.js index 5f65f5bfd..968b694aa 100644 --- a/packages-node/providence-analytics/test-node/program/services/InputDataService.test.js +++ b/packages-node/providence-analytics/test-node/program/services/InputDataService.test.js @@ -349,9 +349,7 @@ build/ './package.json': JSON.stringify({ files: ['dist'], }), - '.gitignore': ` -/dist - `, + '.gitignore': '/dist', }); const globOutput = InputDataService.gatherFilesFromDir('/fictional/project'); expect(globOutput).to.eql([ @@ -372,6 +370,36 @@ build/ // This means allowlistMode is 'npm' '/fictional/project/dist/bundle.js', ]); + + mockProject( + { './dist/bundle.js': '', '.gitignore': '/dist' }, + { + projectName: 'detect-as-npm', + projectPath: '/inside/proj/with/node_modules/detect-as-npm', + }, + ); + const globOutput3 = InputDataService.gatherFilesFromDir( + '/inside/proj/with/node_modules/detect-as-npm', + ); + expect(globOutput3).to.eql([ + // This means allowlistMode is 'npm' (even though we found .gitignore) + '/inside/proj/with/node_modules/detect-as-npm/dist/bundle.js', + ]); + + mockProject( + { './dist/bundle.js': '', '.gitignore': '/dist' }, + { + projectName: '@scoped/detect-as-npm', + projectPath: '/inside/proj/with/node_modules/@scoped/detect-as-npm', + }, + ); + const globOutput4 = InputDataService.gatherFilesFromDir( + '/inside/proj/with/node_modules/@scoped/detect-as-npm', + ); + expect(globOutput4).to.eql([ + // This means allowlistMode is 'npm' (even though we found .gitignore) + '/inside/proj/with/node_modules/@scoped/detect-as-npm/dist/bundle.js', + ]); }); it('custom "allowlist" will take precedence over "allowlistMode"', async () => {