UNPKG

@esri/solution-common

Version:

Provides general helper functions for @esri/solution.js.

140 lines 5.37 kB
"use strict"; /** @license * Copyright 2020 Esri * * 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports._extractPathFromResourcePath = exports._extractFilenameFromResourcePath = exports._getSolutionResourceTypeFromResourcePath = exports.transformResourcePathToSolutionResource = exports.transformResourcePathsToSolutionResources = void 0; const solution_resource_1 = require("./solution-resource"); const hub_common_1 = require("@esri/hub-common"); /** * Convert an array of resource strings into an array of ISolutionResources * * Used to migrate Solutions to a more extensible resource schema * * @param resourceStrings Array of resource paths */ function transformResourcePathsToSolutionResources(resourceStrings = []) { return resourceStrings.map(transformResourcePathToSolutionResource); } exports.transformResourcePathsToSolutionResources = transformResourcePathsToSolutionResources; /** * Convert a resource path to a ISolutionResource * * @param resourcePath String */ function transformResourcePathToSolutionResource(resourcePath) { return { filename: _extractFilenameFromResourcePath(resourcePath), type: _getSolutionResourceTypeFromResourcePath(resourcePath), path: _extractPathFromResourcePath(resourcePath), sourceUrl: resourcePath, }; } exports.transformResourcePathToSolutionResource = transformResourcePathToSolutionResource; /** * Convert a resource path to a SolutionResourceType * * @param resourcePath String * @private */ function _getSolutionResourceTypeFromResourcePath(resourcePath) { let type = solution_resource_1.SolutionResourceType.resource; const folder = resourcePath.split("/").slice(0, -1).join("/") || ""; PATH_TO_TYPE_MAP.forEach((entry) => { if (folder.endsWith(entry.ending)) { type = entry.type; } }); return type; } exports._getSolutionResourceTypeFromResourcePath = _getSolutionResourceTypeFromResourcePath; /** * Mapping of strings that have been appended to the resource path to * encode the resource type */ const PATH_TO_TYPE_MAP = [ { ending: "_info_thumbnail", type: solution_resource_1.SolutionResourceType.thumbnail }, { ending: "_info_metadata", type: solution_resource_1.SolutionResourceType.metadata }, { ending: "_info_data", type: solution_resource_1.SolutionResourceType.data }, { ending: "_info_dataz", type: solution_resource_1.SolutionResourceType.fakezip }, { ending: "_info", type: solution_resource_1.SolutionResourceType.info }, ]; /** * Extract the filename from a path * * @param path String that is a path * @private */ function _extractFilenameFromResourcePath(path) { // if we have path separators, split and grab last segment if (path.indexOf("/") > -1) { return path.split("/").reverse()[0]; } else { // Hub resources are {guid}-{filename.ext} if (path.indexOf("-") === 32) { // start at 33 to remove the `-` between the filename and the guid return path.substr(33, path.length - 1); } else { // path is the filename return path; } } } exports._extractFilenameFromResourcePath = _extractFilenameFromResourcePath; /** * Convert the resource path into the correct output path * (aka resource prefix) * * Original resource paths encoded information in the path * including actual sub-folders. The logic is kinda complex * thus this function has extensive tests * * @param path * @private */ function _extractPathFromResourcePath(resourcePath) { // default case let path = ""; // parse up the path by splittins on `/` and dropping the last segment const parsedPath = resourcePath.split("/").slice(0, -1).join("/") || ""; // if we got something... if (parsedPath) { // now we need to see if this has any of the well-known endings const isWellKnownPath = PATH_TO_TYPE_MAP.reduce((acc, entry) => { if (parsedPath.endsWith(entry.ending)) { acc = true; } return acc; }, false); // if it does not match a well-known path... if (!isWellKnownPath && parsedPath.indexOf("http") !== 0) { // see if there is another folder encoded... if (parsedPath.indexOf("_") > -1) { path = parsedPath.split("_")[1]; } else { // if the path is not a naked guid, we return the parsed path if (!(0, hub_common_1.isGuid)(parsedPath)) { path = parsedPath; } } } } return path; } exports._extractPathFromResourcePath = _extractPathFromResourcePath; //# sourceMappingURL=transform-resource-paths-to-solution-resources.js.map