|
| 1 | +var CoreObject = require('core-object'); |
| 2 | +var chalk = require('chalk'); |
| 3 | +var blue = chalk.blue; |
| 4 | + |
| 5 | +var DeployPluginBase = CoreObject.extend({ |
| 6 | + context: null, |
| 7 | + ui: null, |
| 8 | + project: null, |
| 9 | + pluginConfig: null, |
| 10 | + defaultConfig: {}, |
| 11 | + configure: function(context) { |
| 12 | + this.log('validating config'); |
| 13 | + var defaultProps = Object.keys(this.defaultConfig || {}); |
| 14 | + defaultProps.forEach(this.applyDefaultConfigProperty.bind(this)); |
| 15 | + var requiredProps = this.requiredConfig || []; |
| 16 | + requiredProps.forEach(this.ensureConfigPropertySet.bind(this)); |
| 17 | + this.log('config ok'); |
| 18 | + }, |
| 19 | + applyDefaultConfigProperty: function(propertyName){ |
| 20 | + if (!this.pluginConfig[propertyName]) { |
| 21 | + var value = this.defaultConfig[propertyName]; |
| 22 | + this.pluginConfig[propertyName] = value; |
| 23 | + var description = value; |
| 24 | + if (typeof description === "function") { |
| 25 | + description = "[Function]"; |
| 26 | + } |
| 27 | + this.log('Missing config: `' + propertyName + '`, using default: `' + description + '`', { color: 'yellow' }); |
| 28 | + } |
| 29 | + }, |
| 30 | + ensureConfigPropertySet: function(propertyName){ |
| 31 | + if (!this.pluginConfig[propertyName]) { |
| 32 | + var message = 'Missing required config: `' + propertyName + '`'; |
| 33 | + this.log(message, { color: 'red' }); |
| 34 | + throw new Error(message); |
| 35 | + } |
| 36 | + }, |
| 37 | + readConfig: function(property){ |
| 38 | + var configuredValue = this.pluginConfig[property]; |
| 39 | + if (typeof configuredValue === 'function') { |
| 40 | + return configuredValue(this.context); |
| 41 | + } |
| 42 | + return configuredValue; |
| 43 | + }, |
| 44 | + log: function(message, opts) { |
| 45 | + opts = opts || { color: 'blue' } |
| 46 | + opts['color'] = opts['color'] || 'blue'; |
| 47 | + |
| 48 | + this.ui.write(blue('| ')); |
| 49 | + var chalkColor = chalk[opts.color]; |
| 50 | + this.ui.writeLine(chalkColor('- ' + message)); |
| 51 | + } |
| 52 | +}); |
| 53 | + |
| 54 | +module.exports = DeployPluginBase; |
0 commit comments