From 52f1672b0135b53805efa8f225d250c18b174752 Mon Sep 17 00:00:00 2001 From: NJaku01 Date: Tue, 15 Jun 2021 15:31:38 +0200 Subject: [PATCH] store bindings information in ERC.yaml #224 --- ui/bindings/controllers/bindings.js | 3 +++ ui/bindings/controllers/generalFunctions.js | 24 +++++++++++++++++++++ ui/bindings/package-lock.json | 5 +++++ ui/bindings/package.json | 3 ++- ui/bindings/test/package.json | 2 +- 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/ui/bindings/controllers/bindings.js b/ui/bindings/controllers/bindings.js index 5102a0a6..5e779cfc 100644 --- a/ui/bindings/controllers/bindings.js +++ b/ui/bindings/controllers/bindings.js @@ -53,6 +53,8 @@ bindings.start = (conf) => { bindings.createBinding(req.body, res); }); + + app.get('/api/v1/compendium/:compendium/binding/:binding', function(req, res) { let compendium = req.params.compendium; let binding = req.params.binding; @@ -178,6 +180,7 @@ bindings.createBinding = function(binding, response) { bindingCode = fn.replaceVariable( bindingCode, binding.sourcecode.parameter ); let wrappedBindingCode = fn.wrapCode( bindingCode, binding.computationalResult.result, binding.sourcecode.parameter, figureSize ); fn.saveResult( wrappedBindingCode, binding.id, binding.computationalResult.result.replace(/\s/g, '').toLowerCase() ); + fn.accessYaml(binding.id, binding) binding.codesnippet = binding.computationalResult.result.replace(/\s/g, '').toLowerCase() + '.R'; response.send({ callback: 'ok', diff --git a/ui/bindings/controllers/generalFunctions.js b/ui/bindings/controllers/generalFunctions.js index bc8a17f3..4c293986 100644 --- a/ui/bindings/controllers/generalFunctions.js +++ b/ui/bindings/controllers/generalFunctions.js @@ -22,6 +22,7 @@ const fs = require('fs'); const debug = require('debug')('bindings'); const path = require('path'); const exec = require('child_process').exec; +const yaml = require('yaml') let fn = {}; @@ -280,6 +281,29 @@ fn.array2Json = function (array) { return jsonObject; }; +fn.accessYaml= function(compendiumId, binding ){ + debug('Start reading YAML from the compendium %s', compendiumId); + if ( !compendiumId ) { + throw new Error('File does not exist.'); + } + let paper = path.join(config.fs.compendium, compendiumId, "erc.yml"); + console.log(paper) + if(fs.existsSync(paper)) { + debug('Start reading YMAL from %s', paper); + let file =fs.readFileSync(paper, 'utf8'); + let yamlFile = yaml.parse(file); + let element = {interactive: true, bindings:binding} + yamlFile.ui_bindings = element + file = yaml.stringify(yamlFile) + fs.writeFileSync(paper, file) + } else { + debug('Cannot open file %s', paper); + response.status(401).send({ + callback: 'fileNotFound',}); + }; + +} + module.exports = fn; diff --git a/ui/bindings/package-lock.json b/ui/bindings/package-lock.json index 92b415b1..e0b950d2 100644 --- a/ui/bindings/package-lock.json +++ b/ui/bindings/package-lock.json @@ -2103,6 +2103,11 @@ "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=", "dev": true + }, + "yaml": { + "version": "1.10.2", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-1.10.2.tgz", + "integrity": "sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==" } } } diff --git a/ui/bindings/package.json b/ui/bindings/package.json index a0936158..4f7613c6 100644 --- a/ui/bindings/package.json +++ b/ui/bindings/package.json @@ -21,7 +21,8 @@ "net": "^1.0.2", "node-cron": "^2.0.3", "r-script": "git+https://github.com/MarkusKonk/r-script.git", - "request": "^2.88.0" + "request": "^2.88.0", + "yaml": "^1.10.2" }, "devDependencies": { "eslint": "^4.16.0", diff --git a/ui/bindings/test/package.json b/ui/bindings/test/package.json index 51d13fa1..f215366a 100644 --- a/ui/bindings/test/package.json +++ b/ui/bindings/test/package.json @@ -4,7 +4,7 @@ "description": "", "main": "bindingsTests.js", "scripts": { - "test": "echo \"Error: no test was specified\" && exit 1" + "test": "mocha" }, "author": "", "license": "ISC",