// The first-party plugins as the SDK carries them: `plugins/gen//` of its source tree — the // contract.d.ts and the app's wrapper (sdk/), copies of the plugin repo's (core's // scripts/vendor-plugins.ts). A project's HALF FOLDER (`implements` in its manifest) implements one // of these contracts: the one of the SDK this CLI compiles with, so the glue `lecodes plugin gen` // writes for the half and the wrapper in the bundle are of one contract. import { existsSync, readFileSync, readdirSync } from "node:fs" import { join } from "node:path" import { sdkSourceDir } from "../compile/projectCompile" const ID = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/ /** The SDK's copy of a first-party plugin's contract, or null when the SDK carries no such plugin. */ export const firstPartyContractFile = (id: string): string | null => { if (!sdkSourceDir || !ID.test(id)) return null const file = join(sdkSourceDir, "plugins", "gen", id, "contract.d.ts") return existsSync(file) ? file : null } /** The first-party plugins the SDK carries, by id. */ export const firstPartyIds = (): string[] => { if (!sdkSourceDir) return [] const gen = join(sdkSourceDir, "plugins", "gen") return existsSync(gen) ? readdirSync(gen).filter((id) => existsSync(join(gen, id, "contract.d.ts"))).sort() : [] } /** A copy's text as the plugin wrote it: the vendoring points the import of the SDK at its own file * (`../../../plugin`), a contract reads `lecodes-sdk/plugin` and nothing else. */ export const firstPartyContractText = (file: string): string => readFileSync(file, "utf8").replace(/from "(?:\.\.\/)+plugin"/g, `from "lecodes-sdk/plugin"`)