This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 432
better CLI #170
Merged
Merged
better CLI #170
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5289fc1
better CLI, more port control. fixes #169
Rich-Harris d9d93f4
add get-port back as dev dependency, for testing
Rich-Harris 0f8c04b
use port-authority
Rich-Harris a66ac00
tidy up tests
Rich-Harris 47b50f2
admin
Rich-Harris ffaacb4
use fs.existsSync
Rich-Harris bc23200
simplify sade initialisation
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,112 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as child_process from 'child_process'; | ||
import sade from 'sade'; | ||
import mri from 'mri'; | ||
import chalk from 'chalk'; | ||
import prettyMs from 'pretty-ms'; | ||
import help from './help.md'; | ||
import build from './build'; | ||
import exporter from './export'; | ||
import dev from './dev'; | ||
import upgrade from './upgrade'; | ||
import * as ports from 'port-authority'; | ||
import * as pkg from '../../package.json'; | ||
|
||
const opts = mri(process.argv.slice(2), { | ||
alias: { | ||
h: 'help' | ||
} | ||
}); | ||
const prog = sade('sapper').version(pkg.version); | ||
|
||
prog.command('dev') | ||
.describe('Start a development server') | ||
.option('-p, --port', 'Specify a port') | ||
.action(async ({ port }: { port: number }) => { | ||
if (port) { | ||
if (!await ports.check(port)) { | ||
console.log(chalk.bold.red(`> Port ${port} is unavailable`)); | ||
return; | ||
} | ||
} else { | ||
port = await ports.find(3000); | ||
} | ||
|
||
dev(port); | ||
}); | ||
|
||
prog.command('build [dest]') | ||
.describe('Create a production-ready version of your app') | ||
.action((dest = 'build') => { | ||
console.log(`> Building...`); | ||
|
||
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 start = Date.now(); | ||
|
||
switch (cmd) { | ||
case 'build': | ||
process.env.NODE_ENV = 'production'; | ||
process.env.SAPPER_DEST = opts._[1] || 'build'; | ||
process.env.SAPPER_DEST = dest; | ||
|
||
const start = Date.now(); | ||
|
||
build() | ||
.then(() => { | ||
const elapsed = Date.now() - start; | ||
console.error(`built in ${elapsed}ms`); // TODO beautify this, e.g. 'built in 4.7 seconds' | ||
console.error(`\n> Finished in ${prettyMs(elapsed)}. Type ${chalk.bold.cyan(dest === 'build' ? 'npx sapper start' : `npx sapper start ${dest}`)} to run the app.`); | ||
}) | ||
.catch(err => { | ||
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error'); | ||
}); | ||
}); | ||
|
||
prog.command('start [dir]') | ||
.describe('Start your app') | ||
.option('-p, --port', 'Specify a port') | ||
.action(async (dir = 'build', { port }: { port: number }) => { | ||
const resolved = path.resolve(dir); | ||
const server = path.resolve(dir, 'server.js'); | ||
|
||
if (!fs.existsSync(server)) { | ||
console.log(chalk.bold.red(`> ${dir}/server.js does not exist — type ${chalk.bold.cyan(dir === 'build' ? `npx sapper build` : `npx sapper build ${dir}`)} to create it`)); | ||
return; | ||
} | ||
|
||
if (port) { | ||
if (!await ports.check(port)) { | ||
console.log(chalk.bold.red(`> Port ${port} is unavailable`)); | ||
return; | ||
} | ||
} else { | ||
port = await ports.find(3000); | ||
} | ||
|
||
child_process.fork(server, [], { | ||
cwd: process.cwd(), | ||
env: Object.assign({ | ||
NODE_ENV: 'production', | ||
PORT: port, | ||
SAPPER_DEST: dir | ||
}, process.env) | ||
}); | ||
}); | ||
|
||
break; | ||
prog.command('export [dest]') | ||
.describe('Export your app as static files (if possible)') | ||
.action((dest = 'export') => { | ||
console.log(`> Building...`); | ||
|
||
case 'export': | ||
process.env.NODE_ENV = 'production'; | ||
process.env.SAPPER_DEST = '.sapper/.export'; | ||
|
||
const export_dir = opts._[1] || 'export'; | ||
const start = Date.now(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when |
||
|
||
build() | ||
.then(() => exporter(export_dir)) | ||
.then(() => { | ||
const elapsed = Date.now() - start; | ||
console.error(`extracted in ${elapsed}ms`); // TODO beautify this, e.g. 'built in 4.7 seconds' | ||
console.error(`\n> Built in ${prettyMs(elapsed)}. Exporting...`); | ||
}) | ||
.then(() => exporter(dest)) | ||
.then(() => { | ||
const elapsed = Date.now() - start; | ||
console.error(`\n> Finished in ${prettyMs(elapsed)}. Type ${chalk.bold.cyan(`npx serve ${dest}`)} to run the app.`); | ||
}) | ||
.catch(err => { | ||
console.error(err ? err.details || err.stack || err.message || err : 'Unknown error'); | ||
}); | ||
}); | ||
|
||
break; | ||
|
||
case 'dev': | ||
dev(); | ||
break; | ||
|
||
case 'upgrade': | ||
upgrade(); | ||
break; | ||
|
||
case 'start': | ||
const dir = path.resolve(opts._[1] || 'build'); | ||
|
||
child_process.fork(`${dir}/server.js`, [], { | ||
cwd: process.cwd(), | ||
env: Object.assign({ | ||
NODE_ENV: 'production', | ||
SAPPER_DEST: dir | ||
}, process.env) | ||
}); | ||
|
||
break; | ||
// TODO upgrade | ||
|
||
default: | ||
console.log(`unrecognized command ${cmd} — try \`sapper --help\` for more information`); | ||
} | ||
prog.parse(process.argv); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
More for my own curiosity, but why not set
SAPPER_DEST
from withinexport()
? Especially since thedest
is variableThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used by
build
first — it sends the webpack output to.sapper/.export
, and thensapper export
runs the code therein to generate theexport
directory. So it has to be set before either thing happens