From 0cfb9e5bb3bea2d71ed474695054bae17dd681b7 Mon Sep 17 00:00:00 2001 From: Sameer Srivastava Date: Tue, 18 Oct 2022 20:14:25 +0530 Subject: [PATCH] Exclude packages (#13) * chore: exclude-packages added * chore: action yaml updated * chore: parse comma seperated list * chore: logging added * chore: filter dependencies * chore: test added * chore: readme update * chore: build updated * chore: build update * chore: readme update * chore: tests updated * chore: fix tests --- README.md | 4 +++- action.yml | 4 ++++ dist/index.js | 28 ++++++++++++++++++++++------ src/action.js | 9 ++++++++- src/licenses.js | 19 ++++++++++++++----- test/licenses.test.js | 41 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 92 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 774d6ba..fc8b3b6 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ If you're interested in knowing more about OSS licensing and why it is important | `licenses-file` | No | | Licenses output JSON file | | `include-dev` | No | `'false'` | Include development packages | | `include-transitive` | No | `'true'` | Include transitive packages | +| `exclude-packages` | No | `''` | Exclude packages | ## Outputs @@ -29,7 +30,7 @@ This action reads the `package.json` from the provided path to determine the dep ### Generating a license file and commiting the changes -This example runs on every push to the `master` branch and generates a license file located in `src/licenses.json` that gets commited and pushed if there are changes. +This example runs on every push to the `master` branch and generates a license file located in `src/licenses.json` that gets commited and pushed if there are changes. Packages to be excluded are specified using the `exclude-packages` input. ```yaml name: Update licenses file @@ -57,6 +58,7 @@ jobs: uses: nearform/github-action-licenses-export@v1 with: licenses-file: src/licenses.json + exclude-packages: 'lodash' - name: Commit changes uses: EndBug/add-and-commit@v9 with: diff --git a/action.yml b/action.yml index 665cc3c..6a181a9 100644 --- a/action.yml +++ b/action.yml @@ -20,6 +20,10 @@ inputs: description: 'Include transitive packages' required: false default: 'true' + exclude-packages: + description: 'A comma-separated list of packages to ignore' + required: false + default: '' outputs: licenses: diff --git a/dist/index.js b/dist/index.js index 92eff2f..c22b5ee 100644 --- a/dist/index.js +++ b/dist/index.js @@ -9355,8 +9355,15 @@ function getLicenseText(packagePath) { return undefined } -function getDependenciesLicenseInfo(packagePath, dependencies) { - return dependencies.map(dependency => { +function getDependenciesLicenseInfo( + packagePath, + dependencies, + excludePackages +) { + const filteredDependencies = dependencies.filter( + d => !excludePackages.includes(d) + ) + return filteredDependencies.map(dependency => { const dependencyPath = external_node_path_namespaceObject.join(packagePath, 'node_modules', dependency) const packageInfo = parsePackageInfo(dependencyPath) @@ -9390,7 +9397,7 @@ function getLicenses(options) { } const settings = { ...defaultSettings, ...options } - const { path, includeDev, includeTransitive } = settings + const { path, includeDev, includeTransitive, excludePackages = [] } = settings const licenses = [] @@ -9402,9 +9409,11 @@ function getLicenses(options) { }) const packageInfo = parsePackageInfo(subPath) - const dependencies = crawler.listDependencies(packageInfo.name) - licenses.push(...getDependenciesLicenseInfo(subPath, dependencies)) + const dependencies = crawler.listDependencies(packageInfo.name) + licenses.push( + ...getDependenciesLicenseInfo(subPath, dependencies, excludePackages) + ) } return buildUniqueLicenses(licenses) @@ -9421,11 +9430,13 @@ async function run() { const includeDev = core.getBooleanInput('include-dev') const includeTransitive = core.getBooleanInput('include-transitive') const licensesFile = core.getInput('licenses-file') + const excludePackages = parseCSV(core.getInput('exclude-packages')) const licenses = await getLicenses({ path, includeDev, - includeTransitive + includeTransitive, + excludePackages }) if (licensesFile) { @@ -9435,6 +9446,11 @@ async function run() { core.setOutput('licenses', licenses) } +function parseCSV(value) { + if (!value || value.trim() === '') return [] + return value.split(',').map(p => p.trim()) +} + ;// CONCATENATED MODULE: ./src/index.js diff --git a/src/action.js b/src/action.js index a00fbaa..da9d54e 100644 --- a/src/action.js +++ b/src/action.js @@ -8,11 +8,13 @@ export async function run() { const includeDev = core.getBooleanInput('include-dev') const includeTransitive = core.getBooleanInput('include-transitive') const licensesFile = core.getInput('licenses-file') + const excludePackages = parseCSV(core.getInput('exclude-packages')) const licenses = await getLicenses({ path, includeDev, - includeTransitive + includeTransitive, + excludePackages }) if (licensesFile) { @@ -21,3 +23,8 @@ export async function run() { core.setOutput('licenses', licenses) } + +function parseCSV(value) { + if (!value || value.trim() === '') return [] + return value.split(',').map(p => p.trim()) +} diff --git a/src/licenses.js b/src/licenses.js index f85ddfd..2b08ce1 100644 --- a/src/licenses.js +++ b/src/licenses.js @@ -29,8 +29,15 @@ function getLicenseText(packagePath) { return undefined } -function getDependenciesLicenseInfo(packagePath, dependencies) { - return dependencies.map(dependency => { +function getDependenciesLicenseInfo( + packagePath, + dependencies, + excludePackages +) { + const filteredDependencies = dependencies.filter( + d => !excludePackages.includes(d) + ) + return filteredDependencies.map(dependency => { const dependencyPath = path.join(packagePath, 'node_modules', dependency) const packageInfo = parsePackageInfo(dependencyPath) @@ -64,7 +71,7 @@ export default function getLicenses(options) { } const settings = { ...defaultSettings, ...options } - const { path, includeDev, includeTransitive } = settings + const { path, includeDev, includeTransitive, excludePackages = [] } = settings const licenses = [] @@ -76,9 +83,11 @@ export default function getLicenses(options) { }) const packageInfo = parsePackageInfo(subPath) - const dependencies = crawler.listDependencies(packageInfo.name) - licenses.push(...getDependenciesLicenseInfo(subPath, dependencies)) + const dependencies = crawler.listDependencies(packageInfo.name) + licenses.push( + ...getDependenciesLicenseInfo(subPath, dependencies, excludePackages) + ) } return buildUniqueLicenses(licenses) diff --git a/test/licenses.test.js b/test/licenses.test.js index 6e0eb87..bff3efb 100644 --- a/test/licenses.test.js +++ b/test/licenses.test.js @@ -217,4 +217,45 @@ t.test('getLicenses', async t => { }) } ) + + t.test('Ignore packages in exclude-packages list', async t => { + const licenses = getLicenses({ + path: [ + path.join(testDirectory, 'first-package'), + path.join(testDirectory, 'second-package') + ], + excludePackages: ['second-package'] + }) + + t.strictSame(licenses, { + proddep: { + author: undefined, + name: 'proddep', + version: '18.2.0', + license: 'ISC', + licenseText: 'License content from proddep' + }, + transitivedep: { + author: undefined, + name: 'transitivedep', + version: '1.0.0', + license: 'BSD-3-Clause', + licenseText: 'The license content from transitivedep' + }, + deepnested: { + author: undefined, + name: 'deepnested', + version: '2.0.0', + license: 'BSD-3-Clause', + licenseText: 'The license content from deepnested' + }, + anotherdep: { + author: undefined, + name: 'anotherdep', + version: '1.0.1', + license: 'ISC', + licenseText: 'License content from anotherdep' + } + }) + }) })