-
Notifications
You must be signed in to change notification settings - Fork 1
/
dokku-deploy.js
240 lines (229 loc) · 7.89 KB
/
dokku-deploy.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const inquirer = require('inquirer');
const readline = require('readline');
const semver = require('semver');
const chalk = require('chalk');
const pkg = require('./package.json');
const {wait, execPromise, spawnPromise, readPackageJSON} = require('./lib/utils');
class DokkuDeploy {
constructor() {
this.cwd = process.cwd();
this.checksPass = false;
}
precheck() {
if (this.checksPass) {
return Promise.resolve();
}
return Promise.resolve().then(() => {
return execPromise('[ -d .git ] || git rev-parse --git-dir').catch((err) => {
throw new Error('Not a git repository (or any of the parent directories)');
});
}).then(() => {
return readPackageJSON(this.cwd).catch(() => {
throw new Error('Not a Node.js project');
});
}).then((app) => {
this.project = app;
console.log(chalk.magenta('Dokku Deploy CLI'), chalk.gray(`- ${pkg.version}`));
console.log(chalk.yellow('Project:'), app.name, '-', chalk.cyan(app.version), '\n');
this.checksPass = true;
}).then(() => {
return execPromise('git remote get-url dokku').catch((err) => {
return this.setup();
});
});
}
setup(choice) {
return Promise.resolve().then(() => {
if (!this.project) {
return readPackageJSON(this.cwd).then((app) => {
this.project = app;
}).catch(() => {
throw new Error('Not a Node.js project');
});
}
return Promise.resolve();
}).then(() => {
return execPromise('git remote get-url dokku').catch((err) => null);
}).then((currentURL) => {
if (currentURL) {
return Promise.resolve({choice: true, currentURL: currentURL.trim()});
}
if (!choice) {
console.log(chalk.whiteBright('Looks like this repo isn\'t setup to deploy to dokku.'));
return inquirer.prompt({
type: 'confirm',
name: 'choice',
message: 'Would you like to set it up now?',
default: true
});
}
return {choice};
}).then((answer) => {
if (!answer.choice) {
process.exit(1);
}
const example = chalk.gray(` (e.g ssh://[email protected]/${this.project.name || 'my-project'})`);
return inquirer.prompt({
type: 'input',
name: 'url',
message: `Enter your dokku URL${!answer.currentURL && example || ''}:`,
default: answer.currentURL || undefined
});
}).then((answer) => {
console.log(chalk.gray('Setting up dokku remote URL...'));
return execPromise(`git remote add dokku ${answer.url}`).catch(() => {
return execPromise(`git remote set-url dokku ${answer.url}`);
});
}).then(() => {
console.log(chalk.whiteBright('done! You can now re-run your command'));
process.exit(1);
})
}
interactive() {
return this.precheck().then(() => {
return inquirer.prompt({
type: 'list',
name: 'choice',
message: 'What would you like to do?',
choices: [
{ name: 'Bump, Tag & Deploy', value: 'bump-tag-deploy' },
{ name: 'Bump & Tag', value: 'bump-tag' },
{ name: 'Deploy Tag', value: 'deploy' }
]
});
}).then((answer) => {
switch (answer.choice) {
case 'bump-tag-deploy':
return this.menuBumpTagAndDeploy();
break;
case 'bump-tag':
return this.menuBumpAndTag();
break;
case 'deploy':
return this.menuTagAndDeploy();
break;
}
});
}
menuBumpTagAndDeploy() {
return this.menuBumpAndTag().then((version) => {
return this.deployTag(version);
})
}
menuBumpAndTag() {
return this.precheck().then(() => {
return inquirer.prompt({
type: 'list',
name: 'bump',
message: 'Select the type of bump to perform:',
default: 'patch',
choices: [
{ name: 'Major', value: 'major' },
{ name: 'Minor', value: 'minor' },
{ name: 'Patch', value: 'patch' },
{ name: 'Pre-Major', value: 'pre-major' },
{ name: 'Pre-Minor', value: 'pre-minor' },
{ name: 'Pre-Patch', value: 'pre-patch' },
{ name: 'Pre-Release', value: 'prerelease' }
]
});
}).then((answer) => {
return this.bumpAndTag(answer.bump);
});
}
menuTagAndDeploy() {
return this.precheck().then(() => {
return execPromise('git tag -l --sort="-version:refname"').then((raw) => {
const tags = raw.trim().split('\n');
return inquirer.prompt({
type: 'list',
name: 'tag',
message: 'Select a tag to deploy:',
choices: tags
}).then((answer) => {
return this.deployTag(answer.tag);
});
});
});
}
bumpAndTag(type) {
return this.precheck().then(() => {
return execPromise(`git rev-parse --abbrev-ref HEAD`).then((result) => {
const branch = result.trim()
if (branch !== 'master') {
console.log(chalk.yellowBright(`WARNING: You\'re tagging off "${branch}" not "master".`));
return wait(3, (count) => {
process.stdout.write(chalk.gray(`Press Ctrl+C to cancel. Resuming in ${count}...`));
readline.cursorTo(process.stdout, 0);
}).then(() => {
readline.clearLine(process.stdout, 0);
});
}
});
}).then(() => {
return execPromise(`git status -s`).then((result) => {
if (result) {
throw new Error(`Git working directory not clean.\n${result}`);
}
});
}).then(() => {
return semver.inc(this.project.version, type);
}).then((nextVersion) => {
return execPromise(`git tag -l v${nextVersion}`).then((result) => {
if (result) {
throw new Error(`tag 'v${nextVersion}' already exists`);
}
return nextVersion;
});
}).then((nextVersion) => {
console.log(chalk.gray(`Bumping to the next ${type} version...`));
return execPromise(`npm version ${type}`).then(() => {
return nextVersion;
}).catch(() => {
throw new Error(`"bump-type" argument should be one of ${chalk.bold.whiteBright('<newversion>, major, minor, patch, premajor, preminor, prepatch, prerelease, from-git')}`);
});
}).then((nextVersion) => {
console.log(chalk.gray(`Pushing to remote...`));
return execPromise(`git push && git push --tags`).then(() => {
return nextVersion;
});
}).then((nextVersion) => {
console.log(chalk.whiteBright(`Bumped and tagged version ${chalk.green(nextVersion)}`));
return `v${nextVersion}`;
});
}
deployTag(tag) {
return this.precheck().then(() => {
return execPromise(`git rev-parse ${tag}~0`).then((result) => {
return result.trim();
}).catch(() => {
throw new Error(`Tag "${tag}" not found`);
})
}).then((tagHash) => {
console.log(chalk.gray(`Verifying tag...`));
return execPromise('git ls-remote dokku master').then((result) => {
const [remoteHash] = result.split(/\s+/);
if (remoteHash === tagHash) {
console.log(chalk.whiteBright(`Tag ${chalk.green(tag)} is already deployed!`));
process.exit(0);
}
}).catch(() => null);
}).then(() => {
return wait(3, (count) => {
process.stdout.write(chalk.gray(`Deploying tag ${chalk.green(tag)} in ${count}...`));
readline.cursorTo(process.stdout, 0);
});
}).then(() => {
readline.clearLine(process.stdout, 0);
console.log(chalk.gray(`Deploying tag ${chalk.green(tag)}...`));
return spawnPromise(`git push -f dokku ${tag}~0:refs/heads/master`, (data) => {
process.stdout.write(chalk.gray(data));
}).catch(() => {
throw new new Error(`Failed deploying ${tag}`);
});
}).then(() => {
console.log(chalk.whiteBright(`Tag ${chalk.green(tag)} deployed!`));
});
}
}
module.exports = DokkuDeploy;