2024-02-16 21:38:09 +02:00
|
|
|
const utils = require('./utils');
|
2024-04-04 22:00:20 +03:00
|
|
|
const BackendScriptApi = require('./backend_script_api');
|
2018-01-27 17:18:19 -05:00
|
|
|
|
2019-01-27 12:28:20 +01:00
|
|
|
function ScriptContext(allNotes, apiParams = {}) {
|
2018-03-06 23:04:35 -05:00
|
|
|
this.modules = {};
|
|
|
|
this.notes = utils.toObject(allNotes, note => [note.noteId, note]);
|
2019-01-27 12:28:20 +01:00
|
|
|
this.apis = utils.toObject(allNotes, note => [note.noteId, new BackendScriptApi(note, apiParams)]);
|
2018-03-10 11:53:51 -05:00
|
|
|
this.require = moduleNoteIds => {
|
|
|
|
return moduleName => {
|
|
|
|
const candidates = allNotes.filter(note => moduleNoteIds.includes(note.noteId));
|
|
|
|
const note = candidates.find(c => c.title === moduleName);
|
|
|
|
|
|
|
|
if (!note) {
|
2019-08-12 20:41:38 +02:00
|
|
|
return require(moduleName);
|
2018-03-10 11:53:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.modules[note.noteId].exports;
|
|
|
|
}
|
|
|
|
};
|
2018-03-06 23:04:35 -05:00
|
|
|
}
|
|
|
|
|
2023-11-22 19:34:48 +01:00
|
|
|
module.exports = ScriptContext;
|