@platform/react.ssr
Version:
A lightweight SSR (server-side-rendering) system for react apps bundled with ParcelJS and hosted on S3.
153 lines (152 loc) • 7.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var common_1 = require("../common");
function push(args) {
var s3 = common_1.fs.s3(args);
var cli = args.cli;
return {
bundle: function (args) { return bundle(tslib_1.__assign(tslib_1.__assign({}, args), { s3: s3, cli: cli })); },
manifest: function (args) { return manifest(tslib_1.__assign(tslib_1.__assign({}, args), { s3: s3, cli: cli })); },
};
}
exports.push = push;
function bundle(args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var cli, bucket, bundleDir, bucketKey, files, items, formatPath, version, size, endpoint, dirSize, renderer, tasks, error_1;
var _this = this;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
cli = args.cli;
bucket = args.s3.bucket(args.bucket);
bundleDir = common_1.fs.resolve(args.bundleDir);
bucketKey = args.bucketKey.replace(new RegExp("^" + args.bucket + "/"), '');
return [4, common_1.fs.glob.find(common_1.fs.join(bundleDir, '**'))];
case 1:
files = _a.sent();
items = files.map(function (source) {
var key = common_1.fs.join(bucketKey, source.substring(bundleDir.length + 1));
return { source: source, key: key };
});
if (!!args.silent) return [3, 3];
formatPath = common_1.util.formatPath;
version = common_1.fs.basename(bundleDir);
return [4, common_1.fs.size.dir(bundleDir)];
case 2:
size = _a.sent();
endpoint = common_1.fs.join(bucket.endpoint, args.bucket, args.bucketKey);
common_1.log.info();
common_1.log.info.cyan("Pushing bundle " + common_1.log.white(version) + " to S3 \u261D");
common_1.log.info();
common_1.log.info.gray(" size: " + common_1.log.magenta(size.toString()));
common_1.log.info.gray(" from: " + formatPath(bundleDir));
common_1.log.info.gray(" to: " + formatPath(endpoint));
common_1.log.info();
_a.label = 3;
case 3: return [4, common_1.fs.size.dir(bundleDir)];
case 4:
dirSize = _a.sent();
renderer = args.silent ? 'silent' : undefined;
tasks = new common_1.Listr(items.map(function (item) {
var source = item.source, key = item.key;
var file = common_1.fs.basename(key);
var fileSize = dirSize.files.find(function (item) { return item.path.endsWith("/" + file); });
var size = fileSize ? fileSize.toString({ round: 0, spacer: '' }) : '';
size = (size + " ").substring(0, 8);
return {
title: size + " " + file,
task: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var data, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, common_1.fs.readFile(source)];
case 1:
data = _a.sent();
return [4, bucket.put({ data: data, key: key, acl: 'public-read' })];
case 2:
res = _a.sent();
if (!res.ok) {
throw res.error;
}
return [2];
}
});
}); },
};
}), { concurrent: true, renderer: renderer, exitOnError: false });
_a.label = 5;
case 5:
_a.trys.push([5, 7, , 8]);
return [4, tasks.run()];
case 6:
_a.sent();
return [3, 8];
case 7:
error_1 = _a.sent();
common_1.log.error("\nFailed while pushing to S3.\n");
cli.exit(1);
return [3, 8];
case 8: return [2, {
bucket: args.bucket,
endpoint: bucket.endpoint,
s3: items,
manifest: manifest,
}];
}
});
});
}
exports.bundle = bundle;
function manifest(args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var cli, silent, source, bucket, filename, target, formatPath, size, endpoint, title, tasks;
var _this = this;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
cli = args.cli, silent = args.silent;
source = common_1.fs.resolve(args.source);
bucket = args.s3.bucket(args.bucket);
filename = common_1.fs.basename(source);
target = args.target;
target = target.endsWith(filename) ? target : common_1.fs.join(target, filename);
if (!!args.silent) return [3, 2];
formatPath = common_1.util.formatPath;
return [4, common_1.fs.size.file(source)];
case 1:
size = _a.sent();
endpoint = common_1.fs.join(bucket.endpoint, args.bucket, target);
common_1.log.info();
common_1.log.info.cyan("Pushing manifest to S3 \u261D");
common_1.log.info();
common_1.log.info.gray(" size: " + common_1.log.magenta(size.toString()));
common_1.log.info.gray(" from: " + formatPath(source));
common_1.log.info.gray(" to: " + formatPath(endpoint));
common_1.log.info();
_a.label = 2;
case 2:
title = "push " + common_1.fs.basename(source);
tasks = cli.task(title, function (e) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var data;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, common_1.fs.readFile(source)];
case 1:
data = _a.sent();
return [4, bucket.put({ data: data, key: target, acl: 'public-read' })];
case 2:
_a.sent();
return [2];
}
});
}); });
return [4, tasks.run({ silent: silent })];
case 3:
_a.sent();
return [2, { manifest: manifest }];
}
});
});
}
exports.manifest = manifest;