UNPKG

@aws-amplify/cli-internal

Version:
82 lines 5.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GeoRenderer = void 0; const typescript_1 = __importDefault(require("typescript")); const ts_1 = require("../../ts"); const factory = typescript_1.default.factory; class GeoRenderer { render(maps, placeIndices, geofenceCollections) { const allResources = [...maps, ...placeIndices, ...geofenceCollections]; return factory.createNodeArray([ ...this.renderImports(allResources), this.renderBackendTypeImport(), ts_1.newLineIdentifier, this.renderDefineGeo(allResources, maps, placeIndices, geofenceCollections), ]); } renderImports(resources) { return resources.map((r) => { const functionName = `define${r.resourceName.charAt(0).toUpperCase()}${r.resourceName.slice(1)}`; return ts_1.TS.namedImport(`./${r.resourceName}/resource`, functionName); }); } renderBackendTypeImport() { return ts_1.TS.typeImport('../backend', 'Backend'); } renderDefineGeo(allResources, maps, placeIndices, geofenceCollections) { const functionAssignments = allResources.map((r) => { const functionName = `define${r.resourceName.charAt(0).toUpperCase()}${r.resourceName.slice(1)}`; return ts_1.TS.declareConst(r.resourceName, factory.createCallExpression(factory.createIdentifier(functionName), undefined, [factory.createIdentifier('backend')])); }); const addOutputStatement = this.buildAddOutputStatement(allResources, maps, placeIndices, geofenceCollections); return ts_1.TS.exportedFunction('defineGeo', [...functionAssignments, addOutputStatement]); } buildAddOutputStatement(allResources, maps, placeIndices, geofenceCollections) { const geoProps = []; const firstResource = allResources[0]; geoProps.push(factory.createPropertyAssignment(factory.createIdentifier('aws_region'), ts_1.TS.propAccess(firstResource.resourceName, 'region'))); if (maps.length > 0) geoProps.push(this.buildMapsSection(maps)); if (placeIndices.length > 0) geoProps.push(this.buildSearchIndicesSection(placeIndices)); if (geofenceCollections.length > 0) geoProps.push(this.buildGeofenceCollectionsSection(geofenceCollections)); return factory.createExpressionStatement(factory.createCallExpression(ts_1.TS.propAccess('backend', 'addOutput'), undefined, [ factory.createObjectLiteralExpression([factory.createPropertyAssignment(factory.createIdentifier('geo'), factory.createObjectLiteralExpression(geoProps, true))], true), ])); } buildMapsSection(maps) { var _a; const mapItems = maps.map((m) => factory.createPropertyAssignment(factory.createComputedPropertyName(ts_1.TS.propAccess(m.resourceName, 'name')), factory.createObjectLiteralExpression([ factory.createPropertyAssignment(factory.createIdentifier('style'), ts_1.TS.propAccess(m.resourceName, 'style')), ]))); const defaultMap = (_a = maps.find((m) => m.isDefault === 'true')) !== null && _a !== void 0 ? _a : maps[0]; return factory.createPropertyAssignment(factory.createIdentifier('maps'), factory.createObjectLiteralExpression([ factory.createPropertyAssignment(factory.createIdentifier('items'), factory.createObjectLiteralExpression(mapItems, true)), factory.createPropertyAssignment(factory.createIdentifier('default'), ts_1.TS.propAccess(defaultMap.resourceName, 'name')), ], true)); } buildSearchIndicesSection(placeIndices) { var _a; const indexItems = placeIndices.map((p) => ts_1.TS.propAccess(p.resourceName, 'name')); const defaultIndex = (_a = placeIndices.find((p) => p.isDefault === 'true')) !== null && _a !== void 0 ? _a : placeIndices[0]; return factory.createPropertyAssignment(factory.createIdentifier('search_indices'), factory.createObjectLiteralExpression([ factory.createPropertyAssignment(factory.createIdentifier('items'), factory.createArrayLiteralExpression(indexItems)), factory.createPropertyAssignment(factory.createIdentifier('default'), ts_1.TS.propAccess(defaultIndex.resourceName, 'name')), ], true)); } buildGeofenceCollectionsSection(geofenceCollections) { var _a; const collectionItems = geofenceCollections.map((g) => ts_1.TS.propAccess(g.resourceName, 'name')); const defaultCollection = (_a = geofenceCollections.find((g) => g.isDefault === 'true')) !== null && _a !== void 0 ? _a : geofenceCollections[0]; return factory.createPropertyAssignment(factory.createIdentifier('geofence_collections'), factory.createObjectLiteralExpression([ factory.createPropertyAssignment(factory.createIdentifier('items'), factory.createArrayLiteralExpression(collectionItems)), factory.createPropertyAssignment(factory.createIdentifier('default'), ts_1.TS.propAccess(defaultCollection.resourceName, 'name')), ], true)); } } exports.GeoRenderer = GeoRenderer; //# sourceMappingURL=geo.renderer.js.map