Repository navigation
chore(core): find input files with node:fs globSync #1180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ovflowd
wants to merge
2
commits into
main
Choose a base branch
from
chore/node-glob
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@doc-kit/core': patch | ||
| --- | ||
|
|
||
| Find input files with Node.js's `fs.globSync` instead of `tinyglobby` and `glob-parent`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| 'use strict'; | ||
|
|
||
| import assert from 'node:assert/strict'; | ||
| import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { after, before, describe, it } from 'node:test'; | ||
| import { pathToFileURL } from 'node:url'; | ||
|
|
||
| import { findFiles, globParent } from '../glob.mjs'; | ||
|
|
||
| describe('findFiles', () => { | ||
| let root; | ||
|
|
||
| before(() => { | ||
| root = mkdtempSync(join(tmpdir(), 'doc-kit-glob-')); | ||
| mkdirSync(join(root, 'api', 'nested.md'), { recursive: true }); | ||
| writeFileSync(join(root, 'api', 'fs.md'), ''); | ||
| writeFileSync(join(root, 'api', 'path.md'), ''); | ||
| writeFileSync(join(root, 'api', 'quic.md'), ''); | ||
| writeFileSync(join(root, 'api', 'nested.md', 'deep.md'), ''); | ||
| }); | ||
|
|
||
| after(() => rmSync(root, { recursive: true, force: true })); | ||
|
|
||
| it('should return matching files relative to cwd, without directories', () => { | ||
| assert.deepStrictEqual(findFiles('api/*.md', { cwd: root }).sort(), [ | ||
| 'api/fs.md', | ||
| 'api/path.md', | ||
| 'api/quic.md', | ||
| ]); | ||
| }); | ||
|
|
||
| it('should leave out ignored files', () => { | ||
| assert.deepStrictEqual( | ||
| findFiles('api/*.md', { cwd: root, ignore: ['api/quic.md'] }).sort(), | ||
| ['api/fs.md', 'api/path.md'] | ||
| ); | ||
| }); | ||
|
|
||
| it('should return absolute patterns relative to cwd', () => { | ||
| assert.deepStrictEqual( | ||
| findFiles(`${root.split('\\').join('/')}/api/fs.md`, { cwd: root }), | ||
| ['api/fs.md'] | ||
| ); | ||
| }); | ||
|
|
||
| it('should accept a URL as cwd', () => { | ||
| assert.deepStrictEqual( | ||
| findFiles('*.md', { cwd: pathToFileURL(join(root, 'api')) }).sort(), | ||
| ['fs.md', 'path.md', 'quic.md'] | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('globParent', () => { | ||
| it('should return the directory before the first glob segment', () => { | ||
| assert.strictEqual(globParent('doc/api/*.md'), 'doc/api'); | ||
| assert.strictEqual(globParent('./doc/api/*.md'), './doc/api'); | ||
| assert.strictEqual(globParent('/root/*.js'), '/root'); | ||
| assert.strictEqual(globParent('lib/**/*.js'), 'lib'); | ||
| assert.strictEqual(globParent('a/{b,c}/*.md'), 'a'); | ||
| assert.strictEqual(globParent('a/b/[x-y]*.md'), 'a/b'); | ||
| assert.strictEqual(globParent('a/+(b|c)/*.md'), 'a'); | ||
| assert.strictEqual(globParent('a/b?d/*.md'), 'a'); | ||
| }); | ||
|
|
||
| it('should treat as plain what fs.globSync matches literally', () => { | ||
| assert.strictEqual(globParent('a/(b|c)/*.md'), 'a/(b|c)'); | ||
| assert.strictEqual(globParent('a/b+c/*.md'), 'a/b+c'); | ||
| }); | ||
|
|
||
| it('should read backslashes as separators, like fs.globSync', () => { | ||
| assert.strictEqual(globParent('doc\\api\\*.md'), 'doc/api'); | ||
| assert.strictEqual(globParent('C:\\docs\\*.md'), 'C:/docs'); | ||
| }); | ||
|
|
||
| it('should return the parent directory of a plain path', () => { | ||
| assert.strictEqual(globParent('doc/api/fs.md'), 'doc/api'); | ||
| }); | ||
|
|
||
| it('should fall back to the current or root directory', () => { | ||
| assert.strictEqual(globParent('*.md'), '.'); | ||
| assert.strictEqual(globParent('/*.md'), '/'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { globSync } from 'node:fs'; | ||
| import { relative, resolve, sep } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| // A path segment holding any glob syntax (wildcards, classes, braces or extglobs) | ||
| const GLOB_SEGMENT = /[*?[{]|[!+@]\(/; | ||
|
|
||
| /** | ||
| * Finds the files matching glob patterns, as POSIX paths relative to `cwd` | ||
| * (absolute patterns included). Directories are left out. | ||
| * | ||
| * @param {string | string[]} patterns - Glob patterns to match | ||
| * @param {{ cwd?: string | URL, ignore?: string[] }} [options] | ||
| * @returns {string[]} | ||
| */ | ||
| export const findFiles = (patterns, { cwd = process.cwd(), ignore } = {}) => { | ||
| const root = cwd instanceof URL ? fileURLToPath(cwd) : cwd; | ||
|
|
||
| return globSync(patterns, { cwd: root, exclude: ignore, withFileTypes: true }) | ||
| .filter(dirent => !dirent.isDirectory()) | ||
| .map(dirent => | ||
| relative(root, resolve(root, dirent.parentPath, dirent.name)) | ||
| .split(sep) | ||
| .join('/') | ||
| ); | ||
| }; | ||
|
|
||
| /** | ||
| * Returns the directory part of a glob pattern before its first glob segment, | ||
| * e.g. `doc/api` for `doc/api/*.md`, or the parent directory of a plain path. | ||
| * Like `fs.globSync`, it reads backslashes as separators, never as escapes. | ||
| * | ||
| * @param {string} pattern - A glob pattern | ||
| * @returns {string} | ||
| */ | ||
| export const globParent = pattern => { | ||
| const segments = pattern.replaceAll('\\', '/').split('/'); | ||
| const index = segments.findIndex(segment => GLOB_SEGMENT.test(segment)); | ||
| const parent = segments.slice(0, index === -1 ? -1 : index).join('/'); | ||
|
|
||
| return parent || (segments[0] === '' ? '/' : '.'); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,6 @@ | |
| "unist-util-visit": "^5.1.0" | ||
| }, | ||
| "devDependencies": { | ||
| "tinyglobby": "^0.2.17", | ||
| "unist-builder": "^4.0.0" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.