diff --git a/src/cli.ts b/src/cli.ts index c83a081..12c79b2 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -21,7 +21,9 @@ prog.command('dev') prog.command('build [dest]') .describe('Create a production-ready version of your app') - .action(async (dest = 'build') => { + .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'; @@ -32,7 +34,20 @@ prog.command('build [dest]') try { const { build } = await import('./cli/build'); await build(); - console.error(`\n> Finished in ${elapsed(start)}. Type ${clorox.bold.cyan(dest === 'build' ? 'npx sapper start' : `npx sapper start ${dest}`)} to run the app.`); + + 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 ${clorox.bold.cyan(`node ${dest}`)} to run the app.`); } catch (err) { console.error(err ? err.details || err.stack || err.message || err : 'Unknown error'); } diff --git a/test/common/test.js b/test/common/test.js index db18be2..5e667dd 100644 --- a/test/common/test.js +++ b/test/common/test.js @@ -1,3 +1,4 @@ +const fs = require('fs'); const path = require('path'); const assert = require('assert'); const Nightmare = require('nightmare'); @@ -38,6 +39,7 @@ describe('sapper', function() { rimraf.sync('export'); rimraf.sync('build'); rimraf.sync('.sapper'); + rimraf.sync('start.js'); this.timeout(process.env.CI ? 30000 : 10000); @@ -148,7 +150,7 @@ function run({ mode, basepath = '' }) { before(() => { const promise = mode === 'production' - ? exec(`node ${cli} build`).then(() => ports.find(3000)) + ? exec(`node ${cli} build -l`).then(() => ports.find(3000)) : ports.find(3000).then(port => { exec(`node ${cli} dev`); return ports.wait(port).then(() => port); @@ -160,6 +162,10 @@ function run({ mode, basepath = '' }) { const dir = mode === 'production' ? 'build' : '.sapper'; + if (mode === 'production') { + assert.ok(fs.existsSync('build/index.js')); + } + proc = require('child_process').fork(`${dir}/server.js`, { cwd: process.cwd(), env: {