1
0
silverbullet/packages/plugos/syscalls/esbuild.ts

46 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-04-25 17:46:08 +00:00
import { compile } from "../compile";
import { SysCallMapping } from "../system";
import { tmpdir } from "os";
import { mkdir, rm, symlink, writeFile } from "fs/promises";
import { nodeModulesDir } from "../environments/node_sandbox";
const exposedModules = [
"@silverbulletmd/plugos-silverbullet-syscall",
"@plugos/plugos-syscall",
"yaml",
];
export function esbuildSyscalls(): SysCallMapping {
return {
"esbuild.compile": async (
ctx,
filename: string,
2022-05-11 09:49:27 +00:00
code: string,
functionName?: string
2022-04-25 17:46:08 +00:00
): Promise<string> => {
let tmpDir = `${tmpdir()}/plugos-${Math.random()}`;
await mkdir(tmpDir, { recursive: true });
const srcNodeModules = `${nodeModulesDir}/node_modules`;
const targetNodeModules = `${tmpDir}/node_modules`;
await mkdir(`${targetNodeModules}/@silverbulletmd`, { recursive: true });
await mkdir(`${targetNodeModules}/@plugos`, { recursive: true });
for (const exposedModule of exposedModules) {
await symlink(
`${srcNodeModules}/${exposedModule}`,
`${targetNodeModules}/${exposedModule}`,
"dir"
);
}
await writeFile(`${tmpDir}/${filename}`, code);
2022-05-11 09:49:27 +00:00
let jsCode = await compile(`${tmpDir}/${filename}`, functionName, false, [
"yaml",
]);
2022-04-25 17:46:08 +00:00
await rm(tmpDir, { recursive: true });
return jsCode;
},
};
}