Skip to content

Commit

Permalink
fix(server) fromArchive() check error fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehmet Tokgöz committed Aug 27, 2022
1 parent 5559f4f commit fc85854
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions packages/cicero-server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

'use strict';

const fs = require('fs');

const app = require('express')();
const bodyParser = require('body-parser');
const Template = require('@accordproject/cicero-core').Template;
Expand Down Expand Up @@ -236,23 +238,28 @@ app.post('/invoke/:template', async function(req, httpResponse, next) {
});

/**
* Helper function to determine whether the template archived or not
* @param {string} templateName Name of the template directory or archive file
* @returns {boolean} True if the given template is a cta file
* Helper function to determine whether the template is archived or not
* @param {string} templateName Name of the template
* @returns {boolean} True if the given template is a .cta file
*/
function isTemplateArchive(templateName) {
return fs.lstatSync(`${process.env.CICERO_DIR}/${templateName}`).isFile();
try {
fs.lstatSync(`${process.env.CICERO_DIR}/${templateName}.cta`).isFile();
return true;
} catch(err) {
return false;
}
}

/**
* Helper function to load a template from disk
* @param {string} templateName Name of the template directory or archive file
* @param {string} templateName Name of the template
* @param {object} options an optional set of options
* @returns {object} The template instance object.
*/
async function loadTemplate(templateName, options) {
if (isTemplateArchive(templateName)) {
const buffer = fs.readFileSync(`${process.env.CICERO_DIR}/${templateName}`);
const buffer = fs.readFileSync(`${process.env.CICERO_DIR}/${templateName}.cta`);
return await Template.fromArchive(buffer, options);
} else {
return await Template.fromDirectory(`${process.env.CICERO_DIR}/${templateName}`, options);
Expand Down

0 comments on commit fc85854

Please sign in to comment.