mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-12 19:25:10 +00:00
98 lines
3.0 KiB
TypeScript
Executable File
98 lines
3.0 KiB
TypeScript
Executable File
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import * as child_process from 'child_process';
|
|
import sade from 'sade';
|
|
import * as colors from 'ansi-colors';
|
|
import prettyMs from 'pretty-ms';
|
|
// import upgrade from './cli/upgrade';
|
|
import * as ports from 'port-authority';
|
|
import * as pkg from '../package.json';
|
|
|
|
const prog = sade('sapper').version(pkg.version);
|
|
|
|
prog.command('dev')
|
|
.describe('Start a development server')
|
|
.option('-p, --port', 'Specify a port')
|
|
.option('-o, --open', 'Open a browser window')
|
|
.action(async (opts: { port: number, open: boolean }) => {
|
|
const { dev } = await import('./cli/dev');
|
|
dev(opts);
|
|
});
|
|
|
|
prog.command('build [dest]')
|
|
.describe('Create a production-ready version of your app')
|
|
.option('-p, --port', 'Default of process.env.PORT', '3000')
|
|
.example(`build custom-dir -p 4567`)
|
|
.action(async (dest = 'build', opts: { port: string }) => {
|
|
console.log(`> Building...`);
|
|
|
|
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
|
|
process.env.SAPPER_DEST = dest;
|
|
|
|
const start = Date.now();
|
|
|
|
try {
|
|
const { build } = await import('./cli/build');
|
|
await build();
|
|
|
|
const launcher = path.resolve(dest, 'index.js');
|
|
|
|
fs.writeFileSync(launcher, `
|
|
// generated by sapper build at ${new Date().toISOString()}
|
|
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
|
|
process.env.SAPPER_DEST = __dirname;
|
|
process.env.PORT = process.env.PORT || ${opts.port || 3000};
|
|
|
|
console.log('Starting server on port ' + process.env.PORT);
|
|
require('./server.js');
|
|
`.replace(/^\t+/gm, '').trim());
|
|
|
|
console.error(`\n> Finished in ${elapsed(start)}. Type ${colors.bold.cyan(`node ${dest}`)} to run the app.`);
|
|
} catch (err) {
|
|
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
|
process.exit(1);
|
|
}
|
|
});
|
|
|
|
prog.command('start [dir]')
|
|
.describe('Start your app')
|
|
.option('-p, --port', 'Specify a port')
|
|
.option('-o, --open', 'Open a browser window')
|
|
.action(async (dir = 'build', opts: { port: number, open: boolean }) => {
|
|
const { start } = await import('./cli/start');
|
|
start(dir, opts);
|
|
});
|
|
|
|
prog.command('export [dest]')
|
|
.describe('Export your app as static files (if possible)')
|
|
.option('--basepath', 'Specify a base path')
|
|
.action(async (dest = 'export', opts: { basepath?: string }) => {
|
|
console.log(`> Building...`);
|
|
|
|
process.env.NODE_ENV = 'production';
|
|
process.env.SAPPER_DEST = '.sapper/.export';
|
|
|
|
const start = Date.now();
|
|
|
|
try {
|
|
const { build } = await import('./cli/build');
|
|
await build();
|
|
console.error(`\n> Built in ${elapsed(start)}. Crawling site...`);
|
|
|
|
const { exporter } = await import('./cli/export');
|
|
await exporter(dest, opts);
|
|
console.error(`\n> Finished in ${elapsed(start)}. Type ${colors.bold.cyan(`npx serve ${dest}`)} to run the app.`);
|
|
} catch (err) {
|
|
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
|
process.exit(1);
|
|
}
|
|
});
|
|
|
|
// TODO upgrade
|
|
|
|
prog.parse(process.argv);
|
|
|
|
function elapsed(start: number) {
|
|
return prettyMs(Date.now() - start);
|
|
}
|