Skip to content

Commit

Permalink
Exclude packages (#13)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sameer-coder authored Oct 18, 2022
1 parent 18b9c15 commit 0cfb9e5
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
28 changes: 22 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -9390,7 +9397,7 @@ function getLicenses(options) {
}

const settings = { ...defaultSettings, ...options }
const { path, includeDev, includeTransitive } = settings
const { path, includeDev, includeTransitive, excludePackages = [] } = settings

const licenses = []

Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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


Expand Down
9 changes: 8 additions & 1 deletion src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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())
}
19 changes: 14 additions & 5 deletions src/licenses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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 = []

Expand All @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions test/licenses.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
})
})
})

0 comments on commit 0cfb9e5

Please sign in to comment.