Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

fix: include bundled packages in vocabulary listings (#345)

wanchenxing7e03b01a6185
diff --git a/packages/video-cli/src/vocabulary.ts b/packages/video-cli/src/vocabulary.ts
index 1935038e..69cc0090 100644
--- a/packages/video-cli/src/vocabulary.ts
+++ b/packages/video-cli/src/vocabulary.ts
@@ -65,15 +65,14 @@ export type PackageListing = {
};
export async function listPackages(projectRoot: string): Promise<readonly PackageListing[]> {
- // Every scope, and the project's own directory, from both the project's node_modules and the
- // Distribution's: the packages a project does not carry are in the Distribution, and the ones it
- // does are beside it.
+ // Discover installed scopes and unlinked packages in both the project and the Distribution.
const roots = [...new Set([
- join(projectRoot, "node_modules"),
- ...(videoCliDistribution.packageRoot === undefined ? [] : [join(videoCliDistribution.packageRoot, "node_modules")]),
+ projectRoot,
+ ...(videoCliDistribution.packageRoot === undefined ? [] : [videoCliDistribution.packageRoot]),
])];
const candidates: { readonly name: string; readonly directory: string }[] = [];
- for (const modules of roots) {
+ for (const root of roots) {
+ const modules = join(root, "node_modules");
const scopes = (await readdir(modules, { withFileTypes: true }).catch(() => []))
.filter((entry) => entry.isDirectory() && entry.name.startsWith("@"))
.map((entry) => entry.name)
@@ -85,12 +84,14 @@ export async function listPackages(projectRoot: string): Promise<readonly Packag
}
}
}
- // A project's own packages are its `packages/<name>/`, where the loader finds them by name whether
- // or not a package manager linked them.
- for (const entry of (await readdir(join(projectRoot, "packages")).catch(() => [] as string[])).sort()) {
- const directory = join(projectRoot, "packages", entry);
- const own = await readJson<{ readonly name?: string }>(join(directory, "package.json"));
- if (own?.name !== undefined && !candidates.some((item) => item.name === own.name)) candidates.push({ name: own.name, directory });
+ // The loader also finds packages by name under `packages/<name>/`, even without package-manager
+ // links. Published Distributions carry their built-in packages here.
+ for (const root of roots) {
+ for (const entry of (await readdir(join(root, "packages")).catch(() => [] as string[])).sort()) {
+ const directory = join(root, "packages", entry);
+ const own = await readJson<{ readonly name?: string }>(join(directory, "package.json"));
+ if (own?.name !== undefined && !candidates.some((item) => item.name === own.name)) candidates.push({ name: own.name, directory });
+ }
}
const listing: PackageListing[] = [];
for (const { name, directory } of candidates) {
diff --git a/packages/video-cli/test/vocabulary.test.ts b/packages/video-cli/test/vocabulary.test.ts
index 0234d63e..6c93a148 100644
--- a/packages/video-cli/test/vocabulary.test.ts
+++ b/packages/video-cli/test/vocabulary.test.ts
@@ -1,10 +1,14 @@
import assert from "node:assert/strict";
-import { basename, dirname, resolve } from "node:path";
+import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { basename, dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import test from "node:test";
import type { CliIo } from "@hypit/cli";
+import { markupSurfaceHostFacetAbi } from "@hypit/markup";
+import { videoCliDistribution } from "../src/distribution.js";
import { listPackages, listSurfaces, runVocabularyCli, visualSchema } from "../src/vocabulary.js";
const repositoryRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..", "..");
@@ -24,12 +28,77 @@ function io(): { readonly io: CliIo; text: () => string } {
test("the listing sees every activatable package from the repository root", async () => {
const listing = await listPackages(repositoryRoot);
+ assert.equal(new Set(listing.map((item) => item.name)).size, listing.length);
const pipeline = listing.find((item) => item.name === "@hypit/media-pipeline");
assert.ok(pipeline !== undefined, "media-pipeline is listed");
assert.ok(pipeline.tags.includes("StillVideo"), `tags: ${pipeline.tags.join(", ")}`);
assert.equal(pipeline.unreadable, undefined);
});
+test("vocabulary discovers bundled packages from a separate project without node_modules", async () => {
+ const root = await mkdtemp(join(tmpdir(), "hypit-vocabulary-"));
+ const project = join(root, "project");
+ const distribution = join(root, "distribution");
+ const originalRoot = Object.getOwnPropertyDescriptor(videoCliDistribution, "packageRoot")!;
+ async function packageAt(directory: string, name: string, description = name): Promise<void> {
+ await mkdir(directory, { recursive: true });
+ await writeFile(join(directory, "package.json"), JSON.stringify({
+ name, version: "1.0.0", type: "module", description,
+ hypit: { activation: "./activation.mjs" },
+ }));
+ await writeFile(join(directory, "activation.mjs"), `export default {
+ format: "hypit.node-package@1",
+ hostFacets: [{
+ abi: ${JSON.stringify(markupSurfaceHostFacetAbi)},
+ implementation: {
+ module: { name: ${JSON.stringify(name)}, version: "1" },
+ surface: "card", tag: "Card", mode: "raw", outputs: [], handler: () => [],
+ },
+ }],
+ };`);
+ }
+ try {
+ await mkdir(project, { recursive: true });
+ await writeFile(join(project, "package.json"), JSON.stringify({ name: "example-project", private: true }));
+ const bundled = join(distribution, "packages", "bundled");
+ await packageAt(bundled, "@hypit/bundled");
+ await writeFile(join(bundled, "README.md"), "Example bundled component.\n");
+ await mkdir(join(distribution, "packages", "inactive"), { recursive: true });
+ await writeFile(join(distribution, "packages", "inactive", "package.json"), JSON.stringify({ name: "@hypit/inactive" }));
+ Object.defineProperty(videoCliDistribution, "packageRoot", { value: distribution });
+
+ const human = io();
+ await runVocabularyCli(["vocabulary"], human.io, project);
+ assert.match(human.text(), /@hypit\/bundled\n\s+tags: Card/u);
+ const json = io();
+ await runVocabularyCli(["vocabulary", "--json"], json.io, project);
+ assert.deepEqual(JSON.parse(json.text()).packages, [{
+ name: "@hypit/bundled", description: "@hypit/bundled", tags: ["Card"],
+ }]);
+
+ const named = io();
+ await runVocabularyCli(["vocabulary", "@hypit/bundled", "--tag", "Card", "--json"], named.io, project);
+ const surfaces = JSON.parse(named.text()).surfaces;
+ assert.equal(surfaces.length, 1);
+ assert.equal(surfaces[0].package, "@hypit/bundled");
+ assert.equal(surfaces[0].tag, "Card");
+ assert.equal(basename(surfaces[0].readme), "README.md");
+
+ // Workspace links and project components must not duplicate the bundled listing.
+ await packageAt(join(distribution, "node_modules", "@hypit", "bundled"), "@hypit/bundled");
+ await packageAt(join(project, "packages", "local"), "@studio/local");
+ await packageAt(join(project, "node_modules", "@studio", "custom"), "@studio/custom", "project package");
+ await packageAt(join(distribution, "packages", "custom"), "@studio/custom", "distribution fallback");
+ const listing = await listPackages(project);
+ assert.deepEqual(listing.map((item) => item.name).sort(), ["@hypit/bundled", "@studio/custom", "@studio/local"]);
+ assert.equal(listing.find((item) => item.name === "@studio/custom")!.description, "project package");
+ assert.ok(listing.every((item) => item.unreadable === undefined && item.tags.includes("Card")));
+ } finally {
+ Object.defineProperty(videoCliDistribution, "packageRoot", originalRoot);
+ await rm(root, { recursive: true, force: true });
+ }
+});
+
test("a named package answers with its own Surfaces only", async () => {
const surfaces = await listSurfaces(repositoryRoot, ["@hypit/media-pipeline"], ["StillVideo"]);
assert.equal(surfaces.length, 1);