Skip to content

Commit

Permalink
Implement std.glob() function
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewlacy committed Aug 31, 2024
1 parent 329e8dc commit 6d7a935
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/std/core/recipes/glob.bri
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as runtime from "../runtime.bri";
import { type AsyncRecipe, type Recipe, createRecipe } from "./recipe.bri";
import type { Directory } from "./directory.bri";

/**
* Returns a directory recipe containing only files that match one of the
* specified glob patterns.
*
* ## Example
*
* ```typescript
* import * as std from "std";
*
* const dir = std.directory({
* "file.txt": std.file("Hello, world!"),
* "script.sh": std.file("echo 'Hello, world!'"),
* });
*
* // Get only the text files from the directory
* const textFiles = std.glob(dir, ["*.txt"]);
* ```
*/
export function glob(
recipe: AsyncRecipe<Directory>,
patterns: string[],
): Recipe<Directory> {
return createRecipe<Directory>(["directory"], {
sourceDepth: 1,
briocheSerialize: async (meta) => {
const serializedDirectory = await (await recipe).briocheSerialize();
return {
meta,
type: "glob",
directory: serializedDirectory,
patterns: patterns.map((pattern) => runtime.bstring(pattern)),
};
},
});
}
1 change: 1 addition & 0 deletions packages/std/core/recipes/index.bri
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export { memo, createProxy } from "./proxy.bri";
export { symlink, type SymlinkOptions, Symlink } from "./symlink.bri";
export { sync } from "./sync.bri";
export { collectReferences } from "./collect_references.bri";
export { glob } from "./glob.bri";
export {
type AsyncRecipe,
type Recipe,
Expand Down
1 change: 1 addition & 0 deletions packages/std/core/recipes/recipe.bri
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type RecipeSerialization<T extends Artifact> = runtime.WithMeta &
| runtime.PeelRecipe
| runtime.GetRecipe
| runtime.InsertRecipe
| runtime.GlobRecipe
| runtime.CollectReferencesRecipe
| runtime.ProxyRecipe
| runtime.SyncRecipe
Expand Down
7 changes: 7 additions & 0 deletions packages/std/core/runtime.bri
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export type Recipe =
| PeelRecipe
| GetRecipe
| InsertRecipe
| GlobRecipe
| SetPermissionsRecipe
| CollectReferencesRecipe
| ProxyRecipe
Expand Down Expand Up @@ -177,6 +178,12 @@ export type InsertRecipe = WithMeta & {
recipe?: Recipe | null | undefined;
};

export type GlobRecipe = WithMeta & {
type: "glob";
directory: Recipe;
patterns: BString[];
};

export type SetPermissionsRecipe = WithMeta & {
type: "set_permissions";
file: Recipe;
Expand Down

0 comments on commit 6d7a935

Please sign in to comment.