UNPKG

@accordproject/concerto-cto

Version:
126 lines 5.01 kB
"use strict"; /* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; const debug_1 = __importDefault(require("debug")); const pathBrowserify = __importStar(require("path-browserify")); const concerto_util_1 = require("@accordproject/concerto-util"); const concerto_metamodel_1 = require("@accordproject/concerto-metamodel"); const Parser = require("./parser"); const debugLog = (0, debug_1.default)('concerto:ModelManager'); /** * Update models with a new model * @param {*} models - existing models * @param {*} newModel - new model * @return {*} the updated models */ function updateModels(models, newModel) { const result = { $class: `${concerto_metamodel_1.MetaModelNamespace}.Models`, models: [], }; const newNamespace = newModel.namespace; const priors = models.models; let found = false; priors.forEach((priorModel) => { if (priorModel.namespace === newNamespace) { result.models.push(newModel); found = true; } else { result.models.push(priorModel); } }); if (!found) { result.models.push(newModel); } return result; } /** * Downloads all ModelFiles that are external dependencies and adds or * updates them in this ModelManager. * @param {*} models - the AST for all the known models * @param {Object} [options] - Options object passed to ModelFileLoaders * @param {FileDownloader} [fileDownloader] - an optional FileDownloader * @throws {IllegalModelException} if the models fail validation * @return {Promise} a promise when the download and update operation is completed. */ async function resolveExternal(models, options, fileDownloader) { const NAME = 'updateExternalModels'; debugLog(NAME, 'updateExternalModels', options); // Create default file downloader if none provided const downloader = fileDownloader || createDefaultFileDownloader(); const externalModelFiles = await downloader.downloadExternalDependencies(models.models, options); let result = models; externalModelFiles.forEach((mf) => { result = updateModels(result, mf); }); return result; } /** * Creates a default file downloader * @return {FileDownloader} a default file downloader instance */ function createDefaultFileDownloader() { // How to create a modelfile from the external content const processFile = (name, data) => { // Note: JSON URLs seem to be already parsed in 'data' // return { ast: data, data, name }; if (pathBrowserify.extname(name) === '.cto') { return Parser.parse(data); } throw new Error('External model file references are expected to have a .cto extension'); }; return new concerto_util_1.FileDownloader(new concerto_util_1.DefaultFileLoader(processFile), (file) => concerto_metamodel_1.MetaModelUtil.getExternalImports(file)); } module.exports = { resolveExternal, createDefaultFileDownloader, }; //# sourceMappingURL=external.js.map