1
0

Plug manger rebuild

This commit is contained in:
Zef Hemel 2022-05-11 11:49:27 +02:00
parent f6758dbbaf
commit f9ddb49ec6
7 changed files with 57 additions and 62 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -4,22 +4,26 @@ import path from "path";
export async function compile(
filePath: string,
functionName: string = "",
functionName: string | undefined = undefined,
debug: boolean = false,
excludeModules: string[] = [],
meta = false
): Promise<string> {
let outFile = path.join(path.dirname(filePath), "_out.tmp");
let outFile = path.resolve(path.dirname(filePath), "_out.tmp");
let inFile = filePath;
if (functionName) {
// Generate a new file importing just this one function and exporting it
inFile = "_in.ts";
inFile = path.resolve(path.dirname(filePath), "_in.ts");
await writeFile(
inFile,
`import {${functionName}} from "./${filePath}";export default ${functionName};`
`import {${functionName}} from "./${path.basename(
filePath
)}";export default ${functionName};`
);
}
// console.log("In:", inFile);
// console.log("Outfile:", outFile);
// TODO: Figure out how to make source maps work correctly with eval() code
let result = await esbuild.build({

View File

@ -93,7 +93,8 @@ self.addEventListener("message", (event: { data: WorkerMessage }) => {
id: data.id,
error: e.message,
});
throw e;
console.error("Error invoking function", data.name, e.message);
// throw e;
}
break;

View File

@ -15,7 +15,8 @@ export function esbuildSyscalls(): SysCallMapping {
"esbuild.compile": async (
ctx,
filename: string,
code: string
code: string,
functionName?: string
): Promise<string> => {
let tmpDir = `${tmpdir()}/plugos-${Math.random()}`;
await mkdir(tmpDir, { recursive: true });
@ -34,7 +35,9 @@ export function esbuildSyscalls(): SysCallMapping {
}
await writeFile(`${tmpDir}/${filename}`, code);
let jsCode = await compile(`${tmpDir}/${filename}`, "", false, ["yaml"]);
let jsCode = await compile(`${tmpDir}/${filename}`, functionName, false, [
"yaml",
]);
await rm(tmpDir, { recursive: true });
return jsCode;
},

View File

@ -58,69 +58,55 @@ export async function checkCommand() {
async function compileDefinition(text: string): Promise<Manifest> {
let tree = await parseMarkdown(text);
let pageMeta = extractMeta(tree);
if (!pageMeta.name) {
throw new Error("No 'name' specified in page meta");
}
addParentPointers(tree);
let allHeaders = collectNodesOfType(tree, "ATXHeading2");
let manifest: Manifest = {
name: pageMeta.name,
functions: {},
};
for (let t of allHeaders) {
let parent = t.parent!;
let headerIdx = parent.children!.indexOf(t);
let headerTitle = t.children![1].text!.trim();
if (!headerTitle.startsWith("function ")) {
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);
continue;
}
let functionName = headerTitle
.substring("function ".length)
.replace(/[^\w]/g, "_");
let meta: any;
let code: string | undefined;
let language = "js";
for (let i = headerIdx + 1; i < parent.children!.length; i++) {
let child = parent.children![i];
if (child.type === "FencedCode") {
let codeInfo = findNodeOfType(child, "CodeInfo")!.children![0].text!;
let codeText = findNodeOfType(child, "CodeText")!.children![0].text!;
if (codeInfo === "yaml") {
meta = YAML.parse(codeText);
continue;
}
if (codeInfo === "typescript" || codeInfo === "ts") {
language = "ts";
}
code = codeText;
}
if (child.type?.startsWith("ATXHeading")) {
break;
}
}
if (code) {
let compiled = await invokeFunction(
"server",
"compileJS",
`file.${language}`,
code
);
manifest.functions[functionName] = meta;
manifest.functions[functionName].code = compiled;
if (codeInfo === "typescript" || codeInfo === "ts") {
language = "ts";
}
code = codeText;
}
if (!manifest) {
throw new Error("No meta found");
}
if (!code) {
throw new Error("No code found");
}
manifest.functions = manifest.functions || {};
for (let [name, func] of Object.entries(manifest.functions)) {
let compiled = await invokeFunction(
"server",
"compileJS",
`file.${language}`,
code,
name
);
func.code = compiled;
}
console.log("Doing the whole manifest thing");
return manifest;
}
export async function compileJS(
filename: string,
code: string
code: string,
functionName: string
): Promise<string> {
return self.syscall("esbuild.compile", filename, code);
return self.syscall("esbuild.compile", filename, code, functionName);
}
async function listPlugs(): Promise<string[]> {
@ -151,7 +137,7 @@ export async function updatePlugs() {
}
let plugYaml = codeTextNode.children![0].text;
let plugList = YAML.parse(plugYaml!);
// console.log("Plug YAML", plugList);
console.log("Plug YAML", plugList);
let allPlugNames: string[] = [];
for (let plugUri of plugList) {
let [protocol, ...rest] = plugUri.split(":");

1
packages/server/_in.ts Normal file
View File

@ -0,0 +1 @@
import {pullDataCommand} from ".//var/folders/s2/4nqrw2192hngtxg672qzc0nr0000gn/T/plugos-0.8739407042390945/file.js";export default pullDataCommand;

View File

@ -94,7 +94,7 @@ export class ExpressServer {
sandboxSyscalls(this.system),
jwtSyscalls()
);
this.system.addHook(new EndpointHook(this.app, "/_/"));
this.system.addHook(new EndpointHook(this.app, "/_"));
this.eventHook.addLocalListener(
"get-plug:builtin",