-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathheroku.js
More file actions
195 lines (174 loc) · 7.19 KB
/
Copy pathheroku.js
File metadata and controls
195 lines (174 loc) · 7.19 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const axios = require('axios');
class HerokuHandler {
constructor(props) {
this.herokuApiKey = props.herokuApiKey || null;
this.username = props.username || null;
this.password = props.password || null;
}
async checkInstall() {
try {
const response = await axios.get('https://api.heroku.com/apps', {
headers: {
'Authorization': `Bearer ${this.herokuApiKey}`,
'Accept': 'application/vnd.heroku+json; version=3'
}
});
console.log(`Heroku:checkInstall: CLI version: ${response.data}`);
return true; // Heroku is installed
} catch (err) {
console.error('Heroku:checkInstall CLI is not installed.', err);
return false; // Heroku is not installed
}
}
async checkLogin() {
try {
const response = await axios.get('https://api.heroku.com/account', {
headers: {
'Authorization': `Bearer ${this.herokuApiKey}`,
'Accept': 'application/vnd.heroku+json; version=3'
}
});
console.log(`Heroku:checkLogin:Logged in as ${response.data.email}`);
return true;
} catch (error) {
console.error('Heroku:checkLogin:Not logged in to Heroku.', error);
return false;
}
}
static async handleHeroku(args) {
console.log('~~~~ Start handleHeroku\n');
// Create a Heroku Handler
const heroku = new HerokuHandler(args);
if(!await heroku.checkInstall()){
console.log('HEROKU USER NOT INSTALLED\n')
return {status:false, msg: 'Heroku Not Installed'};
}
if(!await heroku.checkLogin()){
console.log('HEROKU USER NOT LOGGED IN\n')
return {status:false, msg: 'Heroku User Not Logged In'};
}
else{
console.log('HEROKU USER LOGGED IN\n')
return {status:true, msg: 'Heroku User Logged In'}
}
}
}
module.exports = HerokuHandler;
exports = HerokuHandler;
// Benching this.
//
// It was writting using exec & spawn commands to the shell which is hacky when a REST API is available.
// if(config.heroku.apiKey || config.heroku.username){ HerokuHandler.handleHeroku(config.heroku); }
/*
class HerokuHandler {
constructor(props) {
this.checkInstall = this.checkInstall.bind(this);
this.checkLogin = this.checkLogin.bind(this);
this.herokuApiKey = props.herokuApiKey || null;
this.username = props.username || null;
this.password = props.password || null;
this.installed = this.checkInstall();
this.loggedIn = this.installed && this.checkLogin();
}
async checkInstall() {
try {
const command = isWindows ? 'heroku.cmd' : 'heroku';
const { stdout } = await exec(`${command} --version`);
console.log(`Heroku:checkInstall: CLI version: ${stdout}`);
return true; // Heroku is installed
} catch (err) {
console.error('Heroku:checkInstall CLI is not installed.', err);
return false; // Heroku is not installed
}
}
install() {
// Note: This installation script is for Unix-like systems only.
// For Windows, you would have to download the installer and run it.
console.log('Attempting to install Heroku CLI...');
const child = spawn('sh', ['-c', 'curl https://cli-assets.heroku.com/install.sh | sh']);
child.stdout.on('data', (data) => {
console.log(data.toString());
});
child.stderr.on('data', (data) => {
console.error(data.toString());
});
child.on('close', (code) => {
if (code === 0) {
console.log('Heroku CLI installed successfully.');
} else {
console.error('Failed to install Heroku CLI.');
}
});
}
async checkLogin() {
try {
const { stdout } = await exec('heroku whoami');
console.log(`Heroku:checkLogin:Logged in as ${stdout.trim()}`);
return true;
} catch (error) {
console.error('Heroku:checkLogin:Not logged in to Heroku.', error);
return false;
}
}
login() {
const user_input = this.username
console.log('~~~~ ~~~~ Heroku:login:Start \n');
// // Subprocess Must be a standalone executable so we invoke a shell with our command as an argument
const cliProcess = spawn(isWindows ? 'cmd.exe' : 'sh', ["heroku login"]);
cliProcess.stdout.on("data", data => { console.log(`CLI process output: ${data}`); });
cliProcess.stderr.on("data", data => { console.error(`CLI process error: ${data}`); });
cliProcess.on("close", code => { console.log(`CLI process exited with code ${code}`); });
cliProcess.stdin.write(`${user_input}\n`, error => { console.error(`Heroku:Login:Error: writing to CLI process: ${error}`); });
cliProcess.stdout.once("data", data => {
console.log("CLI executed successfully");
console.log(`CLI process output: ${data}`)
if (data.includes("Logged in")) {
console.log("Logged in to Heroku");
// Todo: handleHerokuCommands();
}
else {
console.log("Unable to log in to Heroku");
}
});
}
static async handleHeroku(args) {
console.log('~~~~ Start handleHeroku\n');
// Create a Heroku Handler
const heroku = new HerokuHandler(args);
if(!await heroku.installed){
console.log('HEROKU USER NOT INSTALLED\n')
if (!isWindows){ heroku.install(); } // Optional auto-install
if(!heroku.installed) return {status:false, msg: 'Heroku Not Installed'};
}
if(!await heroku.loggedIn){
console.log('HEROKU USER NOT LOGGED IN\n')
heroku.login(); // Optional auto-install
if(!heroku.loggedIn) return {status:false, msg: 'Heroku User Not Logged In'};
}
else{
console.log('HEROKU USER LOGGED IN\n')
return {status:true, msg: 'Heroku User Logged In'}
}
}
}
*/
/*
heroku login command
- now opens your web browser to complete the login flow
- - stores API tokens in the standard Unix file ~/.netrc ($HOME\_netrc on Windows).
- - - The netrc format is well established and well supported by various network tools on unix.
- - - This way `curl -n` can access the Heroku API with little or no extra work
- - Overwride it using HEROKU_API_KEY environment variable.
- To continue using the interactive, terminal-based login flow, pass the --interactive option to heroku login
- - Error: Your account has MFA enabled; API requests using basic authentication with email and password are not supported.
- - - Please generate an authorization token for API access.
let herokuCommands = `
heroku login
heroku ps:resize basic -a addy-puppeteer
heroku ps:resize Standard-1x -a addy-puppeteer
heroku ps:scale web=2 -a addy-puppeteer
heroku apps:info -a addy-puppeteer
heroku auth:whoami
heroku deploy
`
*/