@platform/react.ssr
Version:
A lightweight SSR (server-side-rendering) system for react apps bundled with ParcelJS and hosted on S3.
72 lines (71 loc) • 2.54 kB
JavaScript
import { bundler } from '../bundler';
import { Config } from '../config';
import { cli, fs, log } from './common';
import * as status from './cmd.status';
export async function run(args = {}) {
const { 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({ config, silent });
}
if (type === 'BUNDLE') {
await bundle({ config, silent, prompt: true });
}
if (!args.silent) {
log.info();
}
}
export async function bundle(args = {}) {
const { 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(s3).bundle({
bundleDir,
bucket,
bucketKey,
silent,
});
if (!args.silent) {
log.info();
}
return { version, bundleDir };
}
export async function manifest(args = {}) {
const { 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(s3.config).manifest({ bucket, source, target, silent });
if (!silent) {
const manifest = await config.manifest.s3.pull({ loadBundleManifest: true });
await status.print({ config, manifest });
}
}