Skip to content

Commit

Permalink
feat(utils): globbyOption is removed from glob
Browse files Browse the repository at this point in the history
And integrated into `GlobOptions` directly
  • Loading branch information
jjangga0214 committed Jun 12, 2023
1 parent 1a00a4a commit 0b7cb17
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/utils-glob-rm-globbyoption.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@haetae/utils': patch
---

**BREAKING CHANGE**: `globbyOption` is removed and integrated into `GlobOptions` directly.
11 changes: 7 additions & 4 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ export type PromiseOr<T> = Promise<T> | T
export type Rec = Record<string, unknown>

export interface ToAbsolutePathOptions {
path: string
path?: string
rootDir: string | (() => string)
}

export function toAbsolutePath({
path,
rootDir,
}: ToAbsolutePathOptions): string {
if (upath.isAbsolute(path)) {
return upath.normalize(path)
}
if (typeof rootDir === 'function') {
// eslint-disable-next-line no-param-reassign
rootDir = rootDir()
}
if (path === undefined) {
return upath.resolve(rootDir)
}
if (upath.isAbsolute(path)) {
return upath.normalizeSafe(path)
}

return upath.resolve(rootDir, path)
}
Expand Down
14 changes: 8 additions & 6 deletions packages/docs/pages/apis/haetae-utils.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,11 @@ interface GlobOptions {

A function to find files by a glob pattern.<br/>
Internally, the task is delegated to [**`globby`**](https://github.com/sindresorhus/globby/tree/v13.1.4)
<small>(v13 as of writing)</small>.<br/>
<small>(v13 as of writing)</small>.
`glob` is a [facade function](https://en.wikipedia.org/wiki/Facade_pattern) for `globby`,
providing more handy experience by default options and postprocessing.
providing a more handy experience by sane default `rootDir` options and postprocessing.
There're many options, but aren't explained here, except `roodDir`.
Refer to [globby v13 docs](https://github.com/sindresorhus/globby/tree/v13.1.4) for other options.

**Type**

Expand All @@ -183,10 +185,10 @@ providing more handy experience by default options and postprocessing.

- `patterns{:ts}`: Array of glob patterns. <small>(e.g. `['**/*.test.{ts, tsx}']{:ts}`)</small>
- `options?{:ts}` :
- `options.rootDir?{:ts}` : A directory to start search.
If `options.globbyOptions.cwd{:ts}` is not provided (`undefined{:ts}`), this value is used.
Ignored otherwise. <small>(default: [`core.getConfigDirname(){:ts}`](./haetae-core#getconfigdirname))</small>
- `options.globbyOptions?{:ts}` : Refer to [globby v13 docs](https://github.com/sindresorhus/globby/tree/v13.1.4).
- `rootDir?{:ts}` : A directory to start search.
Its role is same as globby's `cwd` option, but make sure to use `rootDir`, not `cwd`.
<small>(default: [`core.getConfigDirname(){:ts}`](./haetae-core#getconfigdirname))</small>


### `ExecOptions`

Expand Down
18 changes: 8 additions & 10 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,18 @@ export async function recordData({
}
}

export interface GlobOptions {
export interface GlobOptions extends Omit<GlobbyOptions, 'cwd'> {
rootDir?: string // A facade option for `globbyOptions.cwd`
globbyOptions?: GlobbyOptions
}

export async function glob(
patterns: readonly string[],
{ rootDir = core.getConfigDirname(), globbyOptions = {} }: GlobOptions = {},
options: GlobOptions = {},
): Promise<string[]> {
// eslint-disable-next-line no-param-reassign
rootDir = toAbsolutePath({ path: rootDir, rootDir: core.getConfigDirname })
const rootDir = toAbsolutePath({
path: options.rootDir,
rootDir: core.getConfigDirname,
})
// Why `walkUpCountMax` is needed? REF: https://github.com/sindresorhus/globby/issues/168
const walkUpCountMax = { value: 0 }
// eslint-disable-next-line no-param-reassign
Expand All @@ -70,11 +71,8 @@ export async function glob(
})

const res = await globby(patterns, {
...globbyOptions,
cwd:
globbyOptions.cwd ||
upath.join(rootDir, '../'.repeat(walkUpCountMax.value)),
gitignore: globbyOptions.gitignore ?? true,
cwd: upath.resolve(rootDir, '../'.repeat(walkUpCountMax.value)),
...options,
})
return [...new Set(res.map((f) => upath.resolve(rootDir, f)))]
}
Expand Down

0 comments on commit 0b7cb17

Please sign in to comment.