@grouparoo/core
Version:
The Grouparoo Core
57 lines (56 loc) • 2.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeConfig = void 0;
const actionhero_1 = require("actionhero");
const configLoaders_1 = require("../modules/configLoaders");
const pluginDetails_1 = require("../modules/pluginDetails");
const GrouparooModel_1 = require("../models/GrouparooModel");
const GrouparooRecord_1 = require("../models/GrouparooRecord");
const record_1 = require("../modules/ops/record");
const cls_1 = require("../modules/cls");
const runMode_1 = require("../modules/runMode");
class CodeConfig extends actionhero_1.Initializer {
constructor() {
super();
this.name = "codeConfig";
this.loadPriority = 10000;
this.startPriority = 10;
}
async initialize() {
actionhero_1.api.codeConfig = {
allowLockedModelChanges: true,
};
}
async start() {
await cls_1.CLS.wrap(async () => {
const configDir = await (0, pluginDetails_1.getConfigDir)((0, runMode_1.getGrouparooRunMode)() === "cli:config");
const { errors } = await (0, configLoaders_1.loadConfigDirectory)(configDir);
if (errors.length > 0)
throw new Error("code config error: " + errors.join("; "));
});
actionhero_1.api.codeConfig.allowLockedModelChanges = false; // after this point in the Actionhero boot lifecycle, locked models cannot be changed
await loadSampleProfiles();
}
async stop() { }
}
exports.CodeConfig = CodeConfig;
async function loadSampleProfiles() {
if ((0, runMode_1.getGrouparooRunMode)() !== "cli:config")
return;
const models = await GrouparooModel_1.GrouparooModel.findAll();
for (const model of models) {
const records = await GrouparooRecord_1.GrouparooRecord.findAll({
where: { modelId: model.id },
});
if (records.length === 0)
continue;
(0, actionhero_1.log)(`importing ${records.length} sample records from the ${model.name} model`);
const responses = await record_1.RecordOps.opportunisticallyImportAndUpdateInline(records);
(0, actionhero_1.log)(`imported ${responses.filter((r) => r.success).length}/${records.length} sample records from the ${model.name} model`);
for (const { recordId, success, error } of responses) {
if (!success) {
(0, actionhero_1.log)(` - error importing sample record ${recordId} - ${error}`, "error");
}
}
}
}