@platform/react.ssr
Version:
A lightweight SSR (server-side-rendering) system for react apps bundled with ParcelJS and hosted on S3.
75 lines (74 loc) • 2.67 kB
JavaScript
import { bundler } from '../bundler';
import { Config } from '../config';
import { fs, log } from './common';
import * as status from './cmd.status';
export async function run(args) {
const { cli, silent } = args;
const config = await Config.create();
let type = args.type;
if (type === undefined) {
type = await cli.prompt.list({
message: 'type',
items: [
{ name: 'bundle', value: 'BUNDLE' },
{ name: 'manifest', value: 'MANIFEST' },
],
});
}
if (type === 'MANIFEST') {
await manifest({ cli, config, silent });
}
if (type === 'BUNDLE') {
await bundle({ cli, config, silent, prompt: true });
}
if (!args.silent) {
log.info();
}
}
export async function bundle(args) {
const { cli, silent } = args;
const config = args.config || (await Config.create());
const bundlesDir = config.builder.bundles;
const promptForVersion = async () => {
const paths = await bundler.dir(bundlesDir).semver();
const versions = paths.map((path) => fs.basename(path)).reverse();
const items = [...versions, '---'];
const res = await cli.prompt.list({ message: 'bundle version', items });
return res;
};
const version = args.prompt
? await promptForVersion()
: args.version || fs.basename(await bundler.dir(bundlesDir).latest());
const { endpoint, accessKey, secret, bucket } = config.s3;
const s3 = { endpoint, accessKey, secret };
const bucketKey = fs.join(config.s3.path.base, config.s3.path.bundles, version);
const bundleDir = fs.resolve(fs.join(bundlesDir, version));
if (!(await fs.pathExists(bundleDir))) {
log.error(`\nBundle does not exist.`);
log.info.gray(bundleDir);
cli.exit(1);
}
await bundler.push(Object.assign(Object.assign({}, s3), { cli })).bundle({
bundleDir,
bucket,
bucketKey,
silent,
});
if (!args.silent) {
log.info();
}
return { version, bundleDir };
}
export async function manifest(args) {
const { cli, silent } = args;
const config = args.config || (await Config.create());
const s3 = config.s3;
const bucket = s3.bucket;
const source = config.manifest.local.path;
const target = fs.join(s3.path.base, s3.path.manifest);
await bundler.push(Object.assign(Object.assign({}, s3.config), { cli })).manifest({ bucket, source, target, silent });
if (!silent) {
const manifest = await config.manifest.s3.pull({ loadBundleManifest: true });
await status.print({ config, manifest });
}
}