-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
gulpfile.js
229 lines (194 loc) · 7.4 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
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
'use strict';
const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
const fancyLog = require('fancy-log');
const acolors = require('ansi-colors');
const PluginError = require('plugin-error');
const gulpShell = require('gulp-shell');
const execSync = require('child_process').execSync;
const jestCli = require('jest-cli');
const gulpEslint = require('gulp-eslint');
const gulpNsp = require('gulp-nsp');
const gulpPlumber = require('gulp-plumber');
const gulpCoveralls = require('gulp-coveralls');
//Bumping, Releasing tools
const gulpGit = require('gulp-git');
const gulpBump = require('gulp-bump');
const gulpConventionalChangelog = require('gulp-conventional-changelog');
const conventionalGithubReleaser = require('conventional-github-releaser');
const runSequence = require('run-sequence');
const through = require('through2');
const toc = require('markdown-toc');
const yargs = require('yargs');
const argv = yargs
.option('version', {
alias: 'v',
describe: 'Enter Version to bump to',
choices: ['patch', 'minor', 'major'],
type: 'string'
})
.option('ghToken', {
alias: 'gh',
describe: 'Enter Github Token for releasing',
type: 'string'
})
.version(false) // disable default --version from yargs( since v9.0.0)
.argv;
const getPackageJsonVersion = () => {
// We parse the json file instead of using require because require caches
// multiple calls so the version number won't be updated
return JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;
};
const isOK = condition => {
return condition ? acolors.green('[OK]') : acolors.red('[KO]');
};
const readyToRelease = () => {
let isTravisPassing = /build #\d+ passed/.test(execSync('npm run check-travis').toString().trim());
let isAppveyorPassing = /AppVeyor build status: success/.test(execSync('npm run check-appveyor').toString().trim());
let onMasterBranch = execSync('git symbolic-ref --short -q HEAD').toString().trim() === 'master';
let canBump = !!argv.version;
let canGhRelease = argv.ghToken || process.env.CONVENTIONAL_GITHUB_RELEASER_TOKEN;
let canNpmPublish = !!execSync('npm whoami').toString().trim() && execSync('npm config get registry').toString().trim() === 'https://registry.npmjs.org/';
fancyLog(`[travis-ci] Travis build on 'master' branch is passing............................................${isOK(isTravisPassing)}`);
fancyLog(`[appveyor-ci] Appveyor build on 'master' branch is passing..........................................${isOK(isAppveyorPassing)}`);
fancyLog(`[git-branch] User is currently on 'master' branch..................................................${isOK(onMasterBranch)}`);
fancyLog(`[npm-publish] User is currently logged in to NPM Registry...........................................${isOK(canNpmPublish)}`);
fancyLog(`[bump-version] Option '--version' provided, with value : 'major', 'minor' or 'patch'.................${isOK(canBump)}`);
fancyLog(`[github-release] Option '--ghToken' provided or 'CONVENTIONAL_GITHUB_RELEASER_TOKEN' variable set......${isOK(canGhRelease)}`);
return isTravisPassing && isAppveyorPassing && onMasterBranch && canBump && canGhRelease && canNpmPublish;
};
const tocfy = () => {
return through.obj((file, encoding, cb) => {
if (file.isNull()) {
return cb(null, file);
}
if (file.isStream()) {
return cb(new PluginError('tocfy', 'Streaming not supported'));
}
if (!file.contents.length) {
return cb(null, file);
}
let readme = fs.readFileSync(file.path, 'utf-8');
file.contents = new Buffer(toc.insert(readme));
cb(null, file);
});
};
gulp.task('static', () => {
return gulp.src(['app/*.js', 'tests/**/*.js'])
.pipe(gulpPlumber())
.pipe(gulpEslint())
.pipe(gulpEslint.format())
.pipe(gulpEslint.failAfterError());
});
gulp.task('nsp', cb => {
gulpNsp({ package: path.resolve('package.json') }, cb);
});
gulp.task('toc', () => {
return gulp.src('./README.md')
.pipe(tocfy())
.pipe(gulp.dest('./'));
});
gulp.task('test', cb => {
let isTravis = !!process.env.TRAVIS;
jestCli.runCLI({ config: require('./package.json').jest, coverage: true, runInBand: isTravis, ci: isTravis}, ".", function(result) {
cb(result.success ? undefined: 'There are test failures!');
});
});
// Watch changes on *.js files and test
gulp.task('test:watch', () => {
jestCli.runCLI({ config: require('./package.json').jest, watch: true}, ".", function(result) {
cb(result.success ? undefined: 'There are test failures!');
});
});
gulp.task('coveralls', ['test'], () => {
if (!process.env.CI) {
return;
}
return gulp.src(path.join(__dirname, 'coverage/lcov.info'))
.pipe(gulpCoveralls());
});
// Release Tasks
gulp.task('changelog', () => {
return gulp.src('CHANGELOG.md', { buffer: false })
.pipe(gulpPlumber())
.pipe(gulpConventionalChangelog({ preset: 'angular', releaseCount: 0 }))
.pipe(gulp.dest('./'));
});
gulp.task('github-release', (cb) => {
if (!argv.ghToken && !process.env.CONVENTIONAL_GITHUB_RELEASER_TOKEN) {
fancyLog(acolors.red(`You must specify a Github Token via '--ghToken' or set environment variable 'CONVENTIONAL_GITHUB_RELEASER_TOKEN' to allow releasing on Github`));
throw new Error(`Missing '--ghToken' argument and environment variable 'CONVENTIONAL_GITHUB_RELEASER_TOKEN' not set`);
}
conventionalGithubReleaser(
{
type: 'oauth',
token: argv.ghToken || process.env.CONVENTIONAL_GITHUB_RELEASER_TOKEN
},
{ preset: 'angular' },
cb);
});
gulp.task('bump-version', () => {
if (!argv.version) {
fancyLog(acolors.red(`You must specify which version to bump to (Possible values: 'major', 'minor', and 'patch')`));
throw new Error(`Missing '--version' argument`);
}
return gulp.src('./package.json')
.pipe(gulpPlumber())
.pipe(gulpBump({ type: argv.version }))
.pipe(gulp.dest('./'));
});
gulp.task('commit-changes', () => {
let version = getPackageJsonVersion();
return gulp.src('.')
.pipe(gulpPlumber())
.pipe(gulpGit.add())
.pipe(gulpGit.commit(`chore(release): bump version number to ${version}`));
});
gulp.task('push-changes', (cb) => {
gulpGit.push('origin', 'master', cb);
});
gulp.task('create-new-tag', (cb) => {
let version = `v${getPackageJsonVersion()}`;
gulpGit.tag(version, `chore(release): :sparkles: :tada: create tag for version ${version}`, (error) => {
if (error) {
return cb(error);
}
gulpGit.push('origin', 'master', { args: '--tags' }, cb);
});
});
gulp.task('build', ['static', 'test'])
gulp.task('npm-publish', gulpShell.task('npm publish'));
gulp.task('pre-release', cb => {
readyToRelease();
cb();
});
gulp.task('release', (cb) => {
fancyLog('# Performing Pre-Release Checks...');
if (!readyToRelease()) {
fancyLog(acolors.red('# Pre-Release Checks have failed. Please fix them and try again. Aborting...'));
cb();
}
else {
fancyLog(acolors.green('# Pre-Release Checks have succeeded. Continuing...'));
runSequence(
'static',
'bump-version',
'changelog',
'commit-changes',
'push-changes',
'create-new-tag',
'github-release',
'npm-publish',
(error) => {
if (error) {
fancyLog(error.message);
} else {
fancyLog(acolors.green('RELEASE FINISHED SUCCESSFULLY'));
}
cb(error);
});
}
});
gulp.task('prepublish', ['nsp']);
gulp.task('default', ['static', 'test', 'coveralls']);