Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async configuration support. #359

Merged
merged 5 commits into from
Jan 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 83 additions & 50 deletions lib/helpers/config-helper.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
'use strict';

var args = require('yargs').argv;
var path = require('path');
var fs = require('fs');
var helpers = require(__dirname);
var url = require('url');
var _ = require('lodash');

module.exports = {
var args = require('yargs').argv;
var Bluebird = require('bluebird');
var path = require('path');
var fs = require('fs');
var helpers = require(__dirname);
var url = require('url');
var _ = require('lodash');

var api = {
config: undefined,
rawConfig: undefined,
error: undefined,
init: function () {
return Bluebird.resolve()
.then(function () {
var config;

if (args.url) {
config = api.parseDbUrl(args.url);
} else {
try {
config = require(api.getConfigFile());
} catch (e) {
api.error = e;
}
}
return config;
})
.then(function (config) {
if (typeof config === 'object' || config === undefined) {
return config;
} else if (config.length === 1) {
return Bluebird.promisify(config)();
} else {
return config();
}
})
.then(function (config) {
api.rawConfig = config;
})
.then(function () {
// Always return the full config api
return api;
});
},
getConfigFile: function () {
if (args.config) {
return path.resolve(process.cwd(), args.config);
Expand All @@ -20,11 +57,11 @@ module.exports = {
},

relativeConfigFile: function () {
return path.relative(process.cwd(), this.getConfigFile());
return path.relative(process.cwd(), api.getConfigFile());
},

configFileExists: function () {
return helpers.path.existsSync(this.getConfigFile());
return helpers.path.existsSync(api.getConfigFile());
},

getDefaultConfig: function () {
Expand Down Expand Up @@ -54,69 +91,63 @@ module.exports = {
},

writeDefaultConfig: function () {
var configPath = path.dirname(this.getConfigFile());
var configPath = path.dirname(api.getConfigFile());

if (!helpers.path.existsSync(configPath)) {
fs.mkdirSync(configPath);
}

fs.writeFileSync(this.getConfigFile(), this.getDefaultConfig());
fs.writeFileSync(api.getConfigFile(), api.getDefaultConfig());
},

readConfig: function (options) {
var env = helpers.generic.getEnvironment();

options = _.assign({
logging: true
}, options || {});
readConfig: function () {
if (!api.config) {
var env = helpers.generic.getEnvironment();

if (!this.config) {
if (args.url) {
this.config = this.parseDbUrl(args.url);
} else {
try {
this.config = require(this.getConfigFile());
} catch (e) {
throw new Error('Error reading "' + this.relativeConfigFile() + '". Error: ' + e.message);
}
if (api.rawConfig === undefined) {
throw new Error(
'Error reading "' +
api.relativeConfigFile() +
'". Error: ' + api.error
);
}

if (typeof this.config !== 'object') {
throw new Error('Config must be an object: ' + this.relativeConfigFile());
if (typeof api.rawConfig !== 'object') {
throw new Error(
'Config must be an object or a promise for an object: ' +
api.relativeConfigFile()
);
}

if (options.logging) {
if (args.url) {
console.log('Parsed url ' + this.filteredUrl(args.url));
} else {
console.log('Loaded configuration file "' + this.relativeConfigFile() + '".');
}
if (args.url) {
console.log('Parsed url ' + api.filteredUrl(args.url, api.rawConfig));
} else {
console.log('Loaded configuration file "' + api.relativeConfigFile() + '".');
}

if (this.config[env]) {
if (options.logging) {
console.log('Using environment "' + env + '".');
}
if (api.rawConfig[env]) {
console.log('Using environment "' + env + '".');

// The Sequelize library needs a function passed in to its logging option
if (this.config.logging && !_.isFunction(this.config.logging)) {
this.config.logging = console.log;
if (api.rawConfig.logging && !_.isFunction(api.rawConfig.logging)) {
api.rawConfig.logging = console.log;
}

this.config = this.config[env];
api.rawConfig = api.rawConfig[env];
}

// in case url is present - we overwrite the configuration
if (this.config.url) {
this.config = _.merge(this.config, this.parseDbUrl(this.config.url));
if (api.rawConfig.url) {
api.rawConfig = _.merge(api.rawConfig, api.parseDbUrl(api.rawConfig.url));
}
}

return this.config;
api.config = api.rawConfig;
}
return api.config;
},

filteredUrl: function (url) {
var regExp = new RegExp(':?' + (this.config.password || '') + '@');
filteredUrl: function (url, config) {
var regExp = new RegExp(':?' + (config.password || '') + '@');

return url.replace(regExp, ':*****@');
},
Expand All @@ -129,7 +160,7 @@ module.exports = {
}, options || {});

try {
config = this.readConfig();
config = api.readConfig();
} catch (e) {
if (options.ignoreConfig) {
config = {};
Expand Down Expand Up @@ -165,7 +196,7 @@ module.exports = {
},

parseDbUrl: function (urlString) {
var config = this.urlStringToConfigHash(urlString);
var config = api.urlStringToConfigHash(urlString);

config = _.assign(config, {
dialect: config.dialect.replace(/:$/, '')
Expand All @@ -180,3 +211,5 @@ module.exports = {
return config;
}
};

module.exports = api;
99 changes: 55 additions & 44 deletions lib/helpers/gulp-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ var helpers = require(__dirname);
var _ = require('lodash');
var clc = require('cli-color');

var config = helpers.config.init();

module.exports = {
addTask: function (gulp, taskName, task) {
gulp.task(
taskName,
task.descriptions.short,
task.dependencies || [],
function () {
task.task();
return config.then(function () {
task.task();
});
}, {
aliases: task.aliases || []
}
Expand All @@ -25,50 +29,57 @@ module.exports = {
'help:' + taskName,
false,
function () {
helpers.view.log(clc.bold('COMMANDS'));

var commands = [ taskName ].concat(task.aliases || []);
var commandMargin = Math.max.apply(null, commands.map(function (c) {
return c.length;
}));

commands.forEach(function (command) {
var s = [
'sequelize', command + (new Array(commandMargin - command.length + 1).join(' ')),
'--', task.descriptions.short
].join(' ');

helpers.view.log(' ' + s);
});
helpers.view.log();

helpers.view.log(clc.bold('DESCRIPTION'));

(task.descriptions.long || [task.descriptions.short]).forEach(function (line) {
helpers.view.log(' ' + line);
return config.then(function () {
Object.keys(task.descriptions).forEach(function (description) {
if (typeof task.descriptions[description] === 'function') {
task.descriptions[description] = task.descriptions[description]();
}
});
helpers.view.log(clc.bold('COMMANDS'));

var commands = [ taskName ].concat(task.aliases || []);
var commandMargin = Math.max.apply(null, commands.map(function (c) {
return c.length;
}));

commands.forEach(function (command) {
var s = [
'sequelize', command + (new Array(commandMargin - command.length + 1).join(' ')),
'--', task.descriptions.short
].join(' ');

helpers.view.log(' ' + s);
});
helpers.view.log();

helpers.view.log(clc.bold('DESCRIPTION'));

(task.descriptions.long || [task.descriptions.short]).forEach(function (line) {
helpers.view.log(' ' + line);
});

(function (options) {
if (options) {
var margin = Math.max.apply(null, Object.keys(options).map(function (o) {
return o.length;
}));

helpers.view.log();
helpers.view.log(clc.bold('OPTIONS'));

Object.keys(options).forEach(function (option) {
var args = [' ', option];

args.push(new Array(margin - option.length + 1).join(' '));
args.push(options[option]);

helpers.view.log.apply(helpers.view, args);
});
}
})(_.assign(self.getGlobalOptions(), task.descriptions.options));

helpers.view.log();
});

(function (options) {
if (options) {
var margin = Math.max.apply(null, Object.keys(options).map(function (o) {
return o.length;
}));

helpers.view.log();
helpers.view.log(clc.bold('OPTIONS'));

Object.keys(options).forEach(function (option) {
var args = [' ', option];

args.push(new Array(margin - option.length + 1).join(' '));
args.push(options[option]);

helpers.view.log.apply(helpers.view, args);
});
}
})(_.assign(self.getGlobalOptions(), task.descriptions.options));

helpers.view.log();
}
);
},
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/version-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {

getDialect: function () {
try {
return helpers.config.readConfig({ logging: false });
return helpers.config.readConfig();
} catch (e) {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
descriptions: {
'short': 'Generates a model and its migration.',

'long': (function () {
'long': function () {
var migrationFileName = helpers.path.getFileName(
'migration',
helpers.migration.generateMigrationName({ name: 'User' }),
Expand Down Expand Up @@ -61,7 +61,7 @@ module.exports = {
);

return result;
})(),
},

options: {
'--name': 'The name of the new model.',
Expand Down
8 changes: 7 additions & 1 deletion test/db/migrate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var _ = require('lodash');
'db:migrate --coffee',
'db:migrate --config ../../support/tmp/config/config.json',
'db:migrate --config ' + Support.resolveSupportPath('tmp', 'config', 'config.json'),
'db:migrate --config ../../support/tmp/config/config.js'
'db:migrate --config ../../support/tmp/config/config.js',
'db:migrate --config ../../support/tmp/config/config-promise.js'
]).forEach(function (flag) {
var prepare = function (callback, options) {
options = _.assign({ config: {} }, options || {});
Expand All @@ -32,6 +33,11 @@ var _ = require('lodash');
if (flag.match(/config\.js$/)) {
configPath = configPath + 'config.js';
configContent = 'module.exports = ' + configContent;
} else if (flag.match(/config-promise\.js/)) {
configPath = configPath + 'config-promise.js';
configContent = '' +
'var b = require("bluebird");' +
'module.exports = b.resolve(' + configContent + ');';
} else {
configPath = configPath + 'config.json';
}
Expand Down
8 changes: 7 additions & 1 deletion test/db/seed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var _ = require('lodash');
'db:seed --config ../../support/tmp/config/config.json --seed seedPerson.js',
'db:seed --seed seedPerson.js --config ' +
Support.resolveSupportPath('tmp', 'config', 'config.json'),
'db:seed --seed seedPerson.js --config ../../support/tmp/config/config.js'
'db:seed --seed seedPerson.js --config ../../support/tmp/config/config.js',
'db:seed --seed seedPerson.js --config ../../support/tmp/config/config-promise.js'
]).forEach(function (flag) {
var prepare = function (callback, options) {
options = _.assign({ config: {} }, options || {});
Expand All @@ -32,6 +33,11 @@ var _ = require('lodash');
if (flag.match(/config\.js$/)) {
configPath = configPath + 'config.js';
configContent = 'module.exports = ' + configContent;
} else if (flag.match(/config-promise\.js/)) {
configPath = configPath + 'config-promise.js';
configContent = '' +
'var b = require("bluebird");' +
'module.exports = b.resolve(' + configContent + ');';
} else {
configPath = configPath + 'config.json';
}
Expand Down