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

105 lines
2.8 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",
];
2022-05-13 12:36:26 +00:00
import * as ts from "typescript";
type CompileError = {
message: string;
pos: number;
};
function checkTypeScript(scriptFile: string): void {
let program = ts.createProgram([scriptFile], {
noEmit: true,
allowJs: true,
});
let emitResult = program.emit();
let allDiagnostics = ts
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics);
let errors: CompileError[] = [];
allDiagnostics.forEach((diagnostic) => {
if (diagnostic.file) {
let { line, character } = ts.getLineAndCharacterOfPosition(
diagnostic.file,
diagnostic.start!
);
let message = ts.flattenDiagnosticMessageText(
diagnostic.messageText,
"\n"
);
errors.push({
message: ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"),
pos: diagnostic.start!,
});
// console.log(
// `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`
// );
} else {
console.log(
ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n")
);
}
});
let exitCode = emitResult.emitSkipped ? 1 : 0;
console.log(`Process exiting with code '${exitCode}'.`);
process.exit(exitCode);
}
2022-04-25 17:46:08 +00:00
export function esbuildSyscalls(): SysCallMapping {
return {
2022-05-13 12:36:26 +00:00
"tsc.analyze": async (
ctx,
filename: string,
code: string
): Promise<any> => {},
2022-04-25 17:46:08 +00:00
"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> => {
2022-05-13 12:36:26 +00:00
let tmpDir = await prepareCompileEnv(filename, code);
2022-05-11 09:49:27 +00:00
let jsCode = await compile(`${tmpDir}/${filename}`, functionName, false, [
"yaml",
2022-05-13 12:36:26 +00:00
"handlebars",
2022-05-11 09:49:27 +00:00
]);
2022-04-25 17:46:08 +00:00
await rm(tmpDir, { recursive: true });
return jsCode;
},
};
}
2022-05-13 12:36:26 +00:00
async function prepareCompileEnv(filename: string, code: 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);
return tmpDir;
}