@magile/nx-distributed-cache
Version:
Distributed Cache For Your NX build. Optimize your build time by caching the artifacts across multiple devices.
227 lines (226 loc) • 10.4 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
exports.__esModule = true;
var AWS = require('aws-sdk');
var path = require("path");
var fs = require('fs');
var S3Adapter = /** @class */ (function () {
function S3Adapter(options) {
this.isReady = true;
this.files = [];
this.options = Object.assign(options, process.env);
if (!this.options.bucketName) {
this.isReady = false;
}
else {
var awsConfiguration = {};
if (!!this.options.accessKeyId && !!this.options.secretAccessKey) {
awsConfiguration = {
accessKeyId: this.options.accessKeyId,
secretAccessKey: this.options.secretAccessKey
};
}
else {
console.warn('No AccessKey and SecretAccessKey passed. Implicit authorization from AWS is used.');
}
if (!!this.options.endpoint) {
awsConfiguration.endpoint = this.options.endpoint;
console.info('Overriding S3 endpoint to ' + awsConfiguration.endpoint);
}
if (!!this.options.region) {
awsConfiguration.region = this.options.region;
console.info('Region is set to ' + awsConfiguration.region);
}
this.S3 = new AWS.S3(awsConfiguration);
}
}
S3Adapter.prototype.uploadFile = function (fileName, targetPath) {
var _this = this;
fs.readFile(fileName, function (err, data) {
if (err) {
throw err;
}
var params = {
Bucket: _this.options.bucketName,
Key: targetPath,
Body: data
};
_this.S3.upload(params, function (s3Err, data) {
if (s3Err) {
throw s3Err;
}
});
});
};
;
S3Adapter.prototype.getFile = function (directory) {
var _this = this;
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
this.S3.getObject({
Bucket: this.options.bucketName,
Key: directory
}, function (err, data) {
if (err) {
return reject(err);
}
resolve(data.Body);
});
return [2 /*return*/];
});
}); });
};
S3Adapter.prototype.fileExists = function (fileName) {
return __awaiter(this, void 0, void 0, function () {
var files;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.listFiles(fileName)];
case 1:
files = _a.sent();
return [2 /*return*/, files.length > 0];
}
});
});
};
S3Adapter.prototype.listFiles = function (directory) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
this.S3.listObjectsV2({
Bucket: this.options.bucketName,
Prefix: directory + '/'
}, function (err, data) {
if (err) {
console.error('Unable to fetch from AWS S3.listObjectsV2.', err);
return reject(null);
}
resolve(data.Contents.map(function (e) { return ({ key: e.Key, size: e.Size }); }));
});
return [2 /*return*/];
});
}); })];
});
});
};
S3Adapter.prototype.downloadDirectory = function (s3path, localPath) {
return __awaiter(this, void 0, void 0, function () {
var files, size, _i, files_1, file, _a, files_2, file, content, writeFile, writePath;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.listFiles(s3path)];
case 1:
files = _b.sent();
size = 0;
for (_i = 0, files_1 = files; _i < files_1.length; _i++) {
file = files_1[_i];
size += file.size;
}
console.log("Downloading " + files.length + " files (" + (size / 1024).toFixed(1) + "kb) from distributed cache...");
_a = 0, files_2 = files;
_b.label = 2;
case 2:
if (!(_a < files_2.length)) return [3 /*break*/, 5];
file = files_2[_a];
return [4 /*yield*/, this.getFile(file.key)];
case 3:
content = _b.sent();
writeFile = localPath + '/' + file.key;
writePath = writeFile.substring(0, writeFile.lastIndexOf('/'));
try {
fs.mkdirSync(writePath, { recursive: true });
fs.writeFileSync(writeFile, content);
}
catch (e) {
console.log(e);
}
_b.label = 4;
case 4:
_a++;
return [3 /*break*/, 2];
case 5: return [2 /*return*/];
}
});
});
};
S3Adapter.prototype.directoryList = function (Directory) {
var _this = this;
fs.readdirSync(Directory).forEach(function (File) {
var Absolute = path.join(Directory, File);
if (fs.statSync(Absolute).isDirectory()) {
return _this.directoryList(Absolute);
}
else {
return _this.files.push(Absolute);
}
});
};
S3Adapter.prototype.uploadDir = function (s3Path, hash) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
var uploads;
return __generator(this, function (_a) {
this.files = [];
this.directoryList(s3Path);
uploads = this.files.map(function (filePath) {
var bucketPath = filePath.substring(s3Path.length + 1);
return { Bucket: _this.options.bucketName + '/' + hash, Key: bucketPath, Body: fs.readFileSync(filePath) };
});
console.log('Files found', uploads.length);
return [2 /*return*/, Promise.all(uploads.map(function (upload) {
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
this.S3.putObject(upload, function (err, data) {
if (err) {
console.log(err);
reject(err);
}
resolve(upload.Key);
});
return [2 /*return*/];
});
}); });
}))];
});
});
};
;
return S3Adapter;
}());
exports.S3Adapter = S3Adapter;