62 lines
2.1 KiB
JavaScript
62 lines
2.1 KiB
JavaScript
// dev-deploy: Saltcorn plugin for migrating metadata changes across
|
|
// Dev/Test/Prod environments via an ops journal with stable UUIDs.
|
|
|
|
const { PLUGIN_NAME, PLUGIN_VERSION } = require("./lib/constants");
|
|
const { createAllTables } = require("./lib/schema");
|
|
const { getEnv, initEnvIfMissing, markBootstrapped } = require("./lib/env");
|
|
const { backfillAll } = require("./lib/entityIds");
|
|
const { installAllWraps } = require("./lib/wrap");
|
|
const { routes } = require("./lib/routes");
|
|
|
|
|
|
const log = (msg) => {
|
|
// eslint-disable-next-line no-console
|
|
console.log(`[${PLUGIN_NAME}] ${msg}`);
|
|
};
|
|
|
|
|
|
const ensureCsrfBypass = async () => {
|
|
try {
|
|
const { getState } = require("@saltcorn/data/db/state");
|
|
const current = getState().getConfig("disable_csrf_routes", "");
|
|
const want = "/dev-deploy/api/";
|
|
const entries = current.split(",").map((s) => s.trim()).filter(Boolean);
|
|
if (!entries.includes(want)) {
|
|
entries.push(want);
|
|
await getState().setConfig("disable_csrf_routes", entries.join(","));
|
|
}
|
|
} catch (e) {
|
|
// eslint-disable-next-line no-console
|
|
console.error(`[${PLUGIN_NAME}] failed to register csrf bypass:`, e);
|
|
}
|
|
};
|
|
|
|
|
|
const onLoad = async (cfg) => {
|
|
try {
|
|
await createAllTables();
|
|
const env = await initEnvIfMissing();
|
|
if (!env.bootstrapped_at) {
|
|
const counts = await backfillAll();
|
|
const total = Object.values(counts).reduce((a, b) => a + b, 0);
|
|
await markBootstrapped(env.env_id);
|
|
log(`v${PLUGIN_VERSION} bootstrapped env_id=${env.env_id} backfilled ${total} entities ${JSON.stringify(counts)}`);
|
|
} else {
|
|
log(`v${PLUGIN_VERSION} loaded env_id=${env.env_id}`);
|
|
}
|
|
installAllWraps();
|
|
await ensureCsrfBypass();
|
|
} catch (err) {
|
|
// eslint-disable-next-line no-console
|
|
console.error(`[${PLUGIN_NAME}] onLoad failed:`, err);
|
|
throw err;
|
|
}
|
|
};
|
|
|
|
|
|
module.exports = {
|
|
sc_plugin_api_version: 1,
|
|
plugin_name: PLUGIN_NAME,
|
|
onLoad: onLoad,
|
|
routes: routes
|
|
};
|