Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async loadTemplateFromUrl(templateURL) {
const thisTemplateURL = templateURL;
this.setState({ loading: true });
console.log(`Loading template: ${thisTemplateURL}`);
let template;
try {
template = await Template.fromUrl(thisTemplateURL);
} catch (error) {
console.log(`LOAD FAILED! ${error.message}`); // Error!
this.handleLoadingFailed(error.message);
return false;
}
try {
const newState = {...this.state};
newState.templateURL = thisTemplateURL;
newState.clause = new Clause(template);
newState.templateName = newState.clause.getTemplate().getMetadata().getName();
newState.templateVersion = newState.clause.getTemplate().getMetadata().getVersion();
newState.templateType = newState.clause.getTemplate().getMetadata().getTemplateType();
newState.package = JSON.stringify(template.getMetadata().getPackageJson(), null, 2);
newState.grammar = template.getParserManager().getTemplatizedGrammar();
newState.model = template.getModelManager().getModels();
newState.logic = template.getScriptManager().getLogic();
newState.text = template.getMetadata().getSamples().default;
newState.request = JSON.stringify(template.getMetadata().getRequest(), null, 2);
newState.data = 'null';
this.setState(newState);
this.setState(Utils.compileLogic(null, this.state.markers, this.state.logic, this.state.clause, this.state.log));
this.handleModelChange(null, this.state, this.state.model);
this.handleSampleChangeInit(this.state.text);
this._handleLogicChange();
this.handlePackageChange(this.state.package);
async loadTemplateFromBuffer(buffer) {
this.setState({ loading: true });
console.log('Loading template from Buffer');
let template;
try {
template = await Template.fromArchive(buffer);
} catch (error) {
console.log(`LOAD FAILED! ${error.message}`); // Error!
this.handleLoadingFailed(error.message);
return false;
}
try {
const newState = {...this.state};
newState.clause = new Clause(template);
newState.templateName = newState.clause.getTemplate().getMetadata().getName();
newState.templateVersion = newState.clause.getTemplate().getMetadata().getVersion();
newState.templateType = newState.clause.getTemplate().getMetadata().getTemplateType();
newState.package = JSON.stringify(template.getMetadata().getPackageJson(), null, 2);
newState.grammar = template.getParserManager().getTemplatizedGrammar();
newState.model = template.getModelManager().getModels();
newState.logic = template.getScriptManager().getLogic();
newState.text = template.getMetadata().getSamples().default;
newState.request = JSON.stringify(template.getMetadata().getRequest(), null, 2);
newState.data = 'null';
this.setState(newState);
this.setState(Utils.compileLogic(null, this.state.markers, this.state.logic, this.state.clause, this.state.log));
this.handleModelChange(null, this.state, this.state.model);
this.handleSampleChangeInit(this.state.text);
this._handleLogicChange();
this.handlePackageChange(this.state.package);
.then(async (template) => {
// Initialize clause
clause = new Clause(template);
clause.parse(sampleText, currentTime);
let stateJson;
if(!fs.existsSync(statePath)) {
Logger.warn('A state file was not provided, initializing state. Try the --state flag or create a state.json in the root folder of your template.');
const initResult = await engine.init(clause, currentTime);
stateJson = initResult.state;
} else {
stateJson = JSON.parse(fs.readFileSync(statePath, 'utf8'));
}
return engine.invoke(clause, clauseName, paramsJson, stateJson, currentTime);
})
.catch((err) => {
.then((template) => {
// Initialize clause
clause = new Clause(template);
clause.parse(sampleText, currentTime);
return engine.init(clause, currentTime);
})
.catch((err) => {
.then(async (template) => {
// Initialize clause
clause = new Clause(template);
clause.parse(sampleText, currentTime);
let stateJson;
if(!fs.existsSync(statePath)) {
Logger.warn('A state file was not provided, initializing state. Try the --state flag or create a state.json in the root folder of your template.');
const initResult = await engine.init(clause, currentTime);
stateJson = initResult.state;
} else {
stateJson = JSON.parse(fs.readFileSync(statePath, 'utf8'));
}
// First execution to get the initial response
const firstRequest = requestsJson[0];
const initResponse = engine.trigger(clause, firstRequest, stateJson, currentTime);
// Get all the other requests and chain execution through Promise.reduce()
const otherRequests = requestsJson.slice(1, requestsJson.length);
export default async function onClauseUpdated(template, editorRef, clauseNode) {
const clauseId = clauseNode.data.get('clauseid');
try {
const ciceroClause = new Clause(template);
const templateUri = clauseNode.data.get('src');
const value = {
document: {
nodes: clauseNode.nodes
}
};
const slateTransformer = new SlateTransformer();
const parseText = slateTransformer.toMarkdown(value, { wrapVariables: false });
ciceroClause.parse(parseText);
const parseResult = ciceroClause.getData();
store.dispatch(parseClauseSuccess(clauseId, parseResult));
if (template.grammarHasErgoExpression()) {
// only replace node if template as computed expression
const variableText = await ciceroClause.draft({ wrapVariables: false });
async function loadClause(templateDir) {
const template = await Template.fromDirectory(templateDir);
return new Clause(template);
}
.then((template) => {
clause = new Clause(template);
clause.parse(sampleText, currentTime, samplePath);
if (outputPath) {
Logger.info('Creating file: ' + outputPath);
fs.writeFileSync(outputPath, JSON.stringify(clause.getData(),null,2));
}
return clause.getData();
})
.catch((err) => {
.then(async function (template) {
clause = new Clause(template);
clause.parse(sampleText, currentTime, samplePath);
if (outputPath) {
Logger.info('Creating file: ' + outputPath);
fs.writeFileSync(outputPath, JSON.stringify(clause.getData(),null,2));
}
const text = await clause.draft(options, currentTime);
if (outputPath) {
Logger.info('Creating file: ' + outputPath);
fs.writeFileSync(outputPath, text);
}
return text;
})
.catch((err) => {