Skip to content

Commit

Permalink
Add glob option to ignore hidden files (#1791)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
joshmgross authored Aug 15, 2024
1 parent 48a6537 commit 50f2977
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/glob/RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# @actions/glob Releases

This comment has been minimized.

Copy link
@axcep747

axcep747 Oct 8, 2024


### 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)

Expand Down
22 changes: 21 additions & 1 deletion packages/glob/__tests__/internal-globber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
// <root>
// <root>/.emptyFolder
Expand All @@ -734,6 +734,26 @@ describe('globber', () => {
])
})

it('ignores hidden files when excludeHiddenFiles is set', async () => {
// Create the following layout:
// <root>
// <root>/.emptyFolder
// <root>/.file
// <root>/.folder
// <root>/.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:
// <root>/hello/world.txt
Expand Down
2 changes: 1 addition & 1 deletion packages/glob/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/glob/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@actions/glob",
"version": "0.4.0",
"version": "0.5.0",
"preview": true,
"description": "Actions glob lib",
"keywords": [
Expand Down
8 changes: 7 additions & 1 deletion packages/glob/src/internal-glob-options-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export function getOptions(copy?: GlobOptions): GlobOptions {
followSymbolicLinks: true,
implicitDescendants: true,
matchDirectories: true,
omitBrokenSymbolicLinks: true
omitBrokenSymbolicLinks: true,
excludeHiddenFiles: false
}

if (copy) {
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions packages/glob/src/internal-glob-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions packages/glob/src/internal-globber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 50f2977

Please sign in to comment.