@platform/react.ssr
Version:
A lightweight SSR (server-side-rendering) system for react apps bundled with ParcelJS and hosted on S3.
167 lines (166 loc) • 7.43 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var bundler_1 = require("../bundler");
var config_1 = require("../config");
var common_1 = require("./common");
var status = require("./cmd.status");
function run(args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var cli, silent, config, type;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
cli = args.cli, silent = args.silent;
return [4, config_1.Config.create()];
case 1:
config = _a.sent();
type = args.type;
if (!(type === undefined)) return [3, 3];
return [4, cli.prompt.list({
message: 'type',
items: [
{ name: 'bundle', value: 'BUNDLE' },
{ name: 'manifest', value: 'MANIFEST' },
],
})];
case 2:
type = _a.sent();
_a.label = 3;
case 3:
if (!(type === 'MANIFEST')) return [3, 5];
return [4, manifest({ cli: cli, config: config, silent: silent })];
case 4:
_a.sent();
_a.label = 5;
case 5:
if (!(type === 'BUNDLE')) return [3, 7];
return [4, bundle({ cli: cli, config: config, silent: silent, prompt: true })];
case 6:
_a.sent();
_a.label = 7;
case 7:
if (!args.silent) {
common_1.log.info();
}
return [2];
}
});
});
}
exports.run = run;
function bundle(args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var cli, silent, config, _a, bundlesDir, promptForVersion, version, _b, _c, _d, _e, _f, endpoint, accessKey, secret, bucket, s3, bucketKey, bundleDir;
var _this = this;
return tslib_1.__generator(this, function (_g) {
switch (_g.label) {
case 0:
cli = args.cli, silent = args.silent;
_a = args.config;
if (_a) return [3, 2];
return [4, config_1.Config.create()];
case 1:
_a = (_g.sent());
_g.label = 2;
case 2:
config = _a;
bundlesDir = config.builder.bundles;
promptForVersion = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var paths, versions, items, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, bundler_1.bundler.dir(bundlesDir).semver()];
case 1:
paths = _a.sent();
versions = paths.map(function (path) { return common_1.fs.basename(path); }).reverse();
items = tslib_1.__spreadArrays(versions, ['---']);
return [4, cli.prompt.list({ message: 'bundle version', items: items })];
case 2:
res = _a.sent();
return [2, res];
}
});
}); };
if (!args.prompt) return [3, 4];
return [4, promptForVersion()];
case 3:
_b = _g.sent();
return [3, 7];
case 4:
_c = args.version;
if (_c) return [3, 6];
_e = (_d = common_1.fs).basename;
return [4, bundler_1.bundler.dir(bundlesDir).latest()];
case 5:
_c = _e.apply(_d, [_g.sent()]);
_g.label = 6;
case 6:
_b = _c;
_g.label = 7;
case 7:
version = _b;
_f = config.s3, endpoint = _f.endpoint, accessKey = _f.accessKey, secret = _f.secret, bucket = _f.bucket;
s3 = { endpoint: endpoint, accessKey: accessKey, secret: secret };
bucketKey = common_1.fs.join(config.s3.path.base, config.s3.path.bundles, version);
bundleDir = common_1.fs.resolve(common_1.fs.join(bundlesDir, version));
return [4, common_1.fs.pathExists(bundleDir)];
case 8:
if (!(_g.sent())) {
common_1.log.error("\nBundle does not exist.");
common_1.log.info.gray(bundleDir);
cli.exit(1);
}
return [4, bundler_1.bundler.push(tslib_1.__assign(tslib_1.__assign({}, s3), { cli: cli })).bundle({
bundleDir: bundleDir,
bucket: bucket,
bucketKey: bucketKey,
silent: silent,
})];
case 9:
_g.sent();
if (!args.silent) {
common_1.log.info();
}
return [2, { version: version, bundleDir: bundleDir }];
}
});
});
}
exports.bundle = bundle;
function manifest(args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var cli, silent, config, _a, s3, bucket, source, target, manifest_1;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
cli = args.cli, silent = args.silent;
_a = args.config;
if (_a) return [3, 2];
return [4, config_1.Config.create()];
case 1:
_a = (_b.sent());
_b.label = 2;
case 2:
config = _a;
s3 = config.s3;
bucket = s3.bucket;
source = config.manifest.local.path;
target = common_1.fs.join(s3.path.base, s3.path.manifest);
return [4, bundler_1.bundler.push(tslib_1.__assign(tslib_1.__assign({}, s3.config), { cli: cli })).manifest({ bucket: bucket, source: source, target: target, silent: silent })];
case 3:
_b.sent();
if (!!silent) return [3, 6];
return [4, config.manifest.s3.pull({ loadBundleManifest: true })];
case 4:
manifest_1 = _b.sent();
return [4, status.print({ config: config, manifest: manifest_1 })];
case 5:
_b.sent();
_b.label = 6;
case 6: return [2];
}
});
});
}
exports.manifest = manifest;