-
Notifications
You must be signed in to change notification settings - Fork 1
/
release.js
executable file
·147 lines (132 loc) · 4.09 KB
/
release.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
#!/usr/bin/env node
const {exec} = require('child_process');
const fs = require('fs');
const path = require('path');
const semver = require('semver');
const ora = require('ora');
const chalk = require('chalk');
const indent = require('detect-indent');
const inquirer = require('inquirer');
const git = require('simple-git')();
const cwd = process.cwd();
const APP = {};
const manifests = [ 'package.json' ];
const dryrun = false;
const faker = arg => new Promise(resolve => setTimeout(() => resolve(arg), 2000));
function run (cmd) {
if (dryrun) return faker();
return new Promise((resolve, reject) => {
exec(cmd, (err, out) => (err ? reject(err) : resolve(out)));
});
}
function getVersion (manifest) {
const pkgPath = path.join(cwd, manifest || manifests[0]);
const pkg = require(pkgPath);
const current = pkg.version || '0.0.0';
return {
name: pkg.name,
current: current,
nextMajor: semver.inc(current, 'major'),
nextMinor: semver.inc(current, 'minor'),
nextPatch: semver.inc(current, 'patch')
};
}
function bump (manifest, newVersion) {
const pkgPath = path.join(cwd, manifest);
const pkg = require(pkgPath);
const usedIndent = indent(fs.readFileSync(pkgPath, 'utf8')).indent || ' ';
pkg.version = newVersion;
if (!dryrun) fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, usedIndent) + '\n');
}
function commit (version) {
if (dryrun) return faker();
return new Promise((resolve, reject) => {
git
.silent(true)
.add('./*')
.commit('Release v' + version)
.push(['origin', 'master'], err => {
if (err) reject(err);
else resolve({version});
});
});
}
function release () {
const app = getVersion();
APP.name = app.name;
let spinner;
console.log('\n********************************');
console.log('* *');
console.log(`* Releasing ${chalk.cyan(APP.name)} *`);
console.log('* *');
console.log('********************************\n');
inquirer
.prompt([
{
type: 'list',
name: 'version',
message: 'Bump version to:',
default: 1,
choices: [
{ value: app.current, name: 'current (' + app.current + ')' },
{ value: app.nextPatch, name: 'patch (' + app.nextPatch + ')' },
{ value: app.nextMinor, name: 'minor (' + app.nextMinor + ')' },
{ value: app.nextMajor, name: 'major (' + app.nextMajor + ')' },
new inquirer.Separator(),
{ value: 'custom', name: 'custom...' },
]
},
{
type: 'input',
name: 'version',
message: 'Enter the new version number:',
default: app.current,
when: answers => answers.version === 'custom',
filter: semver.clean,
validate: answer => semver.valid(answer) ? true : 'That\'s not a valid version number',
}
])
.then(({version}) => {
APP.version = version;
spinner = ora('').start();
// update package & manifest
manifests.forEach(m => {
spinner.text = `Updating ${m}...`;
bump(m, version);
spinner.text = `Updated ${chalk.cyan(m)} to ${chalk.cyan(version)}`;
spinner.succeed();
});
spinner.text = 'Committing to GitHub...';
spinner.start();
return commit(version); // commit code changes to github
})
.then(() => {
spinner.text = `Update ${chalk.cyan('pushed')} to Github.`;
spinner.succeed();
spinner.text = 'Building a docker image...';
spinner.start();
const tagLatest = `tborychowski/${APP.name}:latest`;
const tagVersion = `tborychowski/${APP.name}:${APP.version}`;
return run(`docker build --no-cache -t ${tagLatest} -t ${tagVersion} .`);
})
.then(() => {
spinner.text = 'Built a ' + chalk.cyan('docker image');
spinner.succeed();
spinner.text = 'Pushing images to the docker hub...';
spinner.start();
const cmd = `docker push tborychowski/${APP.name}:latest && ` +
`docker push tborychowski/${APP.name}:${APP.version}`;
return run(cmd).catch(() => {});
})
.then(() => {
spinner.text = 'Images pushed to ' + chalk.cyan('docker hub');
spinner.succeed();
console.log(chalk.cyan('All done!'));
process.exit(0);
})
.catch(e => {
spinner.text = e;
spinner.fail();
});
}
release();