From 80d3507591e0da0426384e8cacbfc554f4dc7297 Mon Sep 17 00:00:00 2001 From: Guy Date: Mon, 13 Jan 2025 00:05:59 +0200 Subject: [PATCH] feat: hidden directories scan (#405) Adding a command line flag to extend the file scan for hidden directories. --- README.md | 2 ++ __tests__/generate.spec.ts | 22 ++++++++++++++++++++++ action.yml | 8 ++++++++ src/bin/cli.ts | 1 + src/commands/generate.ts | 14 +++++++++++++- 5 files changed, 46 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1b833d2e..5dfb26f3 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,8 @@ You can configure `codeowners-generator` from several places: - **check** (`--check`): It will fail if the CODEOWNERS generated doesn't match the current (or missing) CODEOWNERS . Useful for validating that the CODEOWNERS file is not out of date during CI. +- **hidden-directories** (`--hidden-directories`): Also include searching in hidden (dot) directories. + For more details you can invoke: ```sh diff --git a/__tests__/generate.spec.ts b/__tests__/generate.spec.ts index 7fce09d0..219f5088 100644 --- a/__tests__/generate.spec.ts +++ b/__tests__/generate.spec.ts @@ -844,4 +844,26 @@ describe('Generate', () => { await generateCommand({}, { parent: {} }); expect(writeFile).toHaveBeenCalled(); }); + + it('should call sync with dot flag when hidden flag is set', async () => { + await generateCommand( + { + hiddenDirectories: true, + }, + { parent: {} } + ); + + expect(sync.mock.calls[0][1]?.dot).toBeTruthy; + }); + + it('should call sync without dot flag when hidden flag is not set', async () => { + await generateCommand( + { + hiddenDirectories: false, + }, + { parent: {} } + ); + + expect(sync.mock.calls[0][1]?.dot).toBeFalsy; + }); }); diff --git a/action.yml b/action.yml index df5f1ed2..9b7965a1 100644 --- a/action.yml +++ b/action.yml @@ -32,6 +32,10 @@ inputs: version: description: codeowners-generator version. It will default to the latest in npm otherwise' required: false + hidden-directories: + description: Also include searching in hidden (dot) directories. + required: false + default: 'false' runs: using: 'composite' @@ -70,6 +74,10 @@ runs: ARGS_INPUT+=("--includes ${{inputs.includes}}") fi + if [ "${{inputs.hidden-directories}}" ]; then + ARGS_INPUT+=("--hidden-directories") + fi + if [ ! -z "${{inputs.version}}" ]; then VERSION="${{inputs.version}}" fi diff --git a/src/bin/cli.ts b/src/bin/cli.ts index 13c8e0aa..4b5ac933 100644 --- a/src/bin/cli.ts +++ b/src/bin/cli.ts @@ -43,6 +43,7 @@ program '--check', 'It will fail if the CODEOWNERS generated does not match the current (or missing) CODEOWNERS. Useful for validating that the CODEOWNERS file is up to date date during CI' ) + .option('--hidden-directories', 'Includes hidden directories when searching for CODEOWNERS files', false) .action(generateCommand); program.parse(process.argv); diff --git a/src/commands/generate.ts b/src/commands/generate.ts index d4635489..d1d6b991 100644 --- a/src/commands/generate.ts +++ b/src/commands/generate.ts @@ -24,11 +24,18 @@ type GenerateInput = { useMaintainers?: boolean; useRootMaintainers?: boolean; includes?: string[]; + hiddenDirectories: boolean; }; const { basename, dirname } = path; -export const generate: Generate = async ({ rootDir, includes, useMaintainers = false, useRootMaintainers = false }) => { +export const generate: Generate = async ({ + rootDir, + includes, + useMaintainers = false, + useRootMaintainers = false, + hiddenDirectories = false, +}) => { debug('input:', rootDir, includes, useMaintainers, useRootMaintainers); const includePatterns = includes && includes.length ? includes : INCLUDES; @@ -48,6 +55,7 @@ export const generate: Generate = async ({ rootDir, includes, useMaintainers = f debug('provided globs:', globs); const matches = sync(globs, { + dot: hiddenDirectories, onlyFiles: true, }); @@ -98,6 +106,7 @@ export const generate: Generate = async ({ rootDir, includes, useMaintainers = f interface Options extends GlobalOptions { check?: boolean; + hiddenDirectories?: boolean; } export const command = async (options: Options, command: Command): Promise => { @@ -114,6 +123,7 @@ export const command = async (options: Options, command: Command): Promise const groupSourceComments = globalOptions.groupSourceComments || options.groupSourceComments; const preserveBlockPosition = globalOptions.preserveBlockPosition || options.preserveBlockPosition; const customRegenerationCommand = globalOptions.customRegenerationCommand || options.customRegenerationCommand; + const { hiddenDirectories } = options; debug('Options:', { ...globalOptions, @@ -123,6 +133,7 @@ export const command = async (options: Options, command: Command): Promise groupSourceComments, preserveBlockPosition, customRegenerationCommand, + hiddenDirectories, }); try { @@ -130,6 +141,7 @@ export const command = async (options: Options, command: Command): Promise rootDir: __dirname, useMaintainers, useRootMaintainers, + hiddenDirectories, ...globalOptions, });