diff --git a/.DS_Store b/.DS_Store index eb0ea6a6..826bfb4a 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/packages/plugos/compile.ts b/packages/plugos/compile.ts index dbc2ad48..11c5679b 100644 --- a/packages/plugos/compile.ts +++ b/packages/plugos/compile.ts @@ -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 { - 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({ diff --git a/packages/plugos/environments/sandbox_worker.ts b/packages/plugos/environments/sandbox_worker.ts index 0f365a58..cca927a7 100644 --- a/packages/plugos/environments/sandbox_worker.ts +++ b/packages/plugos/environments/sandbox_worker.ts @@ -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; diff --git a/packages/plugos/syscalls/esbuild.ts b/packages/plugos/syscalls/esbuild.ts index f0f0888c..7dff6e86 100644 --- a/packages/plugos/syscalls/esbuild.ts +++ b/packages/plugos/syscalls/esbuild.ts @@ -15,7 +15,8 @@ export function esbuildSyscalls(): SysCallMapping { "esbuild.compile": async ( ctx, filename: string, - code: string + code: string, + functionName?: string ): Promise => { 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; }, diff --git a/packages/plugs/plugmanager/plugmanager.ts b/packages/plugs/plugmanager/plugmanager.ts index fdd563b5..acd5f471 100644 --- a/packages/plugs/plugmanager/plugmanager.ts +++ b/packages/plugs/plugmanager/plugmanager.ts @@ -58,69 +58,55 @@ export async function checkCommand() { async function compileDefinition(text: string): Promise { 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 { - return self.syscall("esbuild.compile", filename, code); + return self.syscall("esbuild.compile", filename, code, functionName); } async function listPlugs(): Promise { @@ -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(":"); diff --git a/packages/server/_in.ts b/packages/server/_in.ts new file mode 100644 index 00000000..0f3dbd3a --- /dev/null +++ b/packages/server/_in.ts @@ -0,0 +1 @@ +import {pullDataCommand} from ".//var/folders/s2/4nqrw2192hngtxg672qzc0nr0000gn/T/plugos-0.8739407042390945/file.js";export default pullDataCommand; \ No newline at end of file diff --git a/packages/server/express_server.ts b/packages/server/express_server.ts index 127dc3a4..83ae9ed3 100644 --- a/packages/server/express_server.ts +++ b/packages/server/express_server.ts @@ -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",