1
0
silverbullet/plugs/plugmd/plugmd.ts

126 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-10-14 13:11:33 +00:00
import { collectNodesOfType, findNodeOfType } from "$sb/lib/tree.ts";
2022-04-26 17:04:36 +00:00
import {
2022-10-14 13:11:33 +00:00
editor,
markdown,
space,
system,
} from "$sb/silverbullet-syscall/mod.ts";
import { syscall } from "$sb/plugos-syscall/mod.ts";
import * as YAML from "yaml";
2022-04-26 18:31:31 +00:00
import type { Manifest } from "../../common/manifest.ts";
2022-04-26 18:31:31 +00:00
2022-04-26 17:04:36 +00:00
export async function compileCommand() {
2022-10-14 13:11:33 +00:00
const text = await editor.getText();
2022-04-26 17:04:36 +00:00
try {
2022-10-14 13:11:33 +00:00
const manifest = await compileDefinition(text);
await space.writePage(
2022-04-26 17:04:36 +00:00
`_plug/${manifest.name}`,
JSON.stringify(manifest, null, 2),
2022-04-26 17:04:36 +00:00
);
console.log("Wrote this plug", manifest);
2022-10-14 13:11:33 +00:00
await editor.hidePanel("bhs");
2022-05-13 12:36:26 +00:00
2022-10-14 13:11:33 +00:00
system.reloadPlugs();
} catch (e: any) {
2022-10-14 13:11:33 +00:00
await editor.showPanel("bhs", 1, e.message);
// console.error("Got this error from compiler", e.message);
}
}
export async function checkCommand() {
2022-10-14 13:11:33 +00:00
const text = await editor.getText();
try {
await compileDefinition(text);
2022-10-14 13:11:33 +00:00
await editor.hidePanel("bhs");
system.reloadPlugs();
2022-04-26 17:04:36 +00:00
} catch (e: any) {
2022-10-14 13:11:33 +00:00
await editor.showPanel("bhs", 1, e.message);
2022-04-26 17:04:36 +00:00
// console.error("Got this error from compiler", e.message);
}
}
async function compileDefinition(text: string): Promise<Manifest> {
2022-10-14 13:11:33 +00:00
const tree = await markdown.parseMarkdown(text);
2022-04-26 17:04:36 +00:00
2022-10-14 13:11:33 +00:00
const codeNodes = collectNodesOfType(tree, "FencedCode");
2022-05-11 09:49:27 +00:00
let manifest: Manifest | undefined;
let code: string | undefined;
let language = "js";
2022-10-14 13:11:33 +00:00
for (const codeNode of codeNodes) {
const codeInfo = findNodeOfType(codeNode, "CodeInfo")!.children![0].text!;
const codeText = findNodeOfType(codeNode, "CodeText")!.children![0].text!;
2022-05-11 09:49:27 +00:00
if (codeInfo === "yaml") {
2022-10-14 13:11:33 +00:00
manifest = YAML.parse(codeText) as Manifest;
2022-04-26 17:04:36 +00:00
continue;
}
2022-05-11 09:49:27 +00:00
if (codeInfo === "typescript" || codeInfo === "ts") {
language = "ts";
2022-04-26 17:04:36 +00:00
}
2022-05-11 09:49:27 +00:00
code = codeText;
}
if (!manifest) {
throw new Error("No meta found");
}
if (!code) {
throw new Error("No code found");
2022-04-26 17:04:36 +00:00
}
2022-05-11 09:49:27 +00:00
2022-05-13 15:05:52 +00:00
manifest.dependencies = manifest.dependencies || {};
2022-10-14 13:11:33 +00:00
for (const [dep, depSpec] of Object.entries(manifest.dependencies)) {
const compiled = await system.invokeFunction(
"server",
"compileModule",
depSpec,
);
2022-05-13 15:05:52 +00:00
manifest.dependencies![dep] = compiled;
}
2022-05-11 09:49:27 +00:00
manifest.functions = manifest.functions || {};
2022-10-14 13:11:33 +00:00
for (const [name, func] of Object.entries(manifest.functions)) {
const compiled = await system.invokeFunction(
2022-05-11 09:49:27 +00:00
"server",
"compileJS",
`file.${language}`,
code,
2022-05-13 15:05:52 +00:00
name,
Object.keys(manifest.dependencies),
2022-05-11 09:49:27 +00:00
);
func.code = compiled;
}
console.log("Doing the whole manifest thing");
2022-04-26 17:04:36 +00:00
return manifest;
}
2022-10-14 13:11:33 +00:00
export function compileJS(
2022-04-26 17:04:36 +00:00
filename: string,
2022-05-11 09:49:27 +00:00
code: string,
2022-05-13 15:05:52 +00:00
functionName: string,
excludeModules: string[],
2022-04-26 17:04:36 +00:00
): Promise<string> {
2022-05-13 15:05:52 +00:00
// console.log("Compiling JS", filename, excludeModules);
2022-10-14 13:11:33 +00:00
return syscall(
2022-05-13 15:05:52 +00:00
"esbuild.compile",
filename,
code,
functionName,
excludeModules,
2022-05-13 15:05:52 +00:00
);
}
2022-10-14 13:11:33 +00:00
export function compileModule(moduleName: string): Promise<string> {
return syscall("esbuild.compileModule", moduleName);
2022-04-26 17:04:36 +00:00
}
export async function getPlugPlugMd(pageName: string): Promise<Manifest> {
2022-10-14 13:11:33 +00:00
const text = await space.readPage(pageName);
2022-04-26 18:31:31 +00:00
console.log("Compiling", pageName);
2022-04-26 17:04:36 +00:00
return compileDefinition(text);
}