silverbullet/packages/plugos/bin/plugos-bundle.ts

137 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-10-04 22:33:29 +08:00
#!/usr/bin/env deno
2022-10-04 22:33:29 +08:00
import { Manifest } from "../types.ts";
2022-10-06 17:31:49 +08:00
import { YAML } from "../../../dep_common.ts";
2022-10-04 22:33:29 +08:00
import { compile, esbuild, sandboxCompileModule } from "../compile.ts";
2022-10-06 17:31:49 +08:00
import { path } from "../../../dep_server.ts";
2022-10-04 22:33:29 +08:00
import * as flags from "https://deno.land/std@0.158.0/flags/mod.ts";
2022-04-25 16:33:38 +08:00
async function bundle(
manifestPath: string,
2022-05-13 23:05:52 +08:00
debug: boolean,
2022-10-06 17:31:49 +08:00
excludeModules: string[],
2022-04-25 16:33:38 +08:00
) {
const rootPath = path.dirname(manifestPath);
2022-03-27 17:26:13 +08:00
const manifest = YAML.parse(
2022-10-06 17:31:49 +08:00
await Deno.readTextFile(manifestPath),
2022-03-21 22:21:34 +08:00
) as Manifest<any>;
2022-04-27 01:04:36 +08:00
if (!manifest.name) {
throw new Error(`Missing 'name' in ${manifestPath}`);
}
2022-10-04 22:33:29 +08:00
const allModulesToExclude = excludeModules.slice();
2022-05-13 20:36:26 +08:00
for (let [name, moduleSpec] of Object.entries(manifest.dependencies || {})) {
manifest.dependencies![name] = await sandboxCompileModule(moduleSpec);
allModulesToExclude.push(name);
}
for (let [name, def] of Object.entries(manifest.functions || {})) {
2022-03-20 17:22:38 +08:00
let jsFunctionName = "default",
filePath = path.join(rootPath, def.path!);
if (filePath.indexOf(":") !== -1) {
[filePath, jsFunctionName] = filePath.split(":");
}
2022-04-25 16:33:38 +08:00
def.code = await compile(
filePath,
jsFunctionName,
2022-05-13 23:05:52 +08:00
debug,
2022-05-20 17:29:14 +08:00
allModulesToExclude,
2022-10-06 21:14:21 +08:00
false,
2022-04-25 16:33:38 +08:00
);
delete def.path;
}
return manifest;
}
2022-03-27 15:55:29 +08:00
async function buildManifest(
manifestPath: string,
distPath: string,
2022-04-25 16:33:38 +08:00
debug: boolean,
2022-10-06 17:31:49 +08:00
excludeModules: string[],
2022-03-27 15:55:29 +08:00
) {
2022-10-04 22:33:29 +08:00
const generatedManifest = await bundle(manifestPath, debug, excludeModules);
2022-10-06 17:31:49 +08:00
const outFile = manifestPath.substring(
0,
manifestPath.length - path.extname(manifestPath).length,
) + ".json";
2022-03-27 17:26:13 +08:00
const outPath = path.join(distPath, path.basename(outFile));
2022-03-27 15:55:29 +08:00
console.log("Emitting bundle to", outPath);
2022-10-04 22:33:29 +08:00
await Deno.writeTextFile(outPath, JSON.stringify(generatedManifest, null, 2));
2022-03-27 15:55:29 +08:00
return { generatedManifest, outPath };
}
2022-10-04 22:33:29 +08:00
type BundleArgs = {
_: string[];
dist?: string;
debug?: boolean;
exclude?: string[] | string;
};
2022-03-28 14:51:24 +08:00
2022-10-04 22:33:29 +08:00
export async function run(args: BundleArgs) {
console.log("Args", args);
2022-04-25 16:33:38 +08:00
2022-03-28 14:51:24 +08:00
async function buildAll() {
2022-10-04 22:33:29 +08:00
Deno.mkdirSync(args.dist!, { recursive: true });
2022-03-28 14:51:24 +08:00
for (const plugManifestPath of args._) {
2022-10-04 22:33:29 +08:00
const manifestPath = plugManifestPath as string;
2022-03-28 14:51:24 +08:00
try {
2022-04-25 16:33:38 +08:00
await buildManifest(
manifestPath,
2022-10-04 22:33:29 +08:00
args.dist!,
2022-04-25 16:33:38 +08:00
!!args.debug,
2022-10-06 17:31:49 +08:00
args.exclude as string[],
2022-04-25 16:33:38 +08:00
);
2022-03-28 14:51:24 +08:00
} catch (e) {
console.error(`Error building ${manifestPath}:`, e);
}
}
}
await buildAll();
2022-10-04 22:33:29 +08:00
// if (args.watch) {
// console.log("Watching for changes...");
// for await (const event of Deno.watchFs(".", {
// recursive: true,
// })) {
// if (
// event.filename.endsWith(".plug.yaml") ||
// filename.endsWith(".js") ||
// filename.endsWith(".css") ||
// filename.endsWith(".png") ||
// filename.endsWith(".jpg") ||
// filename.endsWith(".gif") ||
// (filename.endsWith(".ts") && !filename.endsWith("_in.ts"))
// ) {
// console.log("Change detected", eventType, filename);
// await buildAll();
// }
// }
// }
}
2022-10-04 22:33:29 +08:00
if (import.meta.main) {
let args: BundleArgs = flags.parse(Deno.args);
if (args._.length === 0) {
console.log(
2022-10-06 17:31:49 +08:00
"Usage: plugos-bundle [--debug] [--dist <path>] [--exclude=package1,package2] <manifest.plug.yaml> <manifest2.plug.yaml> ...",
2022-10-04 22:33:29 +08:00
);
Deno.exit(1);
}
if (!args.dist) {
args.dist = path.resolve("dist");
}
2022-10-06 17:31:49 +08:00
args.exclude = typeof args.exclude === "string"
? args.exclude.split(",")
: [];
2022-10-04 22:33:29 +08:00
await run(args);
esbuild.stop();
}