Skip to content

Commit

Permalink
fix(forge): simplify workspace() options to use a single directory …
Browse files Browse the repository at this point in the history
…parameter
  • Loading branch information
tugrulates committed Feb 28, 2025
1 parent dc56d34 commit 0ccaf8c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
6 changes: 3 additions & 3 deletions tool/forge/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ Deno.test("packageInfo() returns update at initial version", async () => {
Deno.test("workspace() returns simple package", async () => {
await using directory = await tempDirectory();
await createPackage(directory.path(), { name: "name", version: "version" });
const packages = await workspace({ directories: [directory.path()] });
const packages = await workspace({ directory: directory.path() });
assertEquals(packages, [{
directory: directory.path(),
module: "name",
Expand All @@ -296,7 +296,7 @@ Deno.test("workspace() returns monorepo packages", async () => {
name: "second",
version: "second_version",
});
const packages = await workspace({ directories: [directory.path()] });
const packages = await workspace({ directory: directory.path() });
assertEquals(packages, [pkg1, pkg2]);
});

Expand All @@ -312,6 +312,6 @@ Deno.test("workspace() does not return nested workspace packages", async () => {
name: "second",
version: "second_version",
});
const packages = await workspace({ directories: [directory.path()] });
const packages = await workspace({ directory: directory.path() });
assertEquals(packages, [pkg1]);
});
24 changes: 9 additions & 15 deletions tool/forge/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import { git, GitError, type Tag } from "@roka/git";
import { conventional, type ConventionalCommit } from "@roka/git/conventional";
import { assert } from "@std/assert";
import { distinctBy } from "@std/collections";
import { basename, dirname, fromFileUrl, join, normalize } from "@std/path";
import {
canParse as canParseVersion,
Expand Down Expand Up @@ -140,7 +139,7 @@ export type PermissionDescriptor<T> =
/** Options for {@linkcode packageInfo}. */
export interface PackageOptions {
/**
* Package directory to analyze
* Directory to return package from.
*
* If a directory is not defined, the package of the main module is returned.
*/
Expand All @@ -150,10 +149,10 @@ export interface PackageOptions {
/** Options for {@linkcode workspace}. */
export interface WorkspaceOptions {
/**
* List of directories to fetch packages from.
* Directory to return packages from.
* @default {["."]}
*/
directories?: string[];
directory?: string;
}

/** Returns information about a package. */
Expand Down Expand Up @@ -198,17 +197,12 @@ export async function packageInfo(options?: PackageOptions): Promise<Package> {
export async function workspace(
options?: WorkspaceOptions,
): Promise<Package[]> {
const directories = options?.directories ?? ["."];
const packages = await Promise.all(
directories?.map(async (directory) => {
const pkg = await packageInfo({ directory, ...options });
if (pkg.config.workspace === undefined) return pkg;
return await Promise.all(pkg.config.workspace.map(async (child) => {
return await packageInfo({ directory: join(pkg.directory, child) });
}));
}),
);
return distinctBy(packages.flat(), (pkg) => pkg.directory);
const directory = options?.directory ?? ".";
const pkg = await packageInfo({ directory, ...options });
if (pkg.config.workspace === undefined) return [pkg];
return await Promise.all(pkg.config.workspace.map(async (child) => {
return await packageInfo({ directory: join(pkg.directory, child) });
}));
}

async function readConfig(directory: string): Promise<Config> {
Expand Down

0 comments on commit 0ccaf8c

Please sign in to comment.