dev-deploy/dev-deploy/test/sc-exec.js
2026-05-17 17:31:49 -05:00

57 lines
2.6 KiB
JavaScript

// Like `saltcorn run-js` but without the vm sandbox -- the supplied JS body
// runs with full `require()` access so the e2e suite can drive Field /
// TableConstraint / File creation. Invoked from the test runner via:
//
// source <env.sh> && node test/sc-exec.js "<javascript code>"
const { createRequire } = require("node:module");
const path = require("node:path");
// Resolve @saltcorn/* against the Saltcorn checkout's node_modules. Node
// resolves require from the script's path by default, which doesn't see
// Saltcorn's deps; createRequire reroots the resolution. Layout:
// <project-root>/
// dev-deploy/test/sc-exec.js (this file)
// saltcorn/packages/saltcorn-data/
// So: up 2 to project root, then into saltcorn/packages/saltcorn-data/.
const scRequire = createRequire(path.join(__dirname, "..", "..", "saltcorn", "packages", "saltcorn-data", "package.json"));
const main = async () => {
// Read code from stdin -- avoids shell-escape gymnastics for multi-line
// bodies (notably \n which bash double-quotes pass through literally).
const code = require("node:fs").readFileSync(0, "utf8");
if (!code) {
console.error("usage: pipe JS code into sc-exec.js's stdin");
process.exit(2);
}
const Plugin = scRequire("@saltcorn/data/models/plugin");
const { init_multi_tenant } = scRequire("@saltcorn/data/db/state");
await Plugin.loadAllPlugins();
await init_multi_tenant(Plugin.loadAllPlugins, undefined, []);
const Table = scRequire("@saltcorn/data/models/table");
const Field = scRequire("@saltcorn/data/models/field");
const View = scRequire("@saltcorn/data/models/view");
const Page = scRequire("@saltcorn/data/models/page");
const Trigger = scRequire("@saltcorn/data/models/trigger");
const TableConstraint = scRequire("@saltcorn/data/models/table_constraints");
const File = scRequire("@saltcorn/data/models/file");
const db = scRequire("@saltcorn/data/db");
// Build an async closure with the models + a re-rooted require in scope.
// (`new Function` bodies don't inherit require from the script scope.)
const fn = new Function(
"Table", "Field", "View", "Page", "Trigger", "TableConstraint", "File", "db", "require",
`return (async () => { ${code} })();`
);
await fn(Table, Field, View, Page, Trigger, TableConstraint, File, db, scRequire);
process.exit(0);
};
main().catch((err) => {
console.error(err && err.stack ? err.stack : err);
process.exit(1);
});