diff --git a/action.yml b/action.yml index 8d9b60dc..2ab9f5e3 100644 --- a/action.yml +++ b/action.yml @@ -36,6 +36,12 @@ inputs: description: 'Generate signed commits. This uses a different Github API which conflicts with custom author information.' required: false default: 'false' + packages-detection-strategy: + description: | + The detection strategy for the affected packages, since this action only relies on affected files diff. + Possible values: `diff-only` (default) and `recursive-discovery` + required: true + default: diff-only # Define your outputs here. outputs: diff --git a/src/__tests__/generatePossibleParentsPackages.test.ts b/src/__tests__/generatePossibleParentsPackages.test.ts new file mode 100644 index 00000000..04520b33 --- /dev/null +++ b/src/__tests__/generatePossibleParentsPackages.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, it } from '@jest/globals'; +import { generatePossiblesParentsPackages } from '../generatePossiblesParentsPackages.js'; + +describe('generatePossiblesParentsPackages', () => { + describe('paths with package.json', () => { + it('will return an 1 length array with the provided path', () => { + expect(generatePossiblesParentsPackages("package.json")).toEqual(['package.json']); + expect(generatePossiblesParentsPackages("example/package.json")).toEqual(['example/package.json']); + expect(generatePossiblesParentsPackages("example/foo/package.json")).toEqual(['example/foo/package.json']); + }); + }) + + describe('paths without package.json', () => { + it('will return an dynamic length array with the provided path', () => { + expect(generatePossiblesParentsPackages("dotnet.csproj")).toEqual(['package.json']); + expect(generatePossiblesParentsPackages("example/dotnet.csproj")).toEqual(['example/package.json', 'package.json']); + expect(generatePossiblesParentsPackages("example/foo/dotnet.csproj")).toEqual(['example/foo/package.json', 'example/package.json', 'package.json']); + }); + }) +}); diff --git a/src/generatePossiblesParentsPackages.ts b/src/generatePossiblesParentsPackages.ts new file mode 100644 index 00000000..3a68e8ce --- /dev/null +++ b/src/generatePossiblesParentsPackages.ts @@ -0,0 +1,20 @@ +import * as path from "path" + +export const generatePossiblesParentsPackages = function(patchFilename: string) { + const generator = function*() { + if(patchFilename === 'package.json' || patchFilename.endsWith('/package.json')) { + yield patchFilename; + return; + }; + + let cursor = patchFilename; + + do { + cursor = path.dirname(cursor); + const packageJson = path.join(cursor, "package.json"); + yield packageJson; + } while(path.dirname(cursor) !== cursor); + } + + return Array.from(generator()) +}