diff --git a/examples/package.ts b/examples/package.ts index c2b1cb3..2a9fe66 100755 --- a/examples/package.ts +++ b/examples/package.ts @@ -1,6 +1,6 @@ #!/usr/bin/env -S npx ts-node --transpileOnly -import { Substrate, Box, Package, sb } from "substrate"; +import { Substrate, Box, Module, sb } from "substrate"; async function main() { const SUBSTRATE_API_KEY = process.env["SUBSTRATE_API_KEY"]; @@ -17,38 +17,44 @@ async function main() { const y = sb.input({ type: "string" }); const z = sb.input({ type: "object", properties: {} }); - const a = new Box({ value: { a: x, z: z, liam: [x, x, x] } }, { id: "BoxA" }); + const a = new Box({ value: { a: x, z: z, liam: [x, x, x] } }, { id: "A" }); const b = new Box( { value: { b: sb.interpolate`x=${a.future.value.get("a")}, y=${y}` } }, - { id: "BoxB" }, + { id: "B" }, ); - const packageable = substrate.createPackageable([a, b], { x, y, z }); - // console.log("packageable", JSON.stringify(packageable, null, 2)); - - const p = new Package( + // const publication = await substrate.module.publish({ + // name: "my reusable graph", + // nodes: [a, b], + // inputs: { x, y, z }, + // }); + + const p = new Module( { - package_json: packageable.toJSON(), + json: substrate.module.serialize({ + nodes: [a, b], + inputs: { x, y, z }, + }), inputs: { + // when commented will use "hello" because it is defined as the default above // x: "xxx", y: "yyy", z: { - // string: "hi", - // obj: { a: 123 }, arr: ["123"], }, }, - }, { id: "Package" } + }, + { id: "Module" }, ); const c = new Box( { value: { - "1": p.future.get("BoxB.value.b"), - "2": p.future.get("BoxA.value.z.arr[0]"), + "1": p.future.get("A.value.z.arr[0]"), + "2": p.future.get("B.value.b"), }, }, - { id: "BoxC" }, + { id: "C" }, ); const res = await substrate.run(p, c); diff --git a/src/Package.ts b/src/Package.ts index aced714..551aaf6 100644 --- a/src/Package.ts +++ b/src/Package.ts @@ -66,14 +66,35 @@ export function packager(substrate: Substrate) { }; } -type PackageIn = { - package_json: any; - inputs: Record; -}; +type PackageIn = + | { + package_json: any; + inputs: Record; + } + | { + module_json: any; + inputs: Record; + } + | { + id: any; + inputs: Record; + } + | { + uri: any; + inputs: Record; + } + | { + json: any; + + inputs: Record; + }; export class Package extends Node { constructor(args: PackageIn, options?: Options) { - super(args, options); + // @ts-ignore + let myargs = { package_json: args.json || args.package_json || args.module_json, inputs: args.inputs }; + + super(myargs, options); this.node = "Package"; } } diff --git a/src/Substrate.ts b/src/Substrate.ts index 6739872..91708df 100644 --- a/src/Substrate.ts +++ b/src/Substrate.ts @@ -294,4 +294,16 @@ export class Substrate { return headers; } + + module = { + serialize: ({ nodes, inputs }: { nodes: Node[], inputs: any }) => { + let p = packager(this); + return p(nodes, inputs).toJSON(); + }, + + publish: async (publishable: any) => { + console.log("not implemented yet"); + return publishable; + } + } } diff --git a/src/index.ts b/src/index.ts index ab6a158..11a789c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,7 +51,7 @@ export { DeleteVectors, } from "substrate/Nodes"; -export { Package } from "substrate/Package"; +export { Package, Package as Module } from "substrate/Package"; export { sb } from "substrate/sb"; export { Substrate };