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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
better CLI
- Loading branch information
Showing
10 changed files
with
206 additions
and
213 deletions.
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(); | ||
|
||
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.