-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcli.js
executable file
·153 lines (124 loc) · 3.49 KB
/
cli.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env node
'use strict';
const readline = require('readline');
const fs = require('fs');
const ConfigStore = require('configstore');
const {prompt} = require('inquirer');
const figures = require('figures');
const pokemon = require('pokemon');
const chalk = require('chalk');
const meow = require('meow');
const pify = require('pify');
const ora = require('ora');
const opn = require('opn');
const got = require('got');
const databases = require('./databases');
const pkg = require('./package');
const {writeFile} = pify(fs);
meow(`
Usage
$ pronto [input]
`);
const config = new ConfigStore(pkg.name);
const waitForEnter = () => {
return new Promise(resolve => {
const handleKeyPress = (ch, key) => {
if (key.name === 'return') {
process.stdin.removeListener('keypress', handleKeyPress);
resolve();
}
};
process.stdin.on('keypress', handleKeyPress);
});
};
const getDbName = value => databases.find(db => db.value === value).name;
const log = (message, color = 'blue') => console.log(`${chalk[color](figures.pointer)} ${message}`);
const askForToken = async () => {
const {answer} = await prompt([{
message: 'Enter Compose token:',
name: 'answer'
}]);
return answer;
};
const askForDatabase = async () => {
const {answer} = await prompt([{
type: 'list',
message: 'Select a database to deploy:',
name: 'answer',
choices: databases
}]);
return answer;
};
const getAccountId = async composeToken => {
const res = await got('https://api.compose.io/2016-07/user', {
headers: {
authorization: `Bearer ${composeToken}`
},
json: true
});
return res.body.id;
};
const deploy = async (composeToken, type, name) => {
const accountId = await getAccountId(composeToken);
const res = await got.post('https://api.compose.io/2016-07/deployments', {
headers: {
authorization: `Bearer ${composeToken}`
},
json: true,
body: {
deployment: {
account_id: accountId, // eslint-disable-line camelcase
datacenter: 'aws:us-east-1',
name,
type
}
}
});
return res.body;
};
const main = async () => {
let composeToken = config.get('composeToken');
if (!composeToken) {
log('To use Pronto, you need to enter your Compose access token', 'grey');
log('Press Enter to open a browser and create a token', 'grey');
await waitForEnter();
opn('https://app.compose.io/oauth/api_tokens');
composeToken = await askForToken();
config.set('composeToken', composeToken);
}
const type = await askForDatabase();
const name = getDbName(type);
const deploymentName = `${pokemon.random().toLowerCase()}-${type}`;
const spinner = ora(`Deploying ${name}`).start();
const db = await deploy(composeToken, type, deploymentName);
const certPath = `${deploymentName}.crt`;
spinner.succeed('Deployed');
await writeFile(certPath, Buffer.from(db.ca_certificate_base64, 'base64'));
console.log([
`${chalk.green(figures.tick)} Connection URL copied to clipboard`,
'',
`${chalk.blue('ℹ')} Connect via CLI:`,
` ${db.connection_strings.cli[0]}`,
'',
`${chalk.blue('ℹ')} Connect directly:`,
` ${db.connection_strings.direct[0]}`,
'',
`${chalk.blue('ℹ')} Certificate saved at ${certPath} in the current directory`,
''
].join('\n'));
};
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', (ch, key) => {
if (key.name === 'esc' || (key.name === 'ctrl' && key.ctrl)) {
process.exit();
}
});
main()
.then(() => {
process.exit();
})
.catch(err => {
console.error(err.stack);
process.exit(1);
});