UNPKG

@jefiozie/ngx-aws-deploy

Version:

Deploy an Angular app to Amazon S3 directly from the Angular CLI

88 lines 3.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Uploader = void 0; const tslib_1 = require("tslib"); const AWS = require("aws-sdk"); const mimeTypes = require("mime-types"); const fs = require("fs"); const path = require("path"); const config_1 = require("./config"); class Uploader { constructor(context, builderConfig) { this._context = context; this._builderConfig = builderConfig; this._bucket = (0, config_1.getBucket)(this._builderConfig); this._region = (0, config_1.getRegion)(this._builderConfig); this._subFolder = (0, config_1.getSubFolder)(this._builderConfig); AWS.config.update({ region: this._region }); this._s3 = new AWS.S3({ apiVersion: 'latest', s3ForcePathStyle: (0, config_1.gets3ForcePathStyle)() || false, endpoint: (0, config_1.getAwsEndpoint)(), credentials: new AWS.Credentials({ secretAccessKey: (0, config_1.getSecretAccessKey)(), accessKeyId: (0, config_1.getAccessKeyId)(), sessionToken: (0, config_1.getSessionToken)(), }), }); } upload(files, filesPath) { return tslib_1.__awaiter(this, void 0, void 0, function* () { try { if (!this._region || !this._bucket) { this._context.logger.error(`❌ Looks like you are missing some upload configuration (need region, bucket)`); return false; } const params = { Bucket: this._bucket, }; yield this._s3 .headBucket(params) .promise() .then(() => { return this.uploadFiles(files, filesPath); }) .catch((error) => { this._context.logger.error(`❌ The following error was found during the upload ${error}`); throw error; }); } catch (_a) { return false; } return true; }); } uploadFiles(files, filesPath) { return Promise.all(files.map((file) => tslib_1.__awaiter(this, void 0, void 0, function* () { yield this.uploadFile(path.join(filesPath, file), file); }))); } uploadFile(localFilePath, originFilePath) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const fileName = path.basename(localFilePath); const body = fs.createReadStream(localFilePath); body.on('error', function (err) { console.log('File Error', err); }); const params = { Bucket: this._bucket, Key: this._subFolder ? `${this._subFolder}/${originFilePath}` : originFilePath, Body: body, ContentType: mimeTypes.lookup(fileName) || undefined, }; yield this._s3 .upload(params) .promise() .then((file) => this._context.logger.info(`Uploaded file "${file.Key}" to ${file.Location}`)) .catch((item) => { this._context.logger.error(`Error uploading file: ${item}`); throw item; }); }); } } exports.Uploader = Uploader; //# sourceMappingURL=uploader.js.map