UNPKG

@aws-amplify/cli-internal

Version:
91 lines 4.66 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReferenceAuthGenerator = void 0; const node_path_1 = __importDefault(require("node:path")); const promises_1 = __importDefault(require("node:fs/promises")); const amplify_cli_core_1 = require("@aws-amplify/amplify-cli-core"); const ts_1 = require("../../ts"); const reference_auth_renderer_1 = require("./reference-auth.renderer"); class ReferenceAuthGenerator { constructor(gen1App, backendGenerator, outputDir, resource, logger) { this.renderer = new reference_auth_renderer_1.ReferenceAuthRenderer(); this.gen1App = gen1App; this.backendGenerator = backendGenerator; this.outputDir = outputDir; this.resource = resource; this.logger = logger; } async plan() { const authCategory = this.gen1App.categoryMeta('auth'); if (!authCategory) { throw new amplify_cli_core_1.AmplifyFault('AuthCategoryFault', { message: 'Auth category not found in amplify-meta.json — ReferenceAuthGenerator should only be created when auth exists', }); } const referenceAuth = await this.buildReferenceAuth(authCategory); if (!referenceAuth) { throw new amplify_cli_core_1.AmplifyFault('AuthCategoryFault', { message: 'Auth category exists but no imported auth resource found — ReferenceAuthGenerator should only be created for imported auth', }); } const authDir = node_path_1.default.join(this.outputDir, 'amplify', 'auth'); return [ { resource: this.resource, validate: () => undefined, describe: async () => ['Generate amplify/auth/resource.ts (reference auth)'], execute: async () => { this.logger.info('Rendering auth/resource.ts (reference auth)'); const nodes = this.renderer.render(referenceAuth); const content = ts_1.TS.printNodes(nodes); await promises_1.default.mkdir(authDir, { recursive: true }); await promises_1.default.writeFile(node_path_1.default.join(authDir, 'resource.ts'), content, 'utf-8'); this.backendGenerator.addNamespaceImport('auth', './auth/resource'); this.backendGenerator.addDefineBackendEntry('auth', 'auth', 'auth'); }, }, ]; } async buildReferenceAuth(authCategory) { const isImported = Object.values(authCategory).some((value) => typeof value === 'object' && value !== null && 'serviceType' in value && value.serviceType === 'imported'); if (!isImported) return undefined; const firstAuth = Object.values(authCategory)[0]; const output = firstAuth === null || firstAuth === void 0 ? void 0 : firstAuth.output; const userPoolId = output === null || output === void 0 ? void 0 : output.UserPoolId; const userPoolClientId = output === null || output === void 0 ? void 0 : output.AppClientIDWeb; const identityPoolId = output === null || output === void 0 ? void 0 : output.IdentityPoolId; if (!userPoolId && !userPoolClientId && !identityPoolId) { throw new amplify_cli_core_1.AmplifyError('AuthImportError', { message: 'No user pool or identity pool found for import.', resolution: 'Verify the imported auth resource has valid User Pool or Identity Pool configuration in amplify-meta.json.', }); } let roles; if (identityPoolId) { this.logger.debug(`Fetching identity pool roles for '${identityPoolId}'`); roles = await this.gen1App.aws.fetchIdentityPoolRoles(identityPoolId); } let groups; if (userPoolId) { this.logger.debug(`Fetching user pool groups for '${userPoolId}'`); groups = await this.gen1App.aws.fetchGroupsByUserPoolId(userPoolId); } return { userPoolId, userPoolClientId, identityPoolId, unauthRoleArn: roles === null || roles === void 0 ? void 0 : roles.unauthenticated, authRoleArn: roles === null || roles === void 0 ? void 0 : roles.authenticated, groups, }; } } exports.ReferenceAuthGenerator = ReferenceAuthGenerator; //# sourceMappingURL=reference-auth.generator.js.map