mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-23 07:31:25 +00:00
simplify
This commit is contained in:
35
src/cli.ts
35
src/cli.ts
@@ -21,18 +21,11 @@ prog.command('dev')
|
|||||||
|
|
||||||
prog.command('build [dest]')
|
prog.command('build [dest]')
|
||||||
.describe('Create a production-ready version of your app')
|
.describe('Create a production-ready version of your app')
|
||||||
.option('-l, --launcher', 'Create a launcher file (defaults to start.js)')
|
.option('-p, --port', 'Default of process.env.PORT', '3000')
|
||||||
.option('-p, --port', 'Default port, if using --launcher')
|
.example(`build custom-dir -p 4567`)
|
||||||
.example(`build -l`)
|
.action(async (dest = 'build', opts: { port: string }) => {
|
||||||
.example(`build custom-dir -l custom-launcher.js -p 4567`)
|
|
||||||
.action(async (dest = 'build', opts: { launcher: string | true, port: string }) => {
|
|
||||||
console.log(`> Building...`);
|
console.log(`> Building...`);
|
||||||
|
|
||||||
if (opts.port && !opts.launcher) {
|
|
||||||
console.error(`${clorox.bold.red(`You can only use the --port option with --launcher`)}`);
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
process.env.NODE_ENV = 'production';
|
process.env.NODE_ENV = 'production';
|
||||||
process.env.SAPPER_DEST = dest;
|
process.env.SAPPER_DEST = dest;
|
||||||
|
|
||||||
@@ -42,31 +35,19 @@ prog.command('build [dest]')
|
|||||||
const { build } = await import('./cli/build');
|
const { build } = await import('./cli/build');
|
||||||
await build();
|
await build();
|
||||||
|
|
||||||
let cmd;
|
const launcher = path.resolve(dest, 'index.js');
|
||||||
|
|
||||||
if (opts.launcher) {
|
fs.writeFileSync(launcher, `
|
||||||
const launcher = opts.launcher === true ? path.join(dest, 'index.js') : opts.launcher;
|
|
||||||
const resolvedLauncher = path.resolve(launcher);
|
|
||||||
const resolvedServer = path.resolve(dest, 'server.js');
|
|
||||||
const pathToServer = path.relative(path.dirname(resolvedLauncher), resolvedServer);
|
|
||||||
const pathToDest = path.relative(path.dirname(resolvedLauncher), dest);
|
|
||||||
|
|
||||||
fs.writeFileSync(resolvedLauncher, `
|
|
||||||
// generated by sapper build at ${new Date().toISOString()}
|
// generated by sapper build at ${new Date().toISOString()}
|
||||||
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
|
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
|
||||||
process.env.SAPPER_DEST = ${pathToDest ? `require('path').resolve(__dirname, '${pathToDest}')` : '__dirname'};
|
process.env.SAPPER_DEST = __dirname;
|
||||||
process.env.PORT = process.env.PORT || ${opts.port || 3000};
|
process.env.PORT = process.env.PORT || ${opts.port || 3000};
|
||||||
|
|
||||||
console.log('Starting server on port ' + process.env.PORT);
|
console.log('Starting server on port ' + process.env.PORT);
|
||||||
require('${pathToServer[0] === '.' ? pathToServer : `./${pathToServer}`}');
|
require('./server.js');
|
||||||
`.replace(/^\t+/gm, '').trim());
|
`.replace(/^\t+/gm, '').trim());
|
||||||
|
|
||||||
cmd = `node ${path.relative(process.cwd(), launcher).replace(/[\/\\]index.js$/, '')}`;
|
console.error(`\n> Finished in ${elapsed(start)}. Type ${clorox.bold.cyan(`node ${dest}`)} to run the app.`);
|
||||||
} else {
|
|
||||||
cmd = dest === 'build' ? 'npx sapper start' : `npx sapper start ${dest}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.error(`\n> Finished in ${elapsed(start)}. Type ${clorox.bold.cyan(cmd)} to run the app.`);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user