Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/scrape/commands/codegen-target-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { Command } from "commander";
import { configureTargetCommand } from "../services/command-builder.js";
import { snakeToKebab } from "../services/naming.js";
import { createTargetAction } from "../services/run-target-scrape.js";
import { resolveTargetGroup } from "../services/target-group.js";

export function createCodegenTargetCommands(schema: DecodoSchema): Command[] {
const commands: Command[] = [];

for (const target of schema.listTargets()) {
const commandName = snakeToKebab(target);
const meta = schema.getTargetMeta(target);
const group = resolveTargetGroup(schema, target);
const command = new Command(commandName).description(
meta?.group ? `${meta.group} scrape target` : `${target} scrape target`
group ? `${group} scrape target` : "Scrape target"
);

configureTargetCommand(command, target, schema);
Expand Down
3 changes: 2 additions & 1 deletion src/scrape/commands/list-targets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { DecodoSchema } from "@decodo/sdk-ts";
import { Command } from "commander";
import { snakeToKebab } from "../services/naming.js";
import { resolveTargetGroup } from "../services/target-group.js";

export function createListTargetsCommand(schema: DecodoSchema): Command {
return new Command("targets")
Expand All @@ -9,7 +10,7 @@ export function createListTargetsCommand(schema: DecodoSchema): Command {
const grouped = new Map<string, string[]>();

for (const target of schema.listTargets()) {
const group = schema.getTargetMeta(target)?.group ?? "Other";
const group = resolveTargetGroup(schema, target) ?? "Other";
const names = grouped.get(group) ?? [];
names.push(snakeToKebab(target));
grouped.set(group, names);
Expand Down
14 changes: 14 additions & 0 deletions src/scrape/services/target-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { DecodoSchema } from "@decodo/sdk-ts";

const NO_GROUP = "None";

export function resolveTargetGroup(
schema: DecodoSchema,
target: string
): string | undefined {
const group = schema.getTargetMeta(target)?.group;
if (!group || group === NO_GROUP) {
return;
}
return group;
}
11 changes: 11 additions & 0 deletions tests/scrape/commands/codegen-target-commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@ describe("createCodegenTargetCommands", () => {
true
);
});

it("uses a generic description for targets without a real group", () => {
const schema = BundledSchema.shared;
const commands = createCodegenTargetCommands(schema);

const ungrouped = commands.find((cmd) => cmd.name() === "youtube-video");
expect(ungrouped?.description()).toBe("Scrape target");

const grouped = commands.find((cmd) => cmd.name() === "amazon-product");
expect(grouped?.description()).toBe("Amazon scrape target");
});
});
27 changes: 27 additions & 0 deletions tests/scrape/services/target-group.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { DecodoSchema } from "@decodo/sdk-ts";
import { describe, expect, it } from "vitest";
import { resolveTargetGroup } from "../../../src/scrape/services/target-group.js";

function schemaWithGroup(group: string | undefined): DecodoSchema {
return {
getTargetMeta: () => (group === undefined ? undefined : { group }),
} as unknown as DecodoSchema;
}

describe("resolveTargetGroup", () => {
it("returns the group when it is a real value", () => {
expect(
resolveTargetGroup(schemaWithGroup("Amazon"), "amazon_product")
).toBe("Amazon");
});

it.each([
"None",
"",
undefined,
])("returns undefined for the %p sentinel group", (group) => {
expect(
resolveTargetGroup(schemaWithGroup(group), "youtube_video")
).toBeUndefined();
});
});
Loading