changes in backend script running

This commit is contained in:
azivner
2018-03-03 09:11:41 -05:00
parent 982b723647
commit d169f67901
5 changed files with 54 additions and 58 deletions

View File

@@ -4,7 +4,6 @@ const express = require('express');
const router = express.Router();
const auth = require('../../services/auth');
const wrap = require('express-promise-wrap').wrap;
const notes = require('../../services/notes');
const attributes = require('../../services/attributes');
const script = require('../../services/script');
const Repository = require('../../services/repository');
@@ -24,13 +23,15 @@ router.post('/job', auth.checkApiAuth, wrap(async (req, res, next) => {
}));
router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteIds = await attributes.getNoteIdsWithAttribute("run_on_startup");
const noteIds = await attributes.getNoteIdsWithAttribute("run", "frontend_startup");
const repository = new Repository(req);
const scripts = [];
for (const noteId of noteIds) {
scripts.push(await getNoteWithSubtreeScript(noteId, repository));
const note = await repository.getNote(noteId);
scripts.push(await script.getNoteScript(note));
}
res.send(scripts);
@@ -38,55 +39,9 @@ router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => {
router.get('/subtree/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
const repository = new Repository(req);
const noteId = req.params.noteId;
const note = await repository.getNote(req.params.noteId);
res.send(await getNoteWithSubtreeScript(noteId, repository));
res.send(await script.getNoteScript(note, repository));
}));
async function getNoteWithSubtreeScript(noteId, repository) {
const note = await repository.getNote(noteId);
let noteScript = note.content;
if (note.isJavaScript()) {
// last \r\n is necessary if script contains line comment on its last line
noteScript = "(async function() {" + noteScript + "\r\n})()";
}
const subTreeScripts = await getSubTreeScripts(noteId, [noteId], repository, note.isJavaScript());
return subTreeScripts + noteScript;
}
async function getSubTreeScripts(parentId, includedNoteIds, repository, isJavaScript) {
const children = await repository.getEntities(`
SELECT notes.*
FROM notes JOIN note_tree USING(noteId)
WHERE note_tree.isDeleted = 0 AND notes.isDeleted = 0
AND note_tree.parentNoteId = ? AND (notes.type = 'code' OR notes.type = 'file')
AND (notes.mime = 'application/javascript'
OR notes.mime = 'application/x-javascript'
OR notes.mime = 'text/html')`, [parentId]);
let script = "\r\n";
for (const child of children) {
if (includedNoteIds.includes(child.noteId)) {
return;
}
includedNoteIds.push(child.noteId);
script += await getSubTreeScripts(child.noteId, includedNoteIds, repository);
if (!isJavaScript && child.isJavaScript()) {
child.content = '<script>' + child.content + '</script>';
}
script += child.content + "\r\n";
}
return script;
}
module.exports = router;