@platform/react.ssr
Version:
A lightweight SSR (server-side-rendering) system for react apps bundled with ParcelJS and hosted on S3.
211 lines (210 loc) • 9.71 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = void 0;
var tslib_1 = require("tslib");
var bundler_1 = require("../bundler");
var config_1 = require("../config");
var common_1 = require("./common");
var push = require("./cmd.push");
function run(args) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var cli, config, _a, endpoint, manifest, pkgPath, pkg, version, _b, bundleDir, isPush, _c, tasks, res;
var _this = this;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
cli = args.cli;
_a = args.config;
if (_a) return [3, 2];
return [4, config_1.Config.create()];
case 1:
_a = (_d.sent());
_d.label = 2;
case 2:
config = _a;
endpoint = config.s3.endpoint;
return [4, common_1.fs.ensureDir(config.builder.bundles)];
case 3:
_d.sent();
return [4, common_1.fs.ancestor(config.builder.bundles).first('package.json')];
case 4:
pkgPath = _d.sent();
pkg = common_1.npm.pkg(pkgPath);
common_1.log.info.gray(common_1.fs.dirname(pkgPath));
common_1.log.info();
_b = args.version;
if (_b) return [3, 6];
return [4, common_1.npm.prompt.incrementVersion({ path: pkgPath, noChange: true, save: true })];
case 5:
_b = (_d.sent());
_d.label = 6;
case 6:
version = _b;
bundleDir = common_1.fs.resolve(common_1.fs.join(config.builder.bundles, version));
if (!(args.push !== undefined)) return [3, 7];
_c = args.push;
return [3, 9];
case 7: return [4, cli.prompt.list({ message: 'push to S3', items: ['yes', 'no'] })];
case 8:
_c = (_d.sent()) === 'yes';
_d.label = 9;
case 9:
isPush = _c;
if (isPush && !endpoint) {
throw new Error("The S3 endpoint has not been configured in [ssr.yml].");
}
common_1.log.info();
tasks = cli
.task('build', function (e) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
return [2, execScript(pkg, e, 'build')];
}); }); }, { skip: args.manifest })
.task('bundle', function (e) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
return [2, execScript(pkg, e, 'bundle')];
}); }); }, { skip: args.manifest })
.task('manifest', function (e) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var _a, entries, error, res_1;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4, getEntries(config)];
case 1:
_a = _b.sent(), entries = _a.entries, error = _a.error;
if (!error) return [3, 2];
throw error;
case 2: return [4, bundler_1.bundler.prepare({ bundleDir: bundleDir, entries: entries, silent: true })];
case 3:
res_1 = _b.sent();
manifest = res_1.manifest;
_b.label = 4;
case 4: return [2];
}
});
}); });
return [4, tasks.run({ concurrent: false, exitOnError: true })];
case 10:
res = _d.sent();
if (!res.ok) {
common_1.log.info();
res.errors.forEach(function (item) {
common_1.log.error("ERROR " + item.title);
common_1.log.warn(item.error);
common_1.log.info();
});
return [2, cli.exit(1)];
}
if (!isPush) return [3, 12];
return [4, push.bundle({ cli: cli, config: config, version: version })];
case 11:
_d.sent();
return [3, 13];
case 12:
if (manifest) {
bundler_1.bundler.log.bundle({ bundleDir: bundleDir, manifest: manifest });
}
_d.label = 13;
case 13:
common_1.log.info();
return [2];
}
});
});
}
exports.run = run;
function execScript(pkg, e, scriptName) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var exists, cwd, res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
exists = Boolean(pkg.scripts.bundle);
if (!exists) {
e.error("Package.json does not have a \"" + scriptName + "\" script.");
return [2];
}
cwd = pkg.dir;
return [4, common_1.exec.command("yarn " + scriptName).run({ cwd: cwd, silent: true })];
case 1:
res = _a.sent();
if (!res.ok) {
throw new Error("Failed while executing '" + scriptName + "' script of package '" + pkg.name + "'");
}
return [2];
}
});
});
}
var getRootDir = function (source) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var path;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
path = '';
return [4, common_1.fs.ancestor(source).walk(function (e) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, common_1.fs.readdir(e.dir)];
case 1:
if ((_a.sent()).includes('package.json')) {
return [2, e.stop()];
}
else {
path = e.dir;
}
return [2];
}
});
}); })];
case 1:
_a.sent();
return [2, path];
}
});
}); };
var getEntries = function (config) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var done, source, sourceRoot, sourcePath, localRoot, localPath, err, res, error_1;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
done = function (entries, errorMessage) {
var error = errorMessage ? new Error(errorMessage) : undefined;
return { ok: !Boolean(error), entries: entries, error: error };
};
source = config.builder.entries;
if (!source) {
return [2, done([])];
}
source = common_1.fs.resolve(source);
return [4, getRootDir(source)];
case 1:
sourceRoot = _a.sent();
sourcePath = source.substring(sourceRoot.length);
localRoot = common_1.fs.resolve("tmp/" + common_1.fs.basename(sourceRoot));
localPath = common_1.fs.join(localRoot, sourcePath);
return [4, common_1.fs.ensureDir(common_1.fs.dirname(localRoot))];
case 2:
_a.sent();
return [4, common_1.fs.remove(localRoot)];
case 3:
_a.sent();
return [4, common_1.fs.copy(sourceRoot, localRoot)];
case 4:
_a.sent();
err = "Failed to load bundle entries at path: " + source + ".";
_a.label = 5;
case 5:
_a.trys.push([5, 6, 7, 9]);
res = require(localPath);
if (Array.isArray(res.default)) {
return [2, done(res.default)];
}
return [2, done([], err + " Ensure the array is exported as the module default.")];
case 6:
error_1 = _a.sent();
return [2, done([], err + " " + error_1.message)];
case 7: return [4, common_1.fs.remove(common_1.fs.resolve('tmp'))];
case 8:
_a.sent();
return [7];
case 9: return [2];
}
});
}); };