forked from bigcommerce/stencil-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
155 lines (144 loc) · 5.17 KB
/
gulpfile.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
/**
* These gulp tasks are used to release stencil-cli itself
* (in contrast with /bin/stencil-release.js which is used to release themes)
*/
/* eslint-disable node/no-unpublished-require */
require('colors');
require('path');
const bump = require('gulp-bump');
const exec = require('gulp-exec');
const git = require('gulp-git-streamed');
const gulp = require('gulp');
const gulpif = require('gulp-if');
const gutil = require('gulp-util');
const prompt = require('gulp-prompt');
const semver = require('semver');
const changelog = require('./tasks/changelog');
const { PACKAGE_INFO } = require('./constants');
const packageLock = require('./package-lock.json');
const currentVersion = PACKAGE_INFO.version;
const supportedLockFileVersion = [1];
let branch;
let remote;
let responses;
let targetVersion;
function logError(err) {
if (err) {
gutil.log(err);
}
}
function bumpTask() {
const nextPatchVersion = semver.inc(currentVersion, 'patch');
const nextMinorVersion = semver.inc(currentVersion, 'minor');
const nextMajorVersion = semver.inc(currentVersion, 'major');
const questions = [
{
type: 'list',
name: 'type',
message: 'What type of release would you like to do?',
choices: [
{
value: 'patch',
name:
'Patch: '.yellow +
nextPatchVersion.yellow +
' Backwards-compatible bug fixes.',
},
{
value: 'minor',
name:
'Minor: '.yellow +
nextMinorVersion.yellow +
' Component release or significant update to existing one.',
},
{
value: 'major',
name: 'Major: '.yellow + nextMajorVersion.yellow + ' Major UI refresh.',
},
{
value: 'custom',
name: 'Custom: ?.?.?'.yellow + ' Specify version...',
},
],
},
{
type: 'input',
name: 'custom-version',
message: 'What specific version would you like',
when: (answers) => answers.type === 'custom',
validate: (value) => {
const valid = semver.valid(value) && true;
return valid || 'Must be a valid semver, such as 1.2.3';
},
},
{
name: 'confirmPush',
type: 'confirm',
message: 'Do you want to push this new tag?',
},
{
name: 'pushTo',
type: 'input',
message: 'Where would you like to push to?',
default: 'origin master',
when: (answers) => answers.confirmPush,
validate: (pushResponse) => pushResponse.split(' ').length === 2,
},
];
return gulp
.src('package.json')
.pipe(
prompt.prompt(questions, (res) => {
[remote, branch] = res.pushTo.split(' ');
targetVersion =
res.type !== 'custom'
? semver.inc(currentVersion, res.type)
: res['custom-version'];
responses = res;
if (!supportedLockFileVersion.includes(packageLock.lockfileVersion)) {
throw new Error(
`Release script only supports version ${supportedLockFileVersion}`,
);
}
return (
gulp
.src(['package.json', 'package-lock.json'])
// bump the version number in those files
.pipe(bump({ version: targetVersion }))
// save it back to filesystem
.pipe(gulp.dest(process.cwd()))
// change last modified date
.pipe(exec('touch -c package.json'))
// Fetch Remote Tags
.pipe(git.exec({ args: `fetch ${remote} --tags` }, logError))
);
}),
)
.on('error', logError);
}
function pushTask() {
return (
gulp
.src(['package.json', 'package-lock.json', 'CHANGELOG.md'])
// Add files
.pipe(git.add())
// Commit the changed version number
.pipe(git.commit(`docs(release): releasing ${targetVersion}`))
// Create a Tag
.pipe(git.tag(targetVersion, targetVersion, logError))
// Push Changes
.pipe(gulpif(responses.confirmPush, git.push(remote, branch, logError)))
// Push Tags
.pipe(
gulpif(
responses.confirmPush,
git.push(remote, branch, { args: '--follow-tags' }, logError),
),
)
.on('error', logError)
);
}
gulp.task('bump', bumpTask);
gulp.task('changelog', (done) => changelog.changelogTask({}, done));
gulp.task('push', pushTask);
gulp.task('release', gulp.series('bump', 'changelog', 'push'));