Skip to content

Commit

Permalink
Add schema option to glob-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronccasanova committed Oct 24, 2023
1 parent 7e2942f commit ce8bf78
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-points-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'glob-modules': minor
---

Added a `schema` option to validate the shape of globbed modules
12 changes: 6 additions & 6 deletions package-lock.json

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

53 changes: 45 additions & 8 deletions packages/glob-modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ npm install glob-modules

## Usage

**Example directory structure:**
The examples below assume the following directory structure:

```
src/
Expand All @@ -21,14 +21,20 @@ src/
|__ baz.txt
```

Where the modules are defined as follows:

```js
// src/commands/foo.js
export default 'foo'

// src/commands/bar.js
export default 'bar'
export const baz = 'baz'
```

### Example: Basic

```js
// src/main.js
import { globModules } from 'glob-modules'

Expand All @@ -37,14 +43,45 @@ const { commands } = await globModules('./commands/*.js', {
})
/*
commands === {
foo: {
default: 'foo',
},
bar: {
default: 'bar',
baz: 'baz',
},
foo: {
default: 'foo',
},
bar: {
default: 'bar',
baz: 'baz',
},
}
*/
```

### Example: TypeScript

```ts
// src/main.js
import { globModules } from 'glob-modules'

const { commands } = await globModules<{
foo: string
bar: { default: string; baz: string }
}>('./commands/*.js', { importMeta: import.meta })
```

### Example: Zod

```ts
import { globModules } from 'glob-modules'
import { z } from 'zod'

const { commands } = await globModules('./commands/*.js', {
importMeta: import.meta,
schema: z.object({
foo: z.object({
default: z.string(),
}),
bar: z.object({
default: z.string(),
baz: z.string(),
}),
}),
})
```
11 changes: 9 additions & 2 deletions packages/glob-modules/src/glob-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ type GlobModulesCWDOptions =
}

export type GlobModulesPatterns = GlobbyParameters[0]
export type GlobModulesOptions = GlobModulesCWDOptions
export type GlobModulesOptions<T extends GlobModulesResult> =
GlobModulesCWDOptions & {
schema?: { parse: (globbedModules: unknown) => T }
}

// TODO: Add import-lazy
// - lib: https://www.npmjs.com/package/import-lazy
Expand All @@ -37,7 +40,7 @@ type GlobModulesResult = { [globModuleNames: string]: any }

export async function globModules<T extends GlobModulesResult>(
patterns: GlobModulesPatterns,
options: GlobModulesOptions,
options: GlobModulesOptions<T>,
): Promise<T> {
const cwd = getCWD(options)

Expand Down Expand Up @@ -72,6 +75,10 @@ export async function globModules<T extends GlobModulesResult>(
}
}

if (options.schema) {
return options.schema.parse(modules)
}

return modules as T
}

Expand Down

3 comments on commit ce8bf78

@vercel
Copy link

@vercel vercel bot commented on ce8bf78 Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

aacc-scales – ./apps/scales

aacc-scales-git-main-aaronccasanova.vercel.app
aacc-scales-aaronccasanova.vercel.app
aacc-scales.vercel.app

@vercel
Copy link

@vercel vercel bot commented on ce8bf78 Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

aacc-next-ts – ./recipes/next-ts

aacc-next-ts-aaronccasanova.vercel.app
aacc-next-ts.vercel.app
aacc-next-ts-git-main-aaronccasanova.vercel.app

@vercel
Copy link

@vercel vercel bot commented on ce8bf78 Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

aacc-next-ts-styled-comps – ./recipes/next-ts-styled-comps

aacc-next-ts-styled-comps.vercel.app
aacc-next-ts-styled-comps-git-main-aaronccasanova.vercel.app
aacc-next-ts-styled-comps-aaronccasanova.vercel.app

Please sign in to comment.