From 7189b0ba334bea0198b5762ebfeda21030f4b9e8 Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Sat, 3 Dec 2016 13:45:22 +0100 Subject: [PATCH] replace commander with yargs --- bin/unleash.js | 34 ++++++++++++++++------------- lib/db/db-pool.js | 4 ++-- lib/options.js | 10 ++++----- lib/options.test.js | 8 +++---- lib/server-impl.js | 2 +- migrator.js | 4 ++-- package.json | 3 ++- test/e2e/helpers/database-config.js | 4 ++-- test/e2e/helpers/test-helper.js | 4 ++-- 9 files changed, 39 insertions(+), 34 deletions(-) diff --git a/bin/unleash.js b/bin/unleash.js index e8034743e5..f0709cdf5f 100755 --- a/bin/unleash.js +++ b/bin/unleash.js @@ -3,25 +3,29 @@ process.env.NODE_ENV = 'production'; -const program = require('commander'); const serverImpl = require('../lib/server-impl.js'); -program - .option('-p, --port ', 'The port you want to start unleash on') - .option('-d, --databaseUri ', 'The full databaseUri to connect to, including username and password') - .parse(process.argv); +const argv = require('yargs') + .usage('$0 [options]') + .env(true) + .option('port', { + alias: 'p', + describe: 'The HTTP port you want to start unleash on', + demand: false, + default: 4242, + type: 'number', + }) + .option('databaseUrl', { + alias: 'd', + describe: 'The full databaseUrl to connect to, including username and password', + demand: true, + type: 'string', + }) + .argv -const userOpts = {}; +console.log(argv.databaseUrl); -if(program.databaseUri) { - userOpts.databaseUri = program.databaseUri; -} - -if(program.port) { - userOpts.port = program.port; -} - -serverImpl.start(userOpts) +serverImpl.start(argv) .then(conf => console.log(`Unleash started on http://localhost:${conf.app.get('port')}`)) .catch(console.err); diff --git a/lib/db/db-pool.js b/lib/db/db-pool.js index 9ecea4f695..e59be378d8 100644 --- a/lib/db/db-pool.js +++ b/lib/db/db-pool.js @@ -2,10 +2,10 @@ const knex = require('knex'); -module.exports.createDb = function ({ databaseUri, poolMin = 2, poolMax = 20, databaseSchema = 'public' }) { +module.exports.createDb = function ({ databaseUrl, poolMin = 2, poolMax = 20, databaseSchema = 'public' }) { const db = knex({ client: 'pg', - connection: databaseUri, + connection: databaseUrl, pool: { min: poolMin, max: poolMax }, searchPath: databaseSchema, }); diff --git a/lib/options.js b/lib/options.js index 3abd347f46..92074bf998 100644 --- a/lib/options.js +++ b/lib/options.js @@ -2,7 +2,7 @@ const { publicFolder } = require('unleash-frontend'); const DEFAULT_OPTIONS = { - databaseUri: process.env.DATABASE_URL, + databaseUrl: process.env.DATABASE_URL, port: process.env.HTTP_PORT || process.env.PORT || 4242, baseUriPath: process.env.BASE_URI_PATH || '', serverMetrics: true, @@ -14,12 +14,12 @@ module.exports = { const options = Object.assign({}, DEFAULT_OPTIONS, opts); // If we are running in development we should assume local db - if(process.env.NODE_ENV === 'development' && !options.databaseUri) { - options.databaseUri = 'postgres://unleash_user:passord@localhost:5432/unleash'; + if(process.env.NODE_ENV === 'development' && !options.databaseUrl) { + options.databaseUrl = 'postgres://unleash_user:passord@localhost:5432/unleash'; } - if (!options.databaseUri) { - throw new Error('You must either pass databaseUri option or set environemnt variable DATABASE_URL'); + if (!options.databaseUrl) { + throw new Error('You must either pass databaseUrl option or set environemnt variable DATABASE_URL'); } return options; } diff --git a/lib/options.test.js b/lib/options.test.js index 2dc69b2f1a..3b104277ee 100644 --- a/lib/options.test.js +++ b/lib/options.test.js @@ -11,13 +11,13 @@ test('should require DATABASE_URI', t => { }); }); -test('should set default databaseUri for develpment', t => { +test('should set default databaseUrl for develpment', t => { process.env.NODE_ENV = 'development'; const { createOptions } = require('./options'); const options = createOptions({}); - t.true(options.databaseUri === 'postgres://unleash_user:passord@localhost:5432/unleash'); + t.true(options.databaseUrl === 'postgres://unleash_user:passord@localhost:5432/unleash'); }); test('should not override provided options', t => { @@ -25,8 +25,8 @@ test('should not override provided options', t => { process.env.NODE_ENV = 'production'; const { createOptions } = require('./options'); - const options = createOptions({databaseUri: 'test', port: 1111}); + const options = createOptions({databaseUrl: 'test', port: 1111}); - t.true(options.databaseUri === 'test'); + t.true(options.databaseUrl === 'test'); t.true(options.port === 1111); }); \ No newline at end of file diff --git a/lib/server-impl.js b/lib/server-impl.js index c970538b7b..1de7ddc66f 100644 --- a/lib/server-impl.js +++ b/lib/server-impl.js @@ -38,7 +38,7 @@ function createApp (options) { function start (opts) { const options = createOptions(opts); - return migrator({ databaseUri: options.databaseUri }) + return migrator({ databaseUrl: options.databaseUrl }) .catch(err => logger.error('failed to migrate db', err)) .then(() => createApp(options)) .catch(err => logger.error('failed creating app', err)); diff --git a/migrator.js b/migrator.js index 39aeecc9c5..6c458bd5af 100644 --- a/migrator.js +++ b/migrator.js @@ -3,8 +3,8 @@ const { getInstance } = require('db-migrate'); const parseDbUrl = require('parse-database-url'); -function migrateDb ({ databaseUri, databaseSchema = 'public' }) { - const custom = parseDbUrl(databaseUri); +function migrateDb ({ databaseUrl, databaseSchema = 'public' }) { + const custom = parseDbUrl(databaseUrl); custom.schema = databaseSchema; const dbmigrate = getInstance(true, { cwd: __dirname, diff --git a/package.json b/package.json index cc20e67f2a..bf46bb5891 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,8 @@ "response-time": "^2.3.2", "serve-favicon": "^2.3.0", "unleash-frontend": "github:unleash/unleash-frontend", - "yallist": "^2.0.0" + "yallist": "^2.0.0", + "yargs": "^6.5.0" }, "devDependencies": { "@types/node": "^6.0.46", diff --git a/test/e2e/helpers/database-config.js b/test/e2e/helpers/database-config.js index 384c416b74..8b6ecebf9b 100644 --- a/test/e2e/helpers/database-config.js +++ b/test/e2e/helpers/database-config.js @@ -1,6 +1,6 @@ 'use strict'; -function getDatabaseUri () { +function getDatabaseUrl () { if (process.env.TEST_DATABASE_URL) { return process.env.TEST_DATABASE_URL; } else { @@ -9,5 +9,5 @@ function getDatabaseUri () { } module.exports = { - getDatabaseUri, + getDatabaseUrl, }; diff --git a/test/e2e/helpers/test-helper.js b/test/e2e/helpers/test-helper.js index e1e36a5167..34bf24c604 100644 --- a/test/e2e/helpers/test-helper.js +++ b/test/e2e/helpers/test-helper.js @@ -20,12 +20,12 @@ const eventBus = new EventEmitter(); function createApp (databaseSchema = 'test') { const options = { - databaseUri: require('./database-config').getDatabaseUri(), + databaseUrl: require('./database-config').getDatabaseUrl(), databaseSchema, minPool: 0, maxPool: 0, }; - const db = createDb({ databaseUri: options.databaseUri, minPool: 0, maxPool: 0 }); + const db = createDb({ databaseUrl: options.databaseUrl, minPool: 0, maxPool: 0 }); return db.raw(`CREATE SCHEMA IF NOT EXISTS ${options.databaseSchema}`) .then(() => migrator(options))