diff --git a/cli/src/__tests__/commands/xlr-bundle.test.ts b/cli/src/__tests__/commands/xlr-bundle.test.ts new file mode 100644 index 0000000..61eac83 --- /dev/null +++ b/cli/src/__tests__/commands/xlr-bundle.test.ts @@ -0,0 +1,358 @@ +import fs from "fs"; +import os from "os"; +import path from "path"; +import { test, expect, describe, beforeEach, afterEach } from "vitest"; +import XLRBundle from "../../commands/xlr/bundle"; +import { + spyOnWarn, + writeSource, + readBundledManifest, +} from "./xlr-test-helpers"; + +describe("xlr bundle", () => { + let workspace: string; + let cwd: string; + let warn: ReturnType; + + beforeEach(() => { + workspace = fs.realpathSync( + fs.mkdtempSync(path.join(os.tmpdir(), "xlr-bundle-")), + ); + cwd = process.cwd(); + process.chdir(workspace); + warn = spyOnWarn(); + }); + + afterEach(() => { + process.chdir(cwd); + fs.rmSync(workspace, { recursive: true, force: true }); + warn.mockRestore(); + }); + + /** + * These exercise `bundle`'s core flattening/collision/error behavior via explicit `-s` flags — + * the only input mechanism `rules_player`'s `xlr_bundle` macro uses (it never writes a config + * file, so `config.xlr.bundleSources` is unreachable from Bazel). Behavior covered here applies + * identically under Bazel and non-Bazel invocation. + */ + describe("via --source flags", () => { + test("flattens Assets/Views into one manifest keyed by type name", async () => { + const sourceA = path.join(workspace, "source-a"); + writeSource(sourceA, { + packages: { react: { name: "@test/a", version: "1.0.0" } }, + assets: [{ capabilityName: "Assets.InputAsset", typeName: "input" }], + views: [{ capabilityName: "Views.SummaryAsset", typeName: "summary" }], + }); + + const sourceB = path.join(workspace, "source-b"); + writeSource(sourceB, { + packages: { react: { name: "@test/b", version: "2.0.0" } }, + assets: [{ capabilityName: "Assets.TextAsset", typeName: "text" }], + }); + + await XLRBundle.run(["-s", sourceA, "-s", sourceB, "-o", "out"]); + + expect(readBundledManifest(path.join(workspace, "out"))).toStrictEqual({ + capabilities: { + input: [{ react: { name: "@test/a", version: "1.0.0" } }], + summary: [{ react: { name: "@test/a", version: "1.0.0" } }], + text: [{ react: { name: "@test/b", version: "2.0.0" } }], + }, + }); + }); + + test("collapses a duplicate entry from the same source under two capabilities", async () => { + const sourceA = path.join(workspace, "source-a"); + writeSource(sourceA, { + packages: { react: { name: "@test/a", version: "1.0.0" } }, + assets: [{ capabilityName: "Assets.ActionCard", typeName: "action" }], + views: [{ capabilityName: "Views.ActionCardView", typeName: "action" }], + }); + + await XLRBundle.run(["-s", sourceA, "-o", "out"]); + + expect( + readBundledManifest(path.join(workspace, "out")).capabilities.action, + ).toStrictEqual([{ react: { name: "@test/a", version: "1.0.0" } }]); + }); + + test("ignores DataTypes/Formatters/Validators/Expressions entirely", async () => { + const sourceA = path.join(workspace, "source-a"); + const manifestDir = path.join(sourceA, "dist", "xlr"); + fs.mkdirSync(manifestDir, { recursive: true }); + fs.writeFileSync( + path.join(sourceA, "dist", "xlr", "manifest.json"), + JSON.stringify({ + pluginName: "source-a", + capabilities: { + Assets: [], + Views: [], + DataTypes: ["DataTypes.Foo"], + Formatters: ["Formatters.Bar"], + }, + }), + ); + + await XLRBundle.run(["-s", sourceA, "-o", "out"]); + + expect(readBundledManifest(path.join(workspace, "out"))).toStrictEqual({ + capabilities: {}, + }); + }); + + test("warns when a source contributing entries has no package identity of its own", async () => { + const sourceA = path.join(workspace, "source-a"); + writeSource(sourceA, { + assets: [{ capabilityName: "Assets.InputAsset", typeName: "input" }], + }); + + await XLRBundle.run(["-s", sourceA, "-o", "out"]); + + expect(warn).toHaveBeenCalledWith( + expect.stringContaining("no package identity"), + ); + expect(readBundledManifest(path.join(workspace, "out"))).toStrictEqual({ + capabilities: { input: [{}] }, + }); + }); + + test("does not warn about missing package identity for a source with no Assets/Views at all", async () => { + const sourceA = path.join(workspace, "source-a"); + writeSource(sourceA, {}); + + await XLRBundle.run(["-s", sourceA, "-o", "out"]); + + expect(warn).not.toHaveBeenCalledWith( + expect.stringContaining("no package identity"), + ); + }); + + test("respects --manifestPath for a non-default convention", async () => { + const sourceA = path.join(workspace, "source-a"); + writeSource(sourceA, { + manifestPath: "build/out/manifest.json", + packages: { react: { name: "@test/a", version: "1.0.0" } }, + assets: [{ capabilityName: "Assets.InputAsset", typeName: "input" }], + }); + + await XLRBundle.run([ + "-s", + sourceA, + "--manifestPath", + "build/out/manifest.json", + "-o", + "out", + ]); + + expect(readBundledManifest(path.join(workspace, "out"))).toStrictEqual({ + capabilities: { + input: [{ react: { name: "@test/a", version: "1.0.0" } }], + }, + }); + }); + + test("resolves a --source given as an installed package name", async () => { + const pkgDir = path.join(workspace, "node_modules", "@test", "a"); + writeSource(pkgDir, { + packages: { react: { name: "@test/a", version: "1.0.0" } }, + assets: [{ capabilityName: "Assets.InputAsset", typeName: "input" }], + }); + fs.writeFileSync( + path.join(pkgDir, "package.json"), + JSON.stringify({ + name: "@test/a", + version: "1.0.0", + main: "index.js", + }), + ); + fs.writeFileSync(path.join(pkgDir, "index.js"), "module.exports = {};"); + + await XLRBundle.run(["-s", "@test/a", "-o", "out"]); + + expect(readBundledManifest(path.join(workspace, "out"))).toStrictEqual({ + capabilities: { + input: [{ react: { name: "@test/a", version: "1.0.0" } }], + }, + }); + }); + + test("warns and skips a source with no readable manifest.json, without failing the run", async () => { + await XLRBundle.run([ + "-s", + path.join(workspace, "does-not-exist"), + "-o", + "out", + ]); + + expect(warn).toHaveBeenCalledWith( + expect.stringContaining("does-not-exist"), + ); + expect(readBundledManifest(path.join(workspace, "out"))).toStrictEqual({ + capabilities: {}, + }); + }); + + test("fails when a capability's type file has no type name", async () => { + const sourceA = path.join(workspace, "source-a"); + writeSource(sourceA, { + packages: { react: { name: "@test/a", version: "1.0.0" } }, + assets: [{ capabilityName: "Assets.WeirdAsset", typeName: undefined }], + }); + + await expect(XLRBundle.run(["-s", sourceA, "-o", "out"])).rejects.toThrow( + /Assets\.WeirdAsset/, + ); + }); + + test("writes a manifest.js wrapper that re-exports the JSON", async () => { + const sourceA = path.join(workspace, "source-a"); + writeSource(sourceA, { + packages: { react: { name: "@test/a", version: "1.0.0" } }, + assets: [{ capabilityName: "Assets.InputAsset", typeName: "input" }], + }); + + await XLRBundle.run(["-s", sourceA, "-o", "out"]); + + // eslint-disable-next-line @typescript-eslint/no-require-imports + const wrapped = require( + path.join(workspace, "out", "xlr", "manifest.js"), + ); + expect(wrapped).toStrictEqual( + readBundledManifest(path.join(workspace, "out")), + ); + }); + + test("copies a source's own metaData through onto every entry it contributes", async () => { + const sourceA = path.join(workspace, "source-a"); + writeSource(sourceA, { + packages: { react: { name: "@test/a", version: "1.0.0" } }, + metaData: { kind: "test" }, + assets: [ + { capabilityName: "Assets.ActionAsset", typeName: "action" }, + { capabilityName: "Assets.InputAsset", typeName: "input" }, + ], + }); + + await XLRBundle.run(["-s", sourceA, "-o", "out"]); + + expect(readBundledManifest(path.join(workspace, "out"))).toStrictEqual({ + capabilities: { + action: [ + { + react: { name: "@test/a", version: "1.0.0" }, + metaData: { kind: "test" }, + }, + ], + input: [ + { + react: { name: "@test/a", version: "1.0.0" }, + metaData: { kind: "test" }, + }, + ], + }, + }); + }); + + test("keeps colliding entries apart by each source's own metaData", async () => { + const sourceA = path.join(workspace, "source-a"); + writeSource(sourceA, { + packages: { react: { name: "@test/a", version: "1.0.0" } }, + metaData: { kind: "test" }, + assets: [{ capabilityName: "Assets.ActionAsset", typeName: "action" }], + }); + + const sourceB = path.join(workspace, "source-b"); + writeSource(sourceB, { + packages: { react: { name: "@test/custom-action", version: "1.0.0" } }, + metaData: { kind: "other" }, + assets: [{ capabilityName: "Assets.ActionAsset", typeName: "action" }], + }); + + await XLRBundle.run(["-s", sourceA, "-s", sourceB, "-o", "out"]); + + expect( + readBundledManifest(path.join(workspace, "out")).capabilities.action, + ).toStrictEqual([ + { + react: { name: "@test/a", version: "1.0.0" }, + metaData: { kind: "test" }, + }, + { + react: { name: "@test/custom-action", version: "1.0.0" }, + metaData: { kind: "other" }, + }, + ]); + }); + + test("omits metaData entirely for a source that declared none", async () => { + const sourceA = path.join(workspace, "source-a"); + writeSource(sourceA, { + packages: { react: { name: "@test/a", version: "1.0.0" } }, + assets: [{ capabilityName: "Assets.InputAsset", typeName: "input" }], + }); + + await XLRBundle.run(["-s", sourceA, "-o", "out"]); + + expect( + readBundledManifest(path.join(workspace, "out")).capabilities.input, + ).toStrictEqual([{ react: { name: "@test/a", version: "1.0.0" } }]); + }); + }); + + /** + * `config.xlr.bundleSources` is the non-Bazel path only — how a DSL package supplies its source + * list without hand-typing `-s` flags into a build script. Bazel's `xlr_bundle` macro always + * uses explicit `-s` flags instead (see the sibling describe block above). + */ + describe("via config.xlr.bundleSources (non-Bazel only)", () => { + test("reads the source list from config.xlr.bundleSources when no --source flags are given", async () => { + const sourceA = path.join(workspace, "source-a"); + writeSource(sourceA, { + packages: { react: { name: "@test/a", version: "1.0.0" } }, + assets: [{ capabilityName: "Assets.InputAsset", typeName: "input" }], + }); + + const configPath = path.join(workspace, "player.config.json"); + fs.writeFileSync( + configPath, + JSON.stringify({ xlr: { bundleSources: [sourceA] } }), + ); + + await XLRBundle.run(["-o", "out", "--config", configPath]); + + expect(readBundledManifest(path.join(workspace, "out"))).toStrictEqual({ + capabilities: { + input: [{ react: { name: "@test/a", version: "1.0.0" } }], + }, + }); + }); + + test("--source flags override config.xlr.bundleSources when given", async () => { + const sourceA = path.join(workspace, "source-a"); + writeSource(sourceA, { + packages: { react: { name: "@test/a", version: "1.0.0" } }, + assets: [{ capabilityName: "Assets.InputAsset", typeName: "input" }], + }); + + const sourceB = path.join(workspace, "source-b"); + writeSource(sourceB, { + packages: { react: { name: "@test/b", version: "2.0.0" } }, + assets: [{ capabilityName: "Assets.TextAsset", typeName: "text" }], + }); + + const configPath = path.join(workspace, "player.config.json"); + fs.writeFileSync( + configPath, + JSON.stringify({ xlr: { bundleSources: [sourceA] } }), + ); + + await XLRBundle.run(["-s", sourceB, "-o", "out", "--config", configPath]); + + expect(readBundledManifest(path.join(workspace, "out"))).toStrictEqual({ + capabilities: { + text: [{ react: { name: "@test/b", version: "2.0.0" } }], + }, + }); + }); + }); +}); diff --git a/cli/src/__tests__/commands/xlr-compile-packages.test.ts b/cli/src/__tests__/commands/xlr-compile-packages.test.ts index 2d2c10f..20b3065 100644 --- a/cli/src/__tests__/commands/xlr-compile-packages.test.ts +++ b/cli/src/__tests__/commands/xlr-compile-packages.test.ts @@ -8,7 +8,7 @@ import { spyOnWarn, writePlayerConfig, readManifest, -} from "./xlr-compile-test-helpers"; +} from "./xlr-test-helpers"; describe("xlr compile package info", () => { /** An isolated root, so nothing on the ambient filesystem can be picked up */ @@ -28,6 +28,7 @@ describe("xlr compile package info", () => { delete process.env.XLR_PACKAGE_NAME; delete process.env.XLR_IOS_PACKAGE_NAME; delete process.env.XLR_ANDROID_PACKAGE_NAME; + delete process.env.XLR_META_DATA; delete process.env.JS_BINARY__EXECROOT; }); @@ -192,6 +193,63 @@ describe("xlr compile package info", () => { ); }); }); + + describe("metaData", () => { + test("stamps config.xlr.metaData into the manifest", async () => { + writeFixture(workspace, { name: "@test/plugin", version: "2.3.4" }); + const configPath = path.join(workspace, "player.config.json"); + fs.writeFileSync( + configPath, + JSON.stringify({ xlr: { metaData: { kind: "test" } } }), + ); + + await XLRCompile.run([ + "-i", + "src", + "-o", + "dist", + "--config", + configPath, + ]); + + expect(readManifest(workspace).metaData).toStrictEqual({ + kind: "test", + }); + }); + + test("omits metaData when the config sets none", async () => { + writeFixture(workspace, { name: "@test/plugin", version: "2.3.4" }); + + await XLRCompile.run(["-i", "src", "-o", "dist"]); + + expect(readManifest(workspace).metaData).toBeUndefined(); + }); + + test("carries metaData into the manifest.js wrapper too", async () => { + writeFixture(workspace, { name: "@test/plugin", version: "2.3.4" }); + const configPath = path.join(workspace, "player.config.json"); + fs.writeFileSync( + configPath, + JSON.stringify({ xlr: { metaData: { kind: "test" } } }), + ); + + await XLRCompile.run([ + "-i", + "src", + "-o", + "dist", + "--config", + configPath, + ]); + + expect( + fs.readFileSync( + path.join(workspace, "dist", "xlr", "manifest.js"), + "utf-8", + ), + ).toContain('"metaData": {"kind":"test"}'); + }); + }); }); describe("bazel", () => { @@ -433,5 +491,57 @@ describe("xlr compile package info", () => { ).toBeUndefined(); }); }); + + describe("metaData", () => { + test("stamps XLR_META_DATA into the manifest", async () => { + writeFixture(path.join(workspace, pkgPath)); + process.env.XLR_META_DATA = JSON.stringify({ kind: "test" }); + + await XLRCompile.run([ + "-i", + path.join(pkgPath, "src"), + "-o", + path.join(pkgPath, "dist"), + ]); + + expect( + readManifest(path.join(workspace, pkgPath)).metaData, + ).toStrictEqual({ kind: "test" }); + }); + + test("omits metaData and warns when XLR_META_DATA is not valid JSON", async () => { + writeFixture(path.join(workspace, pkgPath)); + process.env.XLR_META_DATA = "{kind:test}"; + + await XLRCompile.run([ + "-i", + path.join(pkgPath, "src"), + "-o", + path.join(pkgPath, "dist"), + ]); + + expect( + readManifest(path.join(workspace, pkgPath)).metaData, + ).toBeUndefined(); + expect(warn).toHaveBeenCalledWith( + expect.stringContaining("XLR_META_DATA"), + ); + }); + + test("omits metaData when XLR_META_DATA is unset", async () => { + writeFixture(path.join(workspace, pkgPath)); + + await XLRCompile.run([ + "-i", + path.join(pkgPath, "src"), + "-o", + path.join(pkgPath, "dist"), + ]); + + expect( + readManifest(path.join(workspace, pkgPath)).metaData, + ).toBeUndefined(); + }); + }); }); }); diff --git a/cli/src/__tests__/commands/xlr-compile-test-helpers.ts b/cli/src/__tests__/commands/xlr-compile-test-helpers.ts deleted file mode 100644 index 9711dff..0000000 --- a/cli/src/__tests__/commands/xlr-compile-test-helpers.ts +++ /dev/null @@ -1,58 +0,0 @@ -import fs from "fs"; -import path from "path"; -import { vi } from "vitest"; -import { Errors } from "@oclif/core"; - -/** A plugin package with one asset, laid out the way `xlr compile` expects */ -export function writeFixture( - dir: string, - packageJson?: Record, -) { - fs.mkdirSync(path.join(dir, "src"), { recursive: true }); - - if (packageJson) { - fs.writeFileSync( - path.join(dir, "package.json"), - JSON.stringify(packageJson), - ); - } - - fs.writeFileSync( - path.join(dir, "src", "index.ts"), - ` -import type { ExtendedPlayerPlugin } from "@player-ui/player"; - -export interface TestAsset { - id: string; - type: "test"; -} - -export class TestPlugin implements ExtendedPlayerPlugin<[TestAsset]> { - name = "test-plugin"; -} -`, - ); -} - -/** Silences `Errors.warn` while capturing what it was called with */ -export function spyOnWarn() { - return vi.spyOn(Errors, "warn").mockImplementation(() => undefined); -} - -/** A player config file with `xlr.platformPackages` set inline */ -export function writePlayerConfig( - dir: string, - platformPackages: Record, -): string { - const configPath = path.join(dir, "player.config.json"); - - fs.writeFileSync(configPath, JSON.stringify({ xlr: { platformPackages } })); - - return configPath; -} - -export function readManifest(dir: string) { - return JSON.parse( - fs.readFileSync(path.join(dir, "dist", "xlr", "manifest.json"), "utf-8"), - ); -} diff --git a/cli/src/__tests__/commands/xlr-test-helpers.ts b/cli/src/__tests__/commands/xlr-test-helpers.ts new file mode 100644 index 0000000..277ed69 --- /dev/null +++ b/cli/src/__tests__/commands/xlr-test-helpers.ts @@ -0,0 +1,108 @@ +import fs from "fs"; +import path from "path"; +import { vi } from "vitest"; +import { Errors } from "@oclif/core"; + +/** A plugin package with one asset, laid out the way `xlr compile` expects */ +export function writeFixture( + dir: string, + packageJson?: Record, +) { + fs.mkdirSync(path.join(dir, "src"), { recursive: true }); + + if (packageJson) { + fs.writeFileSync( + path.join(dir, "package.json"), + JSON.stringify(packageJson), + ); + } + + fs.writeFileSync( + path.join(dir, "src", "index.ts"), + ` +import type { ExtendedPlayerPlugin } from "@player-ui/player"; + +export interface TestAsset { + id: string; + type: "test"; +} + +export class TestPlugin implements ExtendedPlayerPlugin<[TestAsset]> { + name = "test-plugin"; +} +`, + ); +} + +/** Silences `Errors.warn` while capturing what it was called with */ +export function spyOnWarn() { + return vi.spyOn(Errors, "warn").mockImplementation(() => undefined); +} + +/** A player config file with `xlr.platformPackages` set inline */ +export function writePlayerConfig( + dir: string, + platformPackages: Record, +): string { + const configPath = path.join(dir, "player.config.json"); + + fs.writeFileSync(configPath, JSON.stringify({ xlr: { platformPackages } })); + + return configPath; +} + +export function readManifest(dir: string) { + return JSON.parse( + fs.readFileSync(path.join(dir, "dist", "xlr", "manifest.json"), "utf-8"), + ); +} + +/** Writes a source package's compiled manifest + per-capability type files, for `xlr bundle` */ +export function writeSource( + dir: string, + options: { + manifestPath?: string; + packages?: Record; + metaData?: Record; + assets?: Array<{ capabilityName: string; typeName: string | undefined }>; + views?: Array<{ capabilityName: string; typeName: string | undefined }>; + }, +) { + const manifestPath = options.manifestPath ?? "dist/xlr/manifest.json"; + const manifestDir = path.join(dir, path.dirname(manifestPath)); + fs.mkdirSync(manifestDir, { recursive: true }); + + const assets = options.assets ?? []; + const views = options.views ?? []; + + [...assets, ...views].forEach(({ capabilityName, typeName }) => { + fs.writeFileSync( + path.join(manifestDir, `${capabilityName}.json`), + JSON.stringify( + typeName === undefined + ? { name: capabilityName } + : { extends: { genericArguments: [{ const: typeName }] } }, + ), + ); + }); + + fs.writeFileSync( + path.join(dir, manifestPath), + JSON.stringify({ + pluginName: path.basename(dir), + ...(options.packages ? { packages: options.packages } : {}), + ...(options.metaData ? { metaData: options.metaData } : {}), + capabilities: { + Assets: assets.map((a) => a.capabilityName), + Views: views.map((v) => v.capabilityName), + }, + }), + ); +} + +/** Reads the manifest `xlr bundle` wrote to `outputDir` */ +export function readBundledManifest(outputDir: string) { + return JSON.parse( + fs.readFileSync(path.join(outputDir, "xlr", "manifest.json"), "utf-8"), + ); +} diff --git a/cli/src/commands/xlr/bundle.ts b/cli/src/commands/xlr/bundle.ts new file mode 100644 index 0000000..ca8a996 --- /dev/null +++ b/cli/src/commands/xlr/bundle.ts @@ -0,0 +1,56 @@ +import path from "path"; +import { Flags } from "@oclif/core"; +import { BaseCommand } from "../../utils/base-command"; +import { bundleManifests, writeBundledManifest } from "../../utils/xlr/bundle"; + +/** + * Aggregates several packages' compiled XLR manifests into one collated manifest, keyed by + * type name instead of by source package, scoped to `Assets`/`Views` only. + */ +export default class XLRBundle extends BaseCommand { + static description = + "Collates several packages' compiled xlr manifests (Assets/Views only) into one manifest, keyed by type name"; + + static flags = { + ...BaseCommand.flags, + source: Flags.string({ + char: "s", + description: + "A package (resolved by name) or a directory whose compiled xlr manifest to include. Repeatable. Overrides config.xlr.bundleSources when given.", + multiple: true, + }), + manifestPath: Flags.string({ + description: + "Path to a source's manifest.json, relative to its resolved root.", + default: "dist/xlr/manifest.json", + }), + output: Flags.string({ + char: "o", + description: "Output directory to write the collated manifest to.", + default: "./dist", + }), + }; + + private async getOptions() { + const { flags } = await this.parse(XLRBundle); + const config = await this.getPlayerConfig(); + + return { + sources: flags.source ?? config.xlr?.bundleSources ?? [], + manifestPath: flags.manifestPath, + outputDir: path.join(flags.output, "xlr"), + }; + } + + async run(): Promise<{ + /** the status code */ + exitCode: number; + }> { + const { sources, manifestPath, outputDir } = await this.getOptions(); + + const manifest = bundleManifests(sources, manifestPath); + writeBundledManifest(outputDir, manifest); + + return { exitCode: 0 }; + } +} diff --git a/cli/src/commands/xlr/compile.ts b/cli/src/commands/xlr/compile.ts index e214a45..bb173b5 100644 --- a/cli/src/commands/xlr/compile.ts +++ b/cli/src/commands/xlr/compile.ts @@ -1,4 +1,4 @@ -import { Flags } from "@oclif/core"; +import { Errors, Flags } from "@oclif/core"; import ts from "typescript"; import fs from "fs"; import path from "path"; @@ -12,6 +12,27 @@ import { pluginVisitor, fileVisitor } from "../../utils/xlr/visitors"; import { Mode, customPrimitives } from "../../utils/xlr/consts"; import { getPackages } from "../../utils/xlr/packages"; +/** + * This package's `metaData`, from `XLR_META_DATA` under Bazel (where there's no config file to + * read) or from `config.xlr.metaData` everywhere else — the same split `getPackages` makes. + */ +function getMetaData( + configMetaData?: Record, +): Record | undefined { + const stamped = process.env.XLR_META_DATA; + + if (!stamped) { + return configMetaData; + } + + try { + return JSON.parse(stamped); + } catch { + Errors.warn("Could not parse XLR_META_DATA as JSON; omitting it."); + return configMetaData; + } +} + /** * Exports TS Interfaces/Types to XLR format */ @@ -52,6 +73,7 @@ export default class XLRCompile extends BaseCommand { outputDir: path.join(output, "xlr"), mode: modeValue === "plugin" ? Mode.PLUGIN : Mode.TYPES, platformPackages, + metaData: getMetaData(config.xlr?.metaData), }; } @@ -59,7 +81,7 @@ export default class XLRCompile extends BaseCommand { /** the status code */ exitCode: number; }> { - const { inputPath, outputDir, mode, platformPackages } = + const { inputPath, outputDir, mode, platformPackages, metaData } = await this.getOptions(); const inputFiles = globby.sync([ `${inputPath}/**/*.ts`, @@ -67,7 +89,7 @@ export default class XLRCompile extends BaseCommand { ]); const packages = getPackages(platformPackages); try { - this.processTypes(inputFiles, outputDir, {}, mode, packages); + this.processTypes(inputFiles, outputDir, {}, mode, packages, metaData); } catch (e: any) { console.log(""); console.log( @@ -96,6 +118,7 @@ export default class XLRCompile extends BaseCommand { options: ts.CompilerOptions, mode: Mode = Mode.PLUGIN, packages?: PlatformPackages, + metaData?: Record, ): void { // Build a program using the set of root file names in fileNames const program = ts.createProgram(fileNames, options); @@ -157,6 +180,7 @@ export default class XLRCompile extends BaseCommand { const manifest: Manifest = { ...capabilities, ...(packages ? { packages } : {}), + ...(metaData ? { metaData } : {}), }; // print out the manifest files @@ -175,6 +199,10 @@ export default class XLRCompile extends BaseCommand { manifest.packages ? `\n "packages": ${JSON.stringify(manifest.packages)},` : "" + }${ + manifest.metaData + ? `\n "metaData": ${JSON.stringify(manifest.metaData)},` + : "" } "capabilities": { ${[...(manifest.capabilities?.entries() ?? [])] diff --git a/cli/src/config.ts b/cli/src/config.ts index a1cfae4..4ee2c50 100644 --- a/cli/src/config.ts +++ b/cli/src/config.ts @@ -10,6 +10,9 @@ export interface PlayerConfigFileShape { /** A list of presets to apply */ presets?: Array; + + /** Options related to XLR compilation step */ + xlr?: PlayerConfigResolvedShape["xlr"]; } export interface PlayerConfigResolvedShape { @@ -51,6 +54,20 @@ export interface PlayerConfigResolvedShape { string, Pick >; + + /** + * Stamped into this package's own compiled manifest, and carried through by `xlr bundle` + * into every entry this package contributes. Only load-bearing once two packages provide + * the same type name, where it's what tells their entries apart. + */ + metaData?: Record; + + /** + * The packages (or directories) for `xlr bundle` to collate, as data instead of repeated + * `--source` flags — the list a decomposed DSL package depends on is expected to grow past + * what's reasonable to hand-type into a build script command. + */ + bundleSources?: Array; }; /** Flattened list of plugins */ diff --git a/cli/src/utils/xlr/bundle.ts b/cli/src/utils/xlr/bundle.ts new file mode 100644 index 0000000..a123cd6 --- /dev/null +++ b/cli/src/utils/xlr/bundle.ts @@ -0,0 +1,211 @@ +import fs from "fs"; +import path from "path"; +import { Errors } from "@oclif/core"; +import type { PlatformPackages } from "@xlr-lib/xlr"; + +/** A manifest as `xlr compile` writes it to disk: plain JSON, not the in-memory `Map` shape */ +interface SourceManifest { + pluginName?: string; + packages?: PlatformPackages; + metaData?: Record; + capabilities?: Record>; +} + +/** + * `DataTypes`/`Formatters`/`Validators`/`Expressions`/`Types` ship as one wholesale package a + * topic installs outright, never looked up by name, so `bundle` only walks these two. + */ +const BUNDLED_CAPABILITY_TYPES = ["Assets", "Views"] as const; + +export interface BundledManifestEntry extends PlatformPackages { + /** + * Lets a consumer pick between entries colliding on the same type name. Copied through from + * the source's own manifest, where its own `xlr compile` stamped it. + */ + metaData?: Record; +} + +export interface BundledManifest { + capabilities: Record>; +} + +/** The nearest ancestor directory of `file` containing a `package.json`, or undefined if none does */ +function findPackageRoot(file: string): string | undefined { + let dir = path.dirname(file); + + while (true) { + if (fs.existsSync(path.join(dir, "package.json"))) { + return dir; + } + + const parent = path.dirname(dir); + if (parent === dir) { + return undefined; + } + + dir = parent; + } +} + +/** + * Resolves a `--source` value to a directory: an installed/linked package name, or a literal + * path. Package resolution goes through the package's main/exports entry point rather than + * appending `/package.json` directly, since a package's `exports` map commonly doesn't whitelist + * that subpath. + */ +function resolveSourceDir(source: string): string | undefined { + try { + const entryFile = require.resolve(source, { paths: [process.cwd()] }); + const packageRoot = findPackageRoot(entryFile); + + if (packageRoot) { + return packageRoot; + } + } catch { + // fall through to path resolution below + } + + const asPath = path.resolve(source); + + if (fs.existsSync(asPath) && fs.statSync(asPath).isDirectory()) { + return asPath; + } + + Errors.warn( + `Could not resolve xlr bundle source "${source}" as a package or a directory; skipping it.`, + ); + return undefined; +} + +function readSourceManifest(manifestFile: string): SourceManifest | undefined { + try { + return JSON.parse(fs.readFileSync(manifestFile, "utf-8")); + } catch { + Errors.warn(`Could not read ${manifestFile}; skipping this source.`); + return undefined; + } +} + +/** + * The type name for a compiled XLR node (e.g. `input` for `Assets.InputAsset`), read from the + * per-capability `.json` file `xlr compile` writes next to `manifest.json`. This is not the same + * as the capability name in `manifest.json`'s `Assets`/`Views` list. + */ +function getTypeName(manifestDir: string, capabilityName: string): string { + const typeFilePath = path.join(manifestDir, `${capabilityName}.json`); + + let node: any; + try { + node = JSON.parse(fs.readFileSync(typeFilePath, "utf-8")); + } catch { + Errors.error(`Could not read ${typeFilePath}.`); + } + + const typeName = node?.extends?.genericArguments?.[0]?.const; + + if (typeof typeName !== "string" || !typeName) { + Errors.error( + `Could not determine a type name for "${capabilityName}" in ${manifestDir}. This asset's own compiled output is malformed.`, + ); + } + + return typeName; +} + +/** + * Appends `entry` to `list` unless an identical entry is already present. Two different + * plugins colliding on the same type name are never identical (different `packages` at least) — + * this only collapses the same plugin's own repeat under two capabilities, not a real collision. + */ +function appendUnlessIdentical( + list: Array, + entry: BundledManifestEntry, +) { + const serialized = JSON.stringify(entry); + + if (list.some((existing) => JSON.stringify(existing) === serialized)) { + return; + } + + list.push(entry); +} + +/** + * Flattens each source's `Assets`/`Views` capabilities into one manifest keyed by type name. A + * name produced by more than one source keeps every entry — resolving the collision is a + * topic-side concern, not `bundle`'s. + */ +export function bundleManifests( + sources: Array, + manifestPath: string, +): BundledManifest { + const capabilities: Record> = {}; + + sources.forEach((source) => { + const sourceDir = resolveSourceDir(source); + if (!sourceDir) { + return; + } + + const manifestFile = path.join(sourceDir, manifestPath); + const manifest = readSourceManifest(manifestFile); + if (!manifest) { + return; + } + + const manifestDir = path.dirname(manifestFile); + const entry: BundledManifestEntry = { + ...manifest.packages, + ...(manifest.metaData ? { metaData: manifest.metaData } : {}), + }; + + const hasPackageIdentity = Boolean( + manifest.packages?.react || + manifest.packages?.ios || + manifest.packages?.android, + ); + const willContributeEntries = BUNDLED_CAPABILITY_TYPES.some( + (capabilityType) => + (manifest.capabilities?.[capabilityType] ?? []).length > 0, + ); + + if (!hasPackageIdentity && willContributeEntries) { + Errors.warn( + `"${source}" has no package identity in its own manifest (its own "xlr compile" likely never ran, or ran without config.xlr.platformPackages) — the entries it contributes won't tell a topic what to install.`, + ); + } + + BUNDLED_CAPABILITY_TYPES.forEach((capabilityType) => { + const capabilityNames = manifest.capabilities?.[capabilityType] ?? []; + + capabilityNames.forEach((capabilityName) => { + const typeName = getTypeName(manifestDir, capabilityName); + + if (!capabilities[typeName]) { + capabilities[typeName] = []; + } + + appendUnlessIdentical(capabilities[typeName], entry); + }); + }); + }); + + return { capabilities }; +} + +export function writeBundledManifest( + outputDir: string, + manifest: BundledManifest, +) { + fs.mkdirSync(outputDir, { recursive: true }); + + fs.writeFileSync( + path.join(outputDir, "manifest.json"), + JSON.stringify(manifest, null, 4), + ); + + fs.writeFileSync( + path.join(outputDir, "manifest.js"), + `module.exports = require("./manifest.json");\n`, + ); +} diff --git a/package.json b/package.json index 1bfb56d..5137cb8 100644 --- a/package.json +++ b/package.json @@ -23,10 +23,10 @@ "@oclif/plugin-plugins": "^1.9.0", "@player-lang/react-dsl": "^1.0.1", "@player-lang/json-language-service": "^1.0.1", - "@xlr-lib/xlr": "1.1.0-next.0", - "@xlr-lib/xlr-converters": "1.1.0-next.0", - "@xlr-lib/xlr-sdk": "1.1.0-next.0", - "@xlr-lib/xlr-utils": "1.1.0-next.0", + "@xlr-lib/xlr": "1.1.0", + "@xlr-lib/xlr-converters": "1.1.0", + "@xlr-lib/xlr-sdk": "1.1.0", + "@xlr-lib/xlr-utils": "1.1.0", "@types/babel__register": "^7.17.0", "@types/fs-extra": "^9.0.13", "@types/mkdirp": "^1.0.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 43c17c6..f54d3fe 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -57,17 +57,17 @@ importers: specifier: ^1.0.1 version: 1.0.4 '@xlr-lib/xlr': - specifier: 1.1.0-next.0 - version: 1.1.0-next.0 + specifier: 1.1.0 + version: 1.1.0 '@xlr-lib/xlr-converters': - specifier: 1.1.0-next.0 - version: 1.1.0-next.0(jsonc-parser@2.3.1) + specifier: 1.1.0 + version: 1.1.0(jsonc-parser@2.3.1) '@xlr-lib/xlr-sdk': - specifier: 1.1.0-next.0 - version: 1.1.0-next.0 + specifier: 1.1.0 + version: 1.1.0 '@xlr-lib/xlr-utils': - specifier: 1.1.0-next.0 - version: 1.1.0-next.0(jsonc-parser@2.3.1) + specifier: 1.1.0 + version: 1.1.0(jsonc-parser@2.3.1) chalk: specifier: ^4.0.1 version: 4.1.2 @@ -2224,31 +2224,20 @@ packages: '@vitest/utils@2.1.9': resolution: {integrity: sha512-v0psaMSkNJ3A2NMrUEHFRzJtDPFn+/VWZ5WxImB21T9fjucJRmS7xCS3ppEnARb9y11OAzaD+P2Ps+b+BGX5iQ==, tarball: https://registry.npmjs.org/@vitest/utils/-/utils-2.1.9.tgz} - '@xlr-lib/xlr-converters@1.1.0-next.0': - resolution: {integrity: sha512-Petyxyt0WmEt8ule4qLh5XgGyW2RSzRTWGjy+xHv1qEBNYX6LdHOe96h9xjIw3fGAe2kpm1KYFpUwn/0ZIjipA==, tarball: https://registry.npmjs.org/@xlr-lib/xlr-converters/-/xlr-converters-1.1.0-next.0.tgz} + '@xlr-lib/xlr-converters@1.1.0': + resolution: {integrity: sha512-MCR51TigEljyrx3KHgBW2WxbmU6VM2QCoLzJZQ7I8Vkxdi1Rav3iPvPxe//JgCcA7agBRnVOa78+CRHA8/I56A==, tarball: https://registry.npmjs.org/@xlr-lib/xlr-converters/-/xlr-converters-1.1.0.tgz} hasBin: true - '@xlr-lib/xlr-sdk@1.0.0': - resolution: {integrity: sha512-3ge1NTzCukn9F76HQPZXeNZI/C/HOJF365II6m5GbzhR1Aag9Ghf4/5w5/PIrfb734eZ5kFkMiF6UeMOXUegtQ==, tarball: https://registry.npmjs.org/@xlr-lib/xlr-sdk/-/xlr-sdk-1.0.0.tgz} + '@xlr-lib/xlr-sdk@1.1.0': + resolution: {integrity: sha512-24yqN1e6L/RBT8hurrnRVMu7y948u9TPPiS5vbM4JswYpExa9225tABdSNM17anHzSyfGdavDuKTzS9SdvESBA==, tarball: https://registry.npmjs.org/@xlr-lib/xlr-sdk/-/xlr-sdk-1.1.0.tgz} - '@xlr-lib/xlr-sdk@1.1.0-next.0': - resolution: {integrity: sha512-TG0pHb58/IK/QYh0LNw6OK4/NO4y9hsk6sEw0Z7/PJcIigF6SpevqT2g4vqYC6VnrveGoRcpNFB3fMAw2gJBRA==, tarball: https://registry.npmjs.org/@xlr-lib/xlr-sdk/-/xlr-sdk-1.1.0-next.0.tgz} - - '@xlr-lib/xlr-utils@1.0.0': - resolution: {integrity: sha512-swoRx3QppioaClwvf+89yJrPJ8oEsIJNWUdney7Vom9XmUKbVY4XgPF0fZSYSC+HWsvB8NbkQ5CNCE8AXW6oxQ==, tarball: https://registry.npmjs.org/@xlr-lib/xlr-utils/-/xlr-utils-1.0.0.tgz} + '@xlr-lib/xlr-utils@1.1.0': + resolution: {integrity: sha512-sZo0zramS6JEkxCeAZW21bh9hgshu/9lTX2z8ZsidVnk1JVrMbetrAS6He7xlj1N4E9/or9nV96DJQPZ6DKQCA==, tarball: https://registry.npmjs.org/@xlr-lib/xlr-utils/-/xlr-utils-1.1.0.tgz} peerDependencies: jsonc-parser: 2.3.1 - '@xlr-lib/xlr-utils@1.1.0-next.0': - resolution: {integrity: sha512-eT9KEwQQLdraJdVDY7lq1YnzQY5kYMJES7EUZtFH2Ge0XkRf9K5kyAGO/BI5x7wGkfPmKtPmaRrXc5IbZA/low==, tarball: https://registry.npmjs.org/@xlr-lib/xlr-utils/-/xlr-utils-1.1.0-next.0.tgz} - peerDependencies: - jsonc-parser: 2.3.1 - - '@xlr-lib/xlr@1.0.0': - resolution: {integrity: sha512-Zfu6/lLauRQafuUo+JvbKZ2lmQkxtDgJxnGtWieGNct6mSHhWKWRc7siXQlboBxwu84yMcjLSrOxvD9WrBtLWQ==, tarball: https://registry.npmjs.org/@xlr-lib/xlr/-/xlr-1.0.0.tgz} - - '@xlr-lib/xlr@1.1.0-next.0': - resolution: {integrity: sha512-ZfiMna7EekGVMriUUIUsMPLmpwlHC4GFpbW4pZ4XHm2lAOwC/obA6/29Bt7InlBPrrImqLF50wfmXE1jz5TWZA==, tarball: https://registry.npmjs.org/@xlr-lib/xlr/-/xlr-1.1.0-next.0.tgz} + '@xlr-lib/xlr@1.1.0': + resolution: {integrity: sha512-LRBV/e7xjasLEBX8c0OItLZf/sr5ggIjbQTRst5ecZo/j9qGruVeqaBXUPTLlYLOH1P9UJpIegWHqZs+aA9kzg==, tarball: https://registry.npmjs.org/@xlr-lib/xlr/-/xlr-1.1.0.tgz} '@yarnpkg/lockfile@1.1.0': resolution: {integrity: sha512-GpSwvyXOcOOlV70vbnzjj4fW5xW/FdUF6nQEt1ENy7m4ZCczi1+/buVUPAqmGfqznsORNFzUMjctTIp8a9tuCQ==, tarball: https://registry.npmjs.org/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz} @@ -7180,9 +7169,9 @@ snapshots: '@player-lang/json-language-service@1.0.1': dependencies: '@player-ui/player': 0.15.0 - '@xlr-lib/xlr': 1.0.0 - '@xlr-lib/xlr-sdk': 1.0.0 - '@xlr-lib/xlr-utils': 1.0.0(jsonc-parser@2.3.1) + '@xlr-lib/xlr': 1.1.0 + '@xlr-lib/xlr-sdk': 1.1.0 + '@xlr-lib/xlr-utils': 1.1.0(jsonc-parser@2.3.1) detect-indent: 6.1.0 jsonc-parser: 2.3.1 tapable-ts: 0.2.4 @@ -7902,52 +7891,32 @@ snapshots: loupe: 3.2.1 tinyrainbow: 1.2.0 - '@xlr-lib/xlr-converters@1.1.0-next.0(jsonc-parser@2.3.1)': + '@xlr-lib/xlr-converters@1.1.0(jsonc-parser@2.3.1)': dependencies: - '@xlr-lib/xlr': 1.1.0-next.0 - '@xlr-lib/xlr-utils': 1.1.0-next.0(jsonc-parser@2.3.1) + '@xlr-lib/xlr': 1.1.0 + '@xlr-lib/xlr-utils': 1.1.0(jsonc-parser@2.3.1) tslib: 2.8.1 typescript: 5.5.4 transitivePeerDependencies: - jsonc-parser - '@xlr-lib/xlr-sdk@1.0.0': + '@xlr-lib/xlr-sdk@1.1.0': dependencies: '@types/fs-extra': 9.0.13 '@types/node': 18.19.130 - '@xlr-lib/xlr': 1.0.0 - '@xlr-lib/xlr-utils': 1.0.0(jsonc-parser@2.3.1) + '@xlr-lib/xlr': 1.1.0 + '@xlr-lib/xlr-utils': 1.1.0(jsonc-parser@2.3.1) fs-extra: 10.1.0 jsonc-parser: 2.3.1 tslib: 2.8.1 - '@xlr-lib/xlr-sdk@1.1.0-next.0': + '@xlr-lib/xlr-utils@1.1.0(jsonc-parser@2.3.1)': dependencies: - '@types/fs-extra': 9.0.13 - '@types/node': 18.19.130 - '@xlr-lib/xlr': 1.1.0-next.0 - '@xlr-lib/xlr-utils': 1.1.0-next.0(jsonc-parser@2.3.1) - fs-extra: 10.1.0 + '@xlr-lib/xlr': 1.1.0 jsonc-parser: 2.3.1 tslib: 2.8.1 - '@xlr-lib/xlr-utils@1.0.0(jsonc-parser@2.3.1)': - dependencies: - '@xlr-lib/xlr': 1.0.0 - jsonc-parser: 2.3.1 - tslib: 2.8.1 - - '@xlr-lib/xlr-utils@1.1.0-next.0(jsonc-parser@2.3.1)': - dependencies: - '@xlr-lib/xlr': 1.1.0-next.0 - jsonc-parser: 2.3.1 - tslib: 2.8.1 - - '@xlr-lib/xlr@1.0.0': - dependencies: - tslib: 2.8.1 - - '@xlr-lib/xlr@1.1.0-next.0': + '@xlr-lib/xlr@1.1.0': dependencies: tslib: 2.8.1