2022-10-10 12:50:21 +00:00
|
|
|
import { collectNodesOfType, findNodeOfType } from "../../common/tree.ts";
|
|
|
|
import { getText, hideBhs, showBhs } from "$sb/silverbullet-syscall/editor.ts";
|
|
|
|
import { parseMarkdown } from "$sb/silverbullet-syscall/markdown.ts";
|
|
|
|
import { readPage, writePage } from "$sb/silverbullet-syscall/space.ts";
|
2022-04-26 17:04:36 +00:00
|
|
|
import {
|
|
|
|
invokeFunction,
|
|
|
|
reloadPlugs,
|
2022-10-10 12:50:21 +00:00
|
|
|
} from "$sb/silverbullet-syscall/system.ts";
|
|
|
|
import * as YAML from "yaml";
|
2022-04-26 18:31:31 +00:00
|
|
|
|
2022-10-10 12:50:21 +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() {
|
|
|
|
let text = await getText();
|
|
|
|
try {
|
|
|
|
let manifest = await compileDefinition(text);
|
|
|
|
await writePage(
|
|
|
|
`_plug/${manifest.name}`,
|
2022-10-10 12:50:21 +00:00
|
|
|
JSON.stringify(manifest, null, 2),
|
2022-04-26 17:04:36 +00:00
|
|
|
);
|
|
|
|
console.log("Wrote this plug", manifest);
|
|
|
|
await hideBhs();
|
2022-05-13 12:36:26 +00:00
|
|
|
|
2022-04-28 09:55:38 +00:00
|
|
|
await reloadPlugs();
|
|
|
|
} catch (e: any) {
|
|
|
|
await showBhs(e.message);
|
|
|
|
// console.error("Got this error from compiler", e.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function checkCommand() {
|
|
|
|
let text = await getText();
|
|
|
|
try {
|
|
|
|
await compileDefinition(text);
|
|
|
|
await hideBhs();
|
2022-04-26 17:04:36 +00:00
|
|
|
reloadPlugs();
|
|
|
|
} catch (e: any) {
|
|
|
|
await showBhs(e.message);
|
|
|
|
// console.error("Got this error from compiler", e.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function compileDefinition(text: string): Promise<Manifest> {
|
|
|
|
let tree = await parseMarkdown(text);
|
|
|
|
|
2022-05-11 09:49:27 +00:00
|
|
|
let codeNodes = collectNodesOfType(tree, "FencedCode");
|
|
|
|
let manifest: Manifest | undefined;
|
|
|
|
let code: string | undefined;
|
|
|
|
let language = "js";
|
|
|
|
for (let codeNode of codeNodes) {
|
|
|
|
let codeInfo = findNodeOfType(codeNode, "CodeInfo")!.children![0].text!;
|
|
|
|
let codeText = findNodeOfType(codeNode, "CodeText")!.children![0].text!;
|
|
|
|
if (codeInfo === "yaml") {
|
|
|
|
manifest = YAML.parse(codeText);
|
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 || {};
|
|
|
|
|
|
|
|
for (let [dep, depSpec] of Object.entries(manifest.dependencies)) {
|
|
|
|
let compiled = await invokeFunction("server", "compileModule", depSpec);
|
|
|
|
manifest.dependencies![dep] = compiled;
|
|
|
|
}
|
|
|
|
|
2022-05-11 09:49:27 +00:00
|
|
|
manifest.functions = manifest.functions || {};
|
|
|
|
|
|
|
|
for (let [name, func] of Object.entries(manifest.functions)) {
|
|
|
|
let compiled = await invokeFunction(
|
|
|
|
"server",
|
|
|
|
"compileJS",
|
|
|
|
`file.${language}`,
|
|
|
|
code,
|
2022-05-13 15:05:52 +00:00
|
|
|
name,
|
2022-10-10 12:50:21 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function compileJS(
|
|
|
|
filename: string,
|
2022-05-11 09:49:27 +00:00
|
|
|
code: string,
|
2022-05-13 15:05:52 +00:00
|
|
|
functionName: string,
|
2022-10-10 12:50:21 +00:00
|
|
|
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);
|
|
|
|
return self.syscall(
|
|
|
|
"esbuild.compile",
|
|
|
|
filename,
|
|
|
|
code,
|
|
|
|
functionName,
|
2022-10-10 12:50:21 +00:00
|
|
|
excludeModules,
|
2022-05-13 15:05:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function compileModule(moduleName: string): Promise<string> {
|
|
|
|
return self.syscall("esbuild.compileModule", moduleName);
|
2022-04-26 17:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getPlugPlugMd(pageName: string): Promise<Manifest> {
|
|
|
|
let { text } = await 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);
|
|
|
|
}
|