add --help to CLI

This commit is contained in:
Rich Harris
2018-02-13 17:12:55 -05:00
parent 2be9dd1883
commit 55b60369f9
5 changed files with 81 additions and 5 deletions

16
src/cli/help.md Normal file
View File

@@ -0,0 +1,16 @@
# sapper v<@version@>
https://sapper.svelte.technology
> sapper build
Creates a production-ready version of your app
> sapper export
If possible, exports your app as static files, suitable for hosting on
services like Netlify or Surge
> sapper --help
Shows this message

View File

@@ -1,15 +1,34 @@
import mri from 'mri';
import chalk from 'chalk';
import help from './help.md';
import build from './build.js';
import exporter from './export.js';
import { dest, entry, isDev, src } from '../config';
import { dest, entry, src } from '../config';
import * as pkg from '../../package.json';
process.env.NODE_ENV = 'production';
const opts = mri(process.argv.slice(2), {
alias: {
h: 'help'
}
});
if (opts.help) {
const rendered = help
.replace('<@version@>', pkg.version)
.replace(/^(.+)/gm, (m: string, $1: string) => /[#>]/.test(m) ? $1 : ` ${$1}`)
.replace(/^# (.+)/gm, (m: string, $1: string) => chalk.bold.underline($1))
.replace(/^> (.+)/gm, (m: string, $1: string) => chalk.cyan($1));
console.log(`\n${rendered}\n`);
process.exit(0);
}
const [cmd] = opts._;
const cmd = process.argv[2];
const start = Date.now();
const dev = isDev();
if (cmd === 'build') {
build({ dest, dev, entry, src })
build({ dest, dev: false, entry, src })
.then(() => {
const elapsed = Date.now() - start;
console.error(`built in ${elapsed}ms`); // TODO beautify this, e.g. 'built in 4.7 seconds'