mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-04 00:18:01 +01:00
replace commander with yargs
This commit is contained in:
parent
8541adc40f
commit
7057d11553
@ -3,25 +3,29 @@
|
||||
|
||||
process.env.NODE_ENV = 'production';
|
||||
|
||||
const program = require('commander');
|
||||
const serverImpl = require('../lib/server-impl.js');
|
||||
|
||||
program
|
||||
.option('-p, --port <port>', 'The port you want to start unleash on')
|
||||
.option('-d, --databaseUri <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);
|
||||
|
||||
|
@ -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,
|
||||
});
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
});
|
@ -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));
|
||||
|
@ -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,
|
||||
|
@ -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",
|
||||
|
@ -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,
|
||||
};
|
||||
|
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user