Skip to content
Merged
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
69 changes: 46 additions & 23 deletions typescript/packages/common-cli/main.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,57 @@
// Load .env file
import { CharmManager, createStorage } from "@commontools/charm";
import { fetchInboxEmails } from "./gmail.ts";
import { parse } from "https://deno.land/std/flags/mod.ts";
import { CharmManager, storage, compileRecipe } from "@commontools/charm";
import { getEntityId, isStream } from "@commontools/runner";

const { space, charmId, recipeFile, cause } = parse(Deno.args);

storage.setRemoteStorage(
new URL(process?.env?.TOOLSHED_API_URL ?? "https://toolshed.saga-castor.ts.net/"),
);

const replica = "anotherjesse-test5";
const charmId = "baedreihwuw4dbkvcel76siqztlxvloddfahgu535yupgxvkh5ml3wtqgqu";
async function main() {
const storage = createStorage({
type: "remote",
replica,
url: new URL("https://toolshed.saga-castor.ts.net/"),
});
const manager = new CharmManager(storage);
const manager = new CharmManager(space ?? "common-cli");
const charms = await manager.getCharms();

await new Promise((resolve) => {
charms.sink((charms) => {
if (charms.length > 0) {
charms.forEach((charm) => {
manager.get(charm.cell.entityId["/"]);
});
resolve(undefined);
}
});
charms.sink((charms) => {
console.log(
"charms:",
charms.map((c) => c.toJSON().cell["/"]),
);
});

const charm = await charms.get(charmId);
console.log({ charm });
if (charmId) {
const charm = await manager.get(charmId);
charm?.sink((value) => {
console.log("charm:", charmId, value);
});
}

if (recipeFile) {
try {
const recipeSrc = await Deno.readTextFile(recipeFile);
const recipe = await compileRecipe(recipeSrc, "recipe", []);
const charm = await manager.runPersistent(recipe, undefined, cause);
await manager.syncRecipe(charm);
manager.add([charm]);
const charmWithSchema = await manager.get(charm);
charmWithSchema.sink((value) => {
console.log("running charm:", getEntityId(charm), value);
});
const updater = charmWithSchema.get()?.updater;
if (isStream(updater)) {
console.log("running updater");
updater.send({ newValues: ["test"] });
}
} catch (error) {
console.error("Error loading and compiling recipe:", error);
}
}

const emails = await fetchInboxEmails();
console.log({ emails });
return new Promise(() => {
// This promise never resolves, keeping the program alive
console.log("Program running. Press Ctrl+C to exit.");
});
}

main();
55 changes: 55 additions & 0 deletions typescript/packages/common-cli/recipes/simpleValue.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { h } from "@commontools/html";
import { recipe, handler, UI, NAME, cell, derive, JSONSchema } from "@commontools/builder";

const inputSchema: JSONSchema = {
type: "object",
properties: {
values: { type: "array", items: { type: "string" } },
},
};

const outputSchema: JSONSchema = {
type: "object",
properties: {
values: { type: "array", items: { type: "string" } },
updater: { asCell: true, type: "action" },
},
};

const updater = handler<{ newValues: string[] }, { values: string[] }>((event, state) => {
if (!state.values) state.values = [];
console.log("updating values", event);
event?.newValues?.forEach((value) => {
console.log("adding value", value);
state.values.push(value);
});
});

const adder = handler<{}, { values: string[] }>((_, state) => {
console.log("adding a value");
if (!state.values) state.values = [];
state.values.push(Math.random().toString(36).substring(2, 15));
});

export default recipe(inputSchema, outputSchema, ({ values }) => {
/*derive(values, (values) => {
console.log("values#", values.length);
});*/
return {
[NAME]: "Simple Value",
[UI]: (
<div>
<button onclick={adder({ values })}>Add Value</button>
<div>
{values.map((value, index) => (
<div>
{index}: {value}
</div>
))}
</div>
</div>
),
updater: updater({ values }),
values,
};
});
Loading