forked from jaydev/ember-cli-deploy-nanobox-redis-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (61 loc) · 1.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* eslint-env node */
'use strict';
const BasePlugin = require('ember-cli-deploy-plugin');
const { spawn } = require('child_process');
const redis = require('redis');
module.exports = {
name: 'ember-cli-deploy-nanobox-redis-tunnel',
createDeployPlugin(options) {
const DeployPlugin = BasePlugin.extend({
name: options.name,
defaultConfig: {port: 6379},
requiredConfig: ['remote', 'component'],
setup() {
const remote = this.readConfig('remote'),
component = this.readConfig('component'),
port = this.readConfig('port');
const proc = spawn('nanobox', [
'tunnel',
remote,
component,
'-p',
port
]);
this.log(
`Connected to component \`${component}\` on port \`${port}\` ` +
`on remote \`${remote}\``,
{verbose: true}
);
// Wait for the connection to establish before returning
return this._waitForConnection(port, proc);
},
teardown(context) {
this.log('Closing Redis tunnel', {verbose: true});
context.tunnel.kill('SIGTERM');
},
_waitForConnection(port, proc) {
return new Promise((resolve, reject) => {
const client = redis.createClient({
host: 'localhost',
port: port
});
client.on('error', err => {
if (err.code === 'ECONNREFUSED') {
this.log(
'Connection refused. Tunnel may not be ready. Retrying...',
{verbose: true}
);
} else {
reject(err.toString());
}
});
client.on('ready', () => {
this.log('Connection ready', {verbose: true});
resolve({tunnel: proc});
});
});
}
});
return new DeployPlugin();
}
};