|
4 | 4 | var Promise = require('ember-cli/lib/ext/promise');
|
5 | 5 | var fs = require('fs');
|
6 | 6 |
|
7 |
| -var chalk = require('chalk'); |
8 |
| -var blue = chalk.blue; |
9 |
| -var red = chalk.red; |
10 |
| - |
11 | 7 | var denodeify = require('rsvp').denodeify;
|
12 | 8 | var readFile = denodeify(fs.readFile);
|
13 | 9 |
|
14 |
| -var validateConfig = require('./lib/utilities/validate-config'); |
| 10 | +var DeployPluginBase = require('ember-cli-deploy-plugin'); |
15 | 11 |
|
16 | 12 | module.exports = {
|
17 | 13 | name: 'ember-cli-deploy-redis',
|
18 | 14 |
|
19 | 15 | createDeployPlugin: function(options) {
|
20 | 16 | var Redis = require('./lib/redis');
|
21 | 17 |
|
22 |
| - function _readFileContents(path) { |
23 |
| - return readFile(path) |
24 |
| - .then(function(buffer) { |
25 |
| - return buffer.toString(); |
26 |
| - }); |
27 |
| - } |
28 |
| - |
29 |
| - function _beginUploadMessage(ui, indexPath) { |
30 |
| - ui.write(blue('| ')); |
31 |
| - ui.write(blue('- Uploading `' + indexPath + '`\n')); |
32 |
| - |
33 |
| - return Promise.resolve(); |
34 |
| - } |
35 |
| - |
36 |
| - function _beginActivateMessage(ui, revisionKey) { |
37 |
| - ui.write(blue('| ')); |
38 |
| - ui.write(blue('- Activating revision `' + revisionKey + '`\n')); |
39 |
| - |
40 |
| - return Promise.resolve(); |
41 |
| - } |
42 |
| - |
43 |
| - function _successMessage(ui, key) { |
44 |
| - ui.write(blue('| ')); |
45 |
| - ui.write(blue('- Uploaded with key `' + key + '`\n')); |
46 |
| - |
47 |
| - return Promise.resolve(key); |
48 |
| - } |
49 |
| - |
50 |
| - function _activationSuccessMessage(ui, revisionKey) { |
51 |
| - ui.write(blue('| ')); |
52 |
| - ui.write(blue('- ✔ Activated revision `' + revisionKey + '`\n')); |
53 |
| - |
54 |
| - return Promise.resolve(); |
55 |
| - } |
56 |
| - |
57 |
| - function _errorMessage(ui, error) { |
58 |
| - ui.write(blue('| ')); |
59 |
| - ui.write(red('- ' + error + '`\n')); |
60 |
| - |
61 |
| - return Promise.reject(error); |
62 |
| - } |
63 |
| - |
64 |
| - return { |
| 18 | + var DeployPlugin = DeployPluginBase.extend({ |
65 | 19 | name: options.name,
|
| 20 | + defaultConfig: { |
| 21 | + host: 'localhost', |
| 22 | + port: 6379, |
| 23 | + filePattern: 'dist/index.html', |
| 24 | + keyPrefix: function(context){ |
| 25 | + return context.project.name() + ':index'; |
| 26 | + }, |
| 27 | + didDeployMessage: function(context){ |
| 28 | + if (context.revisionKey && !context.activatedRevisionKey) { |
| 29 | + return "Deployed but did not activate revision " + context.revisionKey + ". " |
| 30 | + + "To activate, run: " |
| 31 | + + "ember activate " + context.revisionKey + " --environment=" + context.deployEnvironment + "\n"; |
| 32 | + } |
| 33 | + }, |
| 34 | + revisionKey: function(context) { |
| 35 | + return context.commandLineArgs.revisionKey || context.revisionKey; |
| 36 | + }, |
| 37 | + redisDeployClient: function(context) { |
| 38 | + return context.redisDeployClient || new Redis(context.config.redis); |
| 39 | + } |
| 40 | + }, |
| 41 | + configure: function(/* context */) { |
| 42 | + this.log('validating config'); |
66 | 43 |
|
67 |
| - configure: function(context) { |
68 |
| - var deployment = context.deployment; |
69 |
| - var ui = deployment.ui; |
70 |
| - var config = deployment.config[this.name] = deployment.config[this.name] || {}; |
71 |
| - var projectName = deployment.project.name(); |
| 44 | + if (!this.pluginConfig.url) { |
| 45 | + ['host', 'port'].forEach(this.applyDefaultConfigProperty.bind(this)); |
| 46 | + } |
| 47 | + ['filePattern', 'keyPrefix', 'revisionKey', 'didDeployMessage', 'redisDeployClient'].forEach(this.applyDefaultConfigProperty.bind(this)); |
72 | 48 |
|
73 |
| - return this._resolvePipelineData(config, context) |
74 |
| - .then(validateConfig.bind(this, ui, config, projectName)); |
| 49 | + this.log('config ok'); |
75 | 50 | },
|
76 | 51 |
|
77 |
| - upload: function(context) { |
78 |
| - var deployment = context.deployment; |
79 |
| - var ui = deployment.ui; |
80 |
| - var config = deployment.config[this.name] || {}; |
81 |
| - var redis = context.redisClient || new Redis(config); |
82 |
| - var revisionKey = this._resolveConfigValue('revisionKey', config, context); |
| 52 | + upload: function(/* context */) { |
| 53 | + var redisDeployClient = this.readConfig('redisDeployClient'); |
| 54 | + var revisionKey = this.readConfig('revisionKey'); |
| 55 | + var filePattern = this.readConfig('filePattern'); |
| 56 | + var keyPrefix = this.readConfig('keyPrefix'); |
83 | 57 |
|
84 |
| - var filePattern = config.filePattern; |
85 |
| - |
86 |
| - return _beginUploadMessage(ui, filePattern) |
87 |
| - .then(_readFileContents.bind(this, filePattern)) |
88 |
| - .then(redis.upload.bind(redis, config.keyPrefix, revisionKey)) |
89 |
| - .then(_successMessage.bind(this, ui)) |
| 58 | + this.log('Uploading `' + filePattern + '`'); |
| 59 | + return this._readFileContents(filePattern) |
| 60 | + .then(redisDeployClient.upload.bind(redisDeployClient, keyPrefix, revisionKey)) |
| 61 | + .then(this._uploadSuccessMessage.bind(this)) |
90 | 62 | .then(function(key) {
|
91 |
| - return { redisKey: key } |
| 63 | + return { redisKey: key }; |
92 | 64 | })
|
93 |
| - .catch(_errorMessage.bind(this, ui)); |
| 65 | + .catch(this._errorMessage.bind(this)); |
94 | 66 | },
|
95 | 67 |
|
96 |
| - activate: function(context) { |
97 |
| - var deployment = context.deployment; |
98 |
| - var ui = deployment.ui; |
99 |
| - var config = deployment.config[this.name] || {}; |
100 |
| - var redis = context.redisClient || new Redis(config); |
101 |
| - var revisionKey = this._resolveConfigValue('revisionKey', config, context); |
| 68 | + activate: function(/* context */) { |
| 69 | + var redisDeployClient = this.readConfig('redisDeployClient'); |
| 70 | + var revisionKey = this.readConfig('revisionKey'); |
| 71 | + var keyPrefix = this.readConfig('keyPrefix'); |
102 | 72 |
|
103 |
| - return _beginActivateMessage(ui, revisionKey) |
104 |
| - .then(redis.activate.bind(redis, config.keyPrefix, revisionKey)) |
| 73 | + this.log('Activating revision `' + revisionKey + '`'); |
| 74 | + return Promise.resolve(redisDeployClient.activate(keyPrefix, revisionKey)) |
| 75 | + .then(this.log.bind(this, '✔ Activated revision `' + revisionKey + '`')) |
105 | 76 | .then(function(){
|
106 |
| - context.activatedRevisionKey = revisionKey; |
| 77 | + return { |
| 78 | + activatedRevisionKey: revisionKey |
| 79 | + }; |
107 | 80 | })
|
108 |
| - .then(_activationSuccessMessage.bind(this, ui, revisionKey)) |
109 |
| - .catch(_errorMessage.bind(this, ui)); |
| 81 | + .catch(this._errorMessage.bind(this)); |
110 | 82 | },
|
111 | 83 |
|
112 |
| - didDeploy: function(context){ |
113 |
| - var deployment = context.deployment; |
114 |
| - var ui = deployment.ui; |
115 |
| - var config = deployment.config[this.name] || {}; |
116 |
| - var didDeployMessage = this._resolveConfigValue('didDeployMessage', config, context); |
| 84 | + didDeploy: function(/* context */){ |
| 85 | + var didDeployMessage = this.readConfig('didDeployMessage'); |
117 | 86 | if (didDeployMessage) {
|
118 |
| - ui.write(blue('| ')); |
119 |
| - ui.write(blue(didDeployMessage + '`\n')); |
| 87 | + this.log(didDeployMessage); |
120 | 88 | }
|
121 |
| - return Promise.resolve(); |
122 | 89 | },
|
123 | 90 |
|
124 |
| - _resolvePipelineData: function(config, context) { |
125 |
| - config.revisionKey = config.revisionKey || function(context) { |
126 |
| - return context.deployment.commandLineArgs.revisionKey || context.revisionKey; |
127 |
| - }; |
128 |
| - |
129 |
| - return Promise.resolve(); |
| 91 | + _readFileContents: function(path) { |
| 92 | + return readFile(path) |
| 93 | + .then(function(buffer) { |
| 94 | + return buffer.toString(); |
| 95 | + }); |
130 | 96 | },
|
131 | 97 |
|
132 |
| - _resolveConfigValue: function(key, config, context) { |
133 |
| - if(typeof config[key] === 'function') { |
134 |
| - return config[key](context); |
135 |
| - } |
| 98 | + _uploadSuccessMessage: function(key) { |
| 99 | + this.log('Uploaded with key `' + key + '`'); |
| 100 | + return Promise.resolve(key); |
| 101 | + }, |
136 | 102 |
|
137 |
| - return config[key]; |
| 103 | + _errorMessage: function(error) { |
| 104 | + this.log(error, { color: 'red' }); |
| 105 | + return Promise.reject(error); |
138 | 106 | }
|
139 |
| - }; |
| 107 | + }); |
| 108 | + return new DeployPlugin(); |
140 | 109 | }
|
141 | 110 | };
|
0 commit comments