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';
|
process.env.NODE_ENV = 'production';
|
||||||
|
|
||||||
const program = require('commander');
|
|
||||||
const serverImpl = require('../lib/server-impl.js');
|
const serverImpl = require('../lib/server-impl.js');
|
||||||
|
|
||||||
program
|
const argv = require('yargs')
|
||||||
.option('-p, --port <port>', 'The port you want to start unleash on')
|
.usage('$0 [options]')
|
||||||
.option('-d, --databaseUri <databaseUri>', 'The full databaseUri to connect to, including username and password')
|
.env(true)
|
||||||
.parse(process.argv);
|
.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) {
|
serverImpl.start(argv)
|
||||||
userOpts.databaseUri = program.databaseUri;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(program.port) {
|
|
||||||
userOpts.port = program.port;
|
|
||||||
}
|
|
||||||
|
|
||||||
serverImpl.start(userOpts)
|
|
||||||
.then(conf => console.log(`Unleash started on http://localhost:${conf.app.get('port')}`))
|
.then(conf => console.log(`Unleash started on http://localhost:${conf.app.get('port')}`))
|
||||||
.catch(console.err);
|
.catch(console.err);
|
||||||
|
|
||||||
|
@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
const knex = require('knex');
|
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({
|
const db = knex({
|
||||||
client: 'pg',
|
client: 'pg',
|
||||||
connection: databaseUri,
|
connection: databaseUrl,
|
||||||
pool: { min: poolMin, max: poolMax },
|
pool: { min: poolMin, max: poolMax },
|
||||||
searchPath: databaseSchema,
|
searchPath: databaseSchema,
|
||||||
});
|
});
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
const { publicFolder } = require('unleash-frontend');
|
const { publicFolder } = require('unleash-frontend');
|
||||||
|
|
||||||
const DEFAULT_OPTIONS = {
|
const DEFAULT_OPTIONS = {
|
||||||
databaseUri: process.env.DATABASE_URL,
|
databaseUrl: process.env.DATABASE_URL,
|
||||||
port: process.env.HTTP_PORT || process.env.PORT || 4242,
|
port: process.env.HTTP_PORT || process.env.PORT || 4242,
|
||||||
baseUriPath: process.env.BASE_URI_PATH || '',
|
baseUriPath: process.env.BASE_URI_PATH || '',
|
||||||
serverMetrics: true,
|
serverMetrics: true,
|
||||||
@ -14,12 +14,12 @@ module.exports = {
|
|||||||
const options = Object.assign({}, DEFAULT_OPTIONS, opts);
|
const options = Object.assign({}, DEFAULT_OPTIONS, opts);
|
||||||
|
|
||||||
// If we are running in development we should assume local db
|
// If we are running in development we should assume local db
|
||||||
if(process.env.NODE_ENV === 'development' && !options.databaseUri) {
|
if(process.env.NODE_ENV === 'development' && !options.databaseUrl) {
|
||||||
options.databaseUri = 'postgres://unleash_user:passord@localhost:5432/unleash';
|
options.databaseUrl = 'postgres://unleash_user:passord@localhost:5432/unleash';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!options.databaseUri) {
|
if (!options.databaseUrl) {
|
||||||
throw new Error('You must either pass databaseUri option or set environemnt variable DATABASE_URL');
|
throw new Error('You must either pass databaseUrl option or set environemnt variable DATABASE_URL');
|
||||||
}
|
}
|
||||||
return options;
|
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';
|
process.env.NODE_ENV = 'development';
|
||||||
const { createOptions } = require('./options');
|
const { createOptions } = require('./options');
|
||||||
|
|
||||||
const options = createOptions({});
|
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 => {
|
test('should not override provided options', t => {
|
||||||
@ -25,8 +25,8 @@ test('should not override provided options', t => {
|
|||||||
process.env.NODE_ENV = 'production';
|
process.env.NODE_ENV = 'production';
|
||||||
|
|
||||||
const { createOptions } = require('./options');
|
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);
|
t.true(options.port === 1111);
|
||||||
});
|
});
|
@ -38,7 +38,7 @@ function createApp (options) {
|
|||||||
function start (opts) {
|
function start (opts) {
|
||||||
const options = createOptions(opts);
|
const options = createOptions(opts);
|
||||||
|
|
||||||
return migrator({ databaseUri: options.databaseUri })
|
return migrator({ databaseUrl: options.databaseUrl })
|
||||||
.catch(err => logger.error('failed to migrate db', err))
|
.catch(err => logger.error('failed to migrate db', err))
|
||||||
.then(() => createApp(options))
|
.then(() => createApp(options))
|
||||||
.catch(err => logger.error('failed creating app', err));
|
.catch(err => logger.error('failed creating app', err));
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
const { getInstance } = require('db-migrate');
|
const { getInstance } = require('db-migrate');
|
||||||
const parseDbUrl = require('parse-database-url');
|
const parseDbUrl = require('parse-database-url');
|
||||||
|
|
||||||
function migrateDb ({ databaseUri, databaseSchema = 'public' }) {
|
function migrateDb ({ databaseUrl, databaseSchema = 'public' }) {
|
||||||
const custom = parseDbUrl(databaseUri);
|
const custom = parseDbUrl(databaseUrl);
|
||||||
custom.schema = databaseSchema;
|
custom.schema = databaseSchema;
|
||||||
const dbmigrate = getInstance(true, {
|
const dbmigrate = getInstance(true, {
|
||||||
cwd: __dirname,
|
cwd: __dirname,
|
||||||
|
@ -75,7 +75,8 @@
|
|||||||
"response-time": "^2.3.2",
|
"response-time": "^2.3.2",
|
||||||
"serve-favicon": "^2.3.0",
|
"serve-favicon": "^2.3.0",
|
||||||
"unleash-frontend": "github:unleash/unleash-frontend",
|
"unleash-frontend": "github:unleash/unleash-frontend",
|
||||||
"yallist": "^2.0.0"
|
"yallist": "^2.0.0",
|
||||||
|
"yargs": "^6.5.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^6.0.46",
|
"@types/node": "^6.0.46",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
function getDatabaseUri () {
|
function getDatabaseUrl () {
|
||||||
if (process.env.TEST_DATABASE_URL) {
|
if (process.env.TEST_DATABASE_URL) {
|
||||||
return process.env.TEST_DATABASE_URL;
|
return process.env.TEST_DATABASE_URL;
|
||||||
} else {
|
} else {
|
||||||
@ -9,5 +9,5 @@ function getDatabaseUri () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
getDatabaseUri,
|
getDatabaseUrl,
|
||||||
};
|
};
|
||||||
|
@ -20,12 +20,12 @@ const eventBus = new EventEmitter();
|
|||||||
|
|
||||||
function createApp (databaseSchema = 'test') {
|
function createApp (databaseSchema = 'test') {
|
||||||
const options = {
|
const options = {
|
||||||
databaseUri: require('./database-config').getDatabaseUri(),
|
databaseUrl: require('./database-config').getDatabaseUrl(),
|
||||||
databaseSchema,
|
databaseSchema,
|
||||||
minPool: 0,
|
minPool: 0,
|
||||||
maxPool: 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}`)
|
return db.raw(`CREATE SCHEMA IF NOT EXISTS ${options.databaseSchema}`)
|
||||||
.then(() => migrator(options))
|
.then(() => migrator(options))
|
||||||
|
Loading…
Reference in New Issue
Block a user