2022-03-20 08:56:28 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2022-05-13 12:36:26 +00:00
|
|
|
import { readFile, watch, writeFile } from "fs/promises";
|
2022-03-20 08:56:28 +00:00
|
|
|
import path from "path";
|
|
|
|
|
|
|
|
import yargs from "yargs";
|
|
|
|
import { hideBin } from "yargs/helpers";
|
2022-03-21 14:21:34 +00:00
|
|
|
import { Manifest } from "../types";
|
2022-03-27 09:26:13 +00:00
|
|
|
import YAML from "yaml";
|
2022-04-21 11:57:45 +00:00
|
|
|
import { mkdirSync } from "fs";
|
2022-05-13 12:36:26 +00:00
|
|
|
import { compile, sandboxCompileModule } from "../compile";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-04-25 08:33:38 +00:00
|
|
|
async function bundle(
|
|
|
|
manifestPath: string,
|
2022-05-13 15:05:52 +00:00
|
|
|
debug: boolean,
|
2022-04-25 08:33:38 +00:00
|
|
|
excludeModules: string[]
|
|
|
|
) {
|
2022-03-20 08:56:28 +00:00
|
|
|
const rootPath = path.dirname(manifestPath);
|
2022-03-27 09:26:13 +00:00
|
|
|
const manifest = YAML.parse(
|
2022-03-21 14:21:34 +00:00
|
|
|
(await readFile(manifestPath)).toString()
|
|
|
|
) as Manifest<any>;
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-04-26 17:04:36 +00:00
|
|
|
if (!manifest.name) {
|
|
|
|
throw new Error(`Missing 'name' in ${manifestPath}`);
|
|
|
|
}
|
|
|
|
|
2022-05-13 12:36:26 +00:00
|
|
|
let allModulesToExclude = excludeModules.slice();
|
|
|
|
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 09:22:38 +00:00
|
|
|
let jsFunctionName = "default",
|
|
|
|
filePath = path.join(rootPath, def.path!);
|
2022-03-20 08:56:28 +00:00
|
|
|
if (filePath.indexOf(":") !== -1) {
|
|
|
|
[filePath, jsFunctionName] = filePath.split(":");
|
|
|
|
}
|
|
|
|
|
2022-04-25 08:33:38 +00:00
|
|
|
def.code = await compile(
|
|
|
|
filePath,
|
|
|
|
jsFunctionName,
|
2022-05-13 15:05:52 +00:00
|
|
|
debug,
|
2022-05-13 12:36:26 +00:00
|
|
|
allModulesToExclude
|
2022-04-25 08:33:38 +00:00
|
|
|
);
|
2022-03-20 08:56:28 +00:00
|
|
|
delete def.path;
|
|
|
|
}
|
|
|
|
return manifest;
|
|
|
|
}
|
2022-03-27 07:55:29 +00:00
|
|
|
|
|
|
|
async function buildManifest(
|
|
|
|
manifestPath: string,
|
|
|
|
distPath: string,
|
2022-04-25 08:33:38 +00:00
|
|
|
debug: boolean,
|
|
|
|
excludeModules: string[]
|
2022-03-27 07:55:29 +00:00
|
|
|
) {
|
2022-04-25 08:33:38 +00:00
|
|
|
let generatedManifest = await bundle(manifestPath, debug, excludeModules);
|
2022-03-27 09:26:13 +00:00
|
|
|
const outFile =
|
|
|
|
manifestPath.substring(
|
|
|
|
0,
|
|
|
|
manifestPath.length - path.extname(manifestPath).length
|
|
|
|
) + ".json";
|
|
|
|
const outPath = path.join(distPath, path.basename(outFile));
|
2022-03-27 07:55:29 +00:00
|
|
|
console.log("Emitting bundle to", outPath);
|
|
|
|
await writeFile(outPath, JSON.stringify(generatedManifest, null, 2));
|
|
|
|
return { generatedManifest, outPath };
|
|
|
|
}
|
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
async function run() {
|
2022-03-27 07:55:29 +00:00
|
|
|
let args = yargs(hideBin(process.argv))
|
2022-03-20 08:56:28 +00:00
|
|
|
.option("debug", {
|
|
|
|
type: "boolean",
|
|
|
|
})
|
2022-03-27 07:55:29 +00:00
|
|
|
.option("watch", {
|
|
|
|
type: "boolean",
|
|
|
|
alias: "w",
|
|
|
|
})
|
|
|
|
.option("dist", {
|
|
|
|
type: "string",
|
|
|
|
default: ".",
|
|
|
|
})
|
2022-04-25 08:33:38 +00:00
|
|
|
.option("exclude", {
|
|
|
|
type: "array",
|
|
|
|
default: [],
|
|
|
|
})
|
2022-03-20 08:56:28 +00:00
|
|
|
.parse();
|
2022-03-27 07:55:29 +00:00
|
|
|
if (args._.length === 0) {
|
|
|
|
console.log(
|
2022-04-25 08:33:38 +00:00
|
|
|
"Usage: plugos-bundle [--debug] [--dist <path>] [--exclude package1 package2] -- <manifest.plug.yaml> <manifest2.plug.yaml> ..."
|
2022-03-27 07:55:29 +00:00
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2022-03-28 06:51:24 +00:00
|
|
|
|
2022-04-25 08:33:38 +00:00
|
|
|
console.log("Args", args);
|
|
|
|
|
2022-03-28 06:51:24 +00:00
|
|
|
async function buildAll() {
|
2022-04-21 11:57:45 +00:00
|
|
|
mkdirSync(args.dist, { recursive: true });
|
2022-03-28 06:51:24 +00:00
|
|
|
for (const plugManifestPath of args._) {
|
|
|
|
let manifestPath = plugManifestPath as string;
|
|
|
|
try {
|
2022-04-25 08:33:38 +00:00
|
|
|
await buildManifest(
|
|
|
|
manifestPath,
|
|
|
|
args.dist,
|
|
|
|
!!args.debug,
|
|
|
|
args.exclude
|
|
|
|
);
|
2022-03-28 06:51:24 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(`Error building ${manifestPath}:`, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await buildAll();
|
|
|
|
if (args.watch) {
|
|
|
|
console.log("Watching for changes...");
|
|
|
|
for await (const { eventType, filename } of watch(".", {
|
|
|
|
recursive: true,
|
|
|
|
})) {
|
|
|
|
if (
|
|
|
|
filename.endsWith(".plug.yaml") ||
|
2022-04-25 17:46:08 +00:00
|
|
|
filename.endsWith(".js") ||
|
|
|
|
(filename.endsWith(".ts") && !filename.endsWith("_in.ts"))
|
2022-03-28 06:51:24 +00:00
|
|
|
) {
|
|
|
|
console.log("Change detected", eventType, filename);
|
|
|
|
await buildAll();
|
|
|
|
}
|
2022-03-27 07:55:29 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
run().catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
process.exit(1);
|
|
|
|
});
|