UNPKG

5.2 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17const fs = require('fs');
18const fsPath = require('path');
19const slash = require('slash');
20const JSZip = require('jszip');
21
22const FileLoader = require('./fileloader');
23const LogicManager = require('./logicmanager');
24
25/**
26 * Builds a LogicManager from a directory.
27 *
28 * @param {String} path to a local directory
29 * @param {Object} [options] - an optional set of options to configure the instance.
30 * @return {Promise<LogicManager>} a Promise to the instantiated logicmanager
31 */
32async function fromDirectory(path, options) {
33 if (!options) {
34 options = {};
35 }
36
37 const logicManager = new LogicManager('es6', options);
38
39 const ctoFiles = await FileLoader.loadFilesContents(path, /model[/\\].*\.cto$/);
40 ctoFiles.forEach((file) => {
41 logicManager.getModelManager().addAPModelFile(file.contents, file.name);
42 });
43
44 // Update external models
45 await logicManager.getModelManager().updateExternalModels();
46
47 // load and add the ergo files
48 const ergoFiles = await FileLoader.loadFilesContents(path, /logic[/\\].*\.ergo$/);
49 ergoFiles.forEach((file) => {
50 const resolvedPath = slash(fsPath.resolve(path));
51 const resolvedFilePath = slash(fsPath.resolve(file.name));
52 const truncatedPath = resolvedFilePath.replace(resolvedPath+'/', '');
53 logicManager.addLogicFile(file.contents, truncatedPath);
54 });
55
56 // load and add the formulas
57 let formulas = await FileLoader.loadFilesContents(path, /text[/\\].*\.tem$/);
58 formulas.forEach((file) => {
59 const baseName = fsPath.basename(file.name).replace('.tem','');
60 logicManager.addTemplateFile(file.contents, baseName);
61 });
62
63 return logicManager;
64}
65
66/**
67 * Builds a LogicManager from a Zip.
68 *
69 * @param {Buffer} buffer - the buffer to a Zip (zip) file
70 * @param {Object} [options] - an optional set of options to configure the instance.
71 * @return {Promise<LogicManager>} a Promise to the instantiated logicmanager
72 */
73async function fromZip(buffer, options) {
74 if (!options) {
75 options = {};
76 }
77
78 const zip = await JSZip.loadAsync(buffer);
79 const logicManager = new LogicManager('es6', options);
80
81 const ctoFiles = await FileLoader.loadZipFilesContents(zip, /model[/\\].*\.cto$/);
82 ctoFiles.forEach((file) => {
83 logicManager.getModelManager().addAPModelFile(file.contents, file.name);
84 });
85
86 // Update external models
87 await logicManager.getModelManager().updateExternalModels();
88
89 // load and add the ergo files
90 const ergoFiles = await FileLoader.loadZipFilesContents(zip, /logic[/\\].*\.ergo$/);
91 ergoFiles.forEach((file) => {
92 logicManager.addLogicFile(file.contents, file.name);
93 });
94
95 // load and add the formulas
96 let formulas = await FileLoader.loadZipFilesContents(zip, /text[/\\].*\.tem$/);
97 formulas.forEach((file) => {
98 const baseName = fsPath.basename(file.name).replace('.tem','');
99 logicManager.addTemplateFile(file.contents, baseName);
100 });
101
102 return logicManager;
103}
104
105/**
106 * Builds a LogicManager from files.
107 *
108 * @param {String[]} files - file names
109 * @param {Object} [options] - an optional set of options to configure the instance.
110 * @return {Promise<LogicManager>} a Promise to the instantiated logicmanager
111 */
112async function fromFiles(files, options) {
113 if (!options) {
114 options = {};
115 }
116
117 const logicManager = new LogicManager('es6', options);
118
119 let modelPaths = [];
120 let logicPaths = [];
121 let formulaPaths = [];
122
123 for (let i = 0; i < files.length; i++) {
124 const file = files[i];
125 if (file.split('.').pop() === 'cto') {
126 modelPaths.push(file);
127 } else if (file.split('.').pop() === 'ergo') {
128 logicPaths.push(file);
129 } else if (file.split('.').pop() === 'tem') {
130 formulaPaths.push(file);
131 }
132 }
133 modelPaths.forEach((path) => {
134 const file = fs.readFileSync(path, 'utf8');
135 logicManager.getModelManager().addAPModelFile(file, path);
136 });
137
138 // Update external models
139 await logicManager.getModelManager().updateExternalModels();
140
141 // load and add the ergo files
142 logicPaths.forEach((path) => {
143 const file = fs.readFileSync(path, 'utf8');
144 logicManager.addLogicFile(file, path);
145 });
146
147 // load and add the formulas
148 formulaPaths.forEach((path) => {
149 const file = fs.readFileSync(path, 'utf8');
150 const baseName = fsPath.basename(path).replace('.tem','');
151 logicManager.addTemplateFile(file, baseName);
152 });
153
154 return logicManager;
155}
156
157module.exports = { fromDirectory, fromZip, fromFiles };