@aws-amplify/cli-internal
Version:
Amplify CLI
87 lines • 3.95 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.S3Generator = void 0;
const node_path_1 = __importDefault(require("node:path"));
const promises_1 = __importDefault(require("node:fs/promises"));
const ts_1 = require("../../ts");
const s3_renderer_1 = require("./s3.renderer");
const PERMISSION_MAP = {
READ: ['read'],
DELETE: ['delete'],
CREATE_AND_UPDATE: ['write'],
};
class S3Generator {
constructor(gen1App, backendGenerator, outputDir, resource, logger) {
this.functionAccess = [];
this.triggers = {};
this.gen1App = gen1App;
this.backendGenerator = backendGenerator;
this.outputDir = outputDir;
this.resource = resource;
this.renderer = new s3_renderer_1.S3Renderer();
this.logger = logger;
}
addFunctionAccess(functionName, permissions) {
this.functionAccess.push({ functionName, permissions });
}
addTrigger(event, functionName) {
this.triggers[event] = functionName;
}
async plan() {
const bucketName = this.gen1App.resourceMetaOutput(this.resource, 'BucketName');
this.logger.debug(`Fetching S3 bucket config '${bucketName}'`);
const [accelerateStatus, versioningStatus, encryption] = await Promise.all([
this.gen1App.aws.fetchBucketAccelerate(bucketName),
this.gen1App.aws.fetchBucketVersioning(bucketName),
this.gen1App.aws.fetchBucketEncryption(bucketName),
]);
const storageDir = node_path_1.default.join(this.outputDir, 'amplify', 'storage');
return [
{
resource: this.resource,
validate: () => undefined,
describe: async () => ['Generate amplify/storage/resource.ts'],
execute: async () => {
this.logger.info('Rendering storage/resource.ts');
const nodes = this.renderer.render({
name: bucketName,
access: this.buildAccessPatterns(),
triggers: this.triggers,
bucketName,
accelerateStatus,
versioningStatus,
encryption,
});
const content = ts_1.TS.printNodes(nodes);
await promises_1.default.mkdir(storageDir, { recursive: true });
await promises_1.default.writeFile(node_path_1.default.join(storageDir, 'resource.ts'), content, 'utf-8');
this.backendGenerator.addNamespaceImport('storage', './storage/resource');
this.backendGenerator.addDefineBackendEntry('storage', 'storage', 'storage');
this.backendGenerator.addApplyEscapeHatchesCall({ alias: 'storage', extraArgs: [] });
this.backendGenerator.addPostRefactorCall('storage.postRefactor(backend);');
},
},
];
}
buildAccessPatterns() {
const cliInputs = this.gen1App.cliInputs('storage', this.resource.resourceName);
let groups;
if (cliInputs.groupAccess && Object.keys(cliInputs.groupAccess).length > 0) {
groups = Object.entries(cliInputs.groupAccess).reduce((acc, [key, value]) => {
acc[key] = value.flatMap((p) => PERMISSION_MAP[p]);
return acc;
}, {});
}
return {
guest: cliInputs.guestAccess.flatMap((p) => PERMISSION_MAP[p]),
auth: cliInputs.authAccess.flatMap((p) => PERMISSION_MAP[p]),
groups,
functions: this.functionAccess.length > 0 ? this.functionAccess : undefined,
};
}
}
exports.S3Generator = S3Generator;
//# sourceMappingURL=s3.generator.js.map