Skip to content

Commit

Permalink
Add async configuration support.
Browse files Browse the repository at this point in the history
  • Loading branch information
wwalser committed Sep 27, 2016
1 parent cba03c8 commit 4583aff
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 93 deletions.
130 changes: 83 additions & 47 deletions lib/helpers/config-helper.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
'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,
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.config = 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 +56,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,13 +90,13 @@ 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) {
Expand All @@ -70,53 +106,51 @@ module.exports = {
logging: true
}, options || {});

if (!this.config) {
if (api.config === undefined) {
throw new Error(
'Error reading "' +
api.relativeConfigFile() +
'". Error: ' + api.error
);
}

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

if (options.logging) {
if (args.url) {
this.config = this.parseDbUrl(args.url);
console.log('Parsed url ' + api.filteredUrl(args.url));
} else {
try {
this.config = require(this.getConfigFile());
} catch (e) {
throw new Error('Error reading "' + this.relativeConfigFile() + '". Error: ' + e.message);
}
console.log('Loaded configuration file "' + api.relativeConfigFile() + '".');
}
}

if (typeof this.config !== 'object') {
throw new Error('Config must be an object: ' + this.relativeConfigFile());
}
// in case url is present - we overwrite the configuration
if (api.config.url) {
api.config = _.merge(api.config, api.parseDbUrl(api.config.url));
}

if (api.config[env]) {
if (options.logging) {
if (args.url) {
console.log('Parsed url ' + this.filteredUrl(args.url));
} else {
console.log('Loaded configuration file "' + this.relativeConfigFile() + '".');
}
console.log('Using environment "' + env + '".');
}

if (this.config[env]) {
if (options.logging) {
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;
}

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

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

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

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

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

try {
config = this.readConfig();
config = api.readConfig();
} catch (e) {
if (options.ignoreConfig) {
config = {};
Expand Down Expand Up @@ -165,7 +199,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 +214,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
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

0 comments on commit 4583aff

Please sign in to comment.