@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.45 kB
JavaScript
import { Config } from '../config';
import * as push from './cmd.push';
import { fs, log } from './common';
export async function run(args) {
const { cli } = args;
const config = await Config.create();
let manifest;
let versions = [];
log.info();
await cli
.task('pull manifest', async (e) => {
manifest = await config.manifest.local.ensureLatest({ minimal: true });
})
.task('pull versions', async (e) => {
versions = await config.s3.versions({ sort: 'DESC' });
})
.run({ concurrent: true });
if (!manifest) {
log.error('\nManifest could not be found.');
log.info.gray(config.manifest.s3.url);
return cli.exit(1);
}
log.info();
const sites = await promptForSites({ cli, manifest });
if (sites.length === 0) {
log.error(`The site named '${name}' does not exist in the manifest.`);
return cli.exit(1);
}
const current = sites.map(({ version, name }) => ({ version, name }));
const version = await promptForVersion({ cli, current, versions });
const s3 = config.s3;
const bundle = fs.join(s3.path.bundles, version);
const saveTo = config.manifest.local.path;
for (const site of sites) {
await manifest.change.site(site).bundle({ value: bundle, saveTo });
}
await push.manifest({ cli, config, silent: false });
log.info();
}
async function promptForSites(args) {
const { cli, manifest } = args;
let sites = manifest.sites.map((site) => site.name || 'Unnamed');
sites = ['all', ...sites];
const name = await cli.prompt.list({ message: 'site', items: sites });
if (name === 'all') {
return manifest.sites;
}
else {
const site = manifest.site.byName(name);
return site ? [site] : [];
}
}
async function promptForVersion(args) {
const { cli } = args;
const versions = args.versions.map((value) => {
const matches = args.current.filter((item) => item.version === value);
const current = matches.length === 0
? ''
: matches.length === 1
? matches[0].name
: matches.map((m) => m.name).join(',');
return {
name: `${value} ${matches.length > 0 ? `🌼 ${current.toUpperCase()}` : ''}`,
value,
};
});
return cli.prompt.list({
message: 'version',
items: [...versions, '---'],
});
}