From 23a7cd1689a51d2ee9577af2b433a3c47a0ba285 Mon Sep 17 00:00:00 2001 From: jarvys Date: Wed, 2 Jul 2014 21:02:59 +0800 Subject: [PATCH] * add alias for port * add -s option for daemon process --- bin/index.js | 13 +++++++++++-- src/cli.js | 39 +++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/bin/index.js b/bin/index.js index b4fd9a8..9ae5ab7 100755 --- a/bin/index.js +++ b/bin/index.js @@ -6,6 +6,15 @@ var cli = require('../src/cli') var notifier = updateNotifier({packagePath: '../package'}) if (notifier.update) notifier.notify() -var argv = minimist(process.argv.slice(2)) +var argv = minimist(process.argv.slice(2), { + boolean: ["silent"], + alias: { + 'p': 'port', + 's': 'silent' + }, + default: { + silent: false + } +}) -cli.run(argv) \ No newline at end of file +cli.run(argv) diff --git a/src/cli.js b/src/cli.js index 6b81b5c..4869115 100644 --- a/src/cli.js +++ b/src/cli.js @@ -19,29 +19,31 @@ function help() { } // Start server -function start(port) { +function start(port, silent) { for (var prop in low.db) { console.log('http://localhost:' + port + '/' + chalk.green(prop)) } - console.log( - '\nEnter ' + chalk.green('`s`') + ' at any time to create a snapshot of the db\n' - ) - process.stdin.resume() - process.stdin.setEncoding('utf8') - process.stdin.on('data', function (chunk) { - if (chunk.trim().toLowerCase() === 's') { - var file = 'db-' + Date.now() + '.json' - low.save(file) - console.log('\nSaved snapshot to ' + chalk.green(file) + '\n') - } - }) + if(!silent) { + console.log( + '\nEnter ' + chalk.green('`s`') + ' at any time to create a snapshot of the db\n' + ) + process.stdin.resume() + process.stdin.setEncoding('utf8') + process.stdin.on('data', function (chunk) { + if (chunk.trim().toLowerCase() === 's') { + var file = 'db-' + Date.now() + '.json' + low.save(file) + console.log('\nSaved snapshot to ' + chalk.green(file) + '\n') + } + }) + } server.listen(port) } // Load source -function load(source, port) { +function load(source, port, silent) { console.log(chalk.green('\n{^ ^} Heya!\n')) console.log('Loading database from ' + source + '\n') @@ -50,13 +52,13 @@ function load(source, port) { var path = process.cwd() + '/' + source low.path = path low.db = require(path); - start(port) + start(port, silent) } if (/\.js$/.test(source)) { var path = process.cwd() + '/' + source low.db = require(path).run(); - start(port) + start(port, silent) } if (/^http/.test(source)) { @@ -67,7 +69,7 @@ function load(source, port) { console.error(err) } else { low.db = JSON.parse(res.text) - start(port) + start(port, silent) } }) } @@ -77,9 +79,10 @@ function load(source, port) { function run(argv) { var source = argv._[0] var port = argv.port || 3000 + var silent = argv.silent if (argv.version) return version() - if (source) return load(source, port) + if (source) return load(source, port, silent) help() }