From 50f2977ccef572a63b5e6e443b12b9972a68491d Mon Sep 17 00:00:00 2001 From: Josh Gross Date: Thu, 15 Aug 2024 17:13:49 -0400 Subject: [PATCH] Add glob option to ignore hidden files (#1791) * Add glob option to ignore hidden files * Use the basename of the file/directory to check for `.` * Ensure the `excludeHiddenFiles` is properly copied * Allow the root directory to be matched * Fix description of `excludeHiddenFiles` * Document Windows hidden attribute limitation * Bump version * `lint` * Document 0.5.0 release * Lint again --- packages/glob/RELEASES.md | 3 +++ .../glob/__tests__/internal-globber.test.ts | 22 ++++++++++++++++++- packages/glob/package-lock.json | 2 +- packages/glob/package.json | 2 +- .../glob/src/internal-glob-options-helper.ts | 8 ++++++- packages/glob/src/internal-glob-options.ts | 9 ++++++++ packages/glob/src/internal-globber.ts | 5 +++++ 7 files changed, 47 insertions(+), 4 deletions(-) diff --git a/packages/glob/RELEASES.md b/packages/glob/RELEASES.md index 84aa06be..142cae8d 100644 --- a/packages/glob/RELEASES.md +++ b/packages/glob/RELEASES.md @@ -1,5 +1,8 @@ # @actions/glob Releases +### 0.5.0 +- Added `excludeHiddenFiles` option, which is disabled by default to preserve existing behavior [#1791: Add glob option to ignore hidden files](https://github.com/actions/toolkit/pull/1791) + ### 0.4.0 - Pass in the current workspace as a parameter to HashFiles [#1318](https://github.com/actions/toolkit/pull/1318) diff --git a/packages/glob/__tests__/internal-globber.test.ts b/packages/glob/__tests__/internal-globber.test.ts index 4ae670fe..4b9d22ad 100644 --- a/packages/glob/__tests__/internal-globber.test.ts +++ b/packages/glob/__tests__/internal-globber.test.ts @@ -708,7 +708,7 @@ describe('globber', () => { expect(itemPaths).toEqual([]) }) - it('returns hidden files', async () => { + it('returns hidden files by default', async () => { // Create the following layout: // // /.emptyFolder @@ -734,6 +734,26 @@ describe('globber', () => { ]) }) + it('ignores hidden files when excludeHiddenFiles is set', async () => { + // Create the following layout: + // + // /.emptyFolder + // /.file + // /.folder + // /.folder/file + const root = path.join(getTestTemp(), 'ignores-hidden-files') + await createHiddenDirectory(path.join(root, '.emptyFolder')) + await createHiddenDirectory(path.join(root, '.folder')) + await createHiddenFile(path.join(root, '.file'), 'test .file content') + await fs.writeFile( + path.join(root, '.folder', 'file'), + 'test .folder/file content' + ) + + const itemPaths = await glob(root, {excludeHiddenFiles: true}) + expect(itemPaths).toEqual([root]) + }) + it('returns normalized paths', async () => { // Create the following layout: // /hello/world.txt diff --git a/packages/glob/package-lock.json b/packages/glob/package-lock.json index 7b954259..17817543 100644 --- a/packages/glob/package-lock.json +++ b/packages/glob/package-lock.json @@ -1,6 +1,6 @@ { "name": "@actions/glob", - "version": "0.4.0", + "version": "0.5.0", "lockfileVersion": 3, "requires": true, "description": "Actions glob lib", diff --git a/packages/glob/package.json b/packages/glob/package.json index 62e6bd29..637f9c6a 100644 --- a/packages/glob/package.json +++ b/packages/glob/package.json @@ -1,6 +1,6 @@ { "name": "@actions/glob", - "version": "0.4.0", + "version": "0.5.0", "preview": true, "description": "Actions glob lib", "keywords": [ diff --git a/packages/glob/src/internal-glob-options-helper.ts b/packages/glob/src/internal-glob-options-helper.ts index c798b165..f1dd5fe9 100644 --- a/packages/glob/src/internal-glob-options-helper.ts +++ b/packages/glob/src/internal-glob-options-helper.ts @@ -9,7 +9,8 @@ export function getOptions(copy?: GlobOptions): GlobOptions { followSymbolicLinks: true, implicitDescendants: true, matchDirectories: true, - omitBrokenSymbolicLinks: true + omitBrokenSymbolicLinks: true, + excludeHiddenFiles: false } if (copy) { @@ -32,6 +33,11 @@ export function getOptions(copy?: GlobOptions): GlobOptions { result.omitBrokenSymbolicLinks = copy.omitBrokenSymbolicLinks core.debug(`omitBrokenSymbolicLinks '${result.omitBrokenSymbolicLinks}'`) } + + if (typeof copy.excludeHiddenFiles === 'boolean') { + result.excludeHiddenFiles = copy.excludeHiddenFiles + core.debug(`excludeHiddenFiles '${result.excludeHiddenFiles}'`) + } } return result diff --git a/packages/glob/src/internal-glob-options.ts b/packages/glob/src/internal-glob-options.ts index ac1e93f1..8b721550 100644 --- a/packages/glob/src/internal-glob-options.ts +++ b/packages/glob/src/internal-glob-options.ts @@ -36,4 +36,13 @@ export interface GlobOptions { * @default true */ omitBrokenSymbolicLinks?: boolean + + /** + * Indicates whether to exclude hidden files (files and directories starting with a `.`). + * This does not apply to Windows files and directories with the hidden attribute unless + * they are also prefixed with a `.`. + * + * @default false + */ + excludeHiddenFiles?: boolean } diff --git a/packages/glob/src/internal-globber.ts b/packages/glob/src/internal-globber.ts index 3978d625..7f56b9b5 100644 --- a/packages/glob/src/internal-globber.ts +++ b/packages/glob/src/internal-globber.ts @@ -128,6 +128,11 @@ export class DefaultGlobber implements Globber { continue } + // Hidden file or directory? + if (options.excludeHiddenFiles && path.basename(item.path).match(/^\./)) { + continue + } + // Directory if (stats.isDirectory()) { // Matched