forked from Workfront/workfront-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
314 lines (268 loc) · 7.77 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* Copyright 2015 Workfront
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
"use strict";
var gulp = require('gulp-help')(require('gulp'));
var BUILD_DIR = './dist/';
var COVERAGE_DIR = './coverage/';
gulp.task('default', false, ['help']);
/**
* Empties BUILD_DIR and cleans coverage data
*/
gulp.task('clean', 'Empty '+BUILD_DIR+' folder and remove generated coverage data from '+COVERAGE_DIR, ['clean-coverage', 'clean-build']);
/**
* Empties BUILD_DIR
*/
gulp.task('clean-build', false, [], function(cb) {
var del = require('del');
del([BUILD_DIR + '*'], cb);
});
/**
* Cleans coverage data
*/
gulp.task('clean-coverage', false, [], function(cb) {
var del = require('del');
del([COVERAGE_DIR], cb);
});
/**
* Generates browser-ready version for API in BUILD_DIR
* File will be named as workfront.js, minified version will be workfront.min.js
*/
gulp.task('build', 'Generates browser-ready version for API in '+BUILD_DIR, ['clean-build'], function() {
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
return browserify(
'./index.js',
{
standalone: 'Workfront'
}
)
.ignore('promise/polyfill')
.exclude('./plugins/upload')
.bundle()
.pipe(source('workfront.js'))
.pipe(buffer())
.pipe(gulp.dest(BUILD_DIR))
.pipe(rename({ extname: '.min.js' }))
.pipe(uglify())
.pipe(gulp.dest(BUILD_DIR));
});
function generateDocs(destinationPath) {
var jsdoc = require("gulp-jsdoc");
return gulp.src(["src/**/*.js", "README.md"])
.pipe(
jsdoc(destinationPath, {
path: 'ink-docstrap',
systemName: 'workfront-api',
//footer: "Something",
//copyright: "Something",
navType: "vertical",
theme: "united",
linenums: true,
collapseSymbols: false,
inverseNav: false
})
);
}
function publishDocs(cb) {
var shell = require('shelljs'),
path = require('path'),
os = require('os'),
dateformat = require('dateformat');
if (!shell.which('git')) {
cb('Sorry, git command was not found.');
return;
}
var ghPagesDir = path.join(os.tmpdir(), 'gh-pages'),
currentDir = process.cwd();
shell.rm('-rf', ghPagesDir);
shell.mkdir(ghPagesDir);
shellExec('git clone -b gh-pages https://github.com/Workfront/workfront-api.git "'+ghPagesDir+'"');
shell.rm('-rf', path.join(ghPagesDir, '*'));
var stream = generateDocs(ghPagesDir);
stream.on('finish', function() {
shell.cd(ghPagesDir);
shellExec('git add -A .');
shellExec('git commit -m "Autogenerated new docs at ' + dateformat(new Date()) + '"');
shellExec('git fetch origin && git rebase origin/gh-pages');
shellExec('git push origin gh-pages');
shell.cd(currentDir);
shell.rm('-rf', ghPagesDir);
cb();
});
stream.on('error', function(e) {
cb('Error ' + e.name + ': ' + e.message);
});
}
/**
* Splits a command result to separate lines.
* @param {String} result The command result string.
* @returns {String[]} The separated lines.
*/
function splitCommandResultToLines(result) {
return result.trim().split("\n");
}
/**
* Returns sorted array of version tags
* @returns {String[]}
*/
function getVersionTags() {
var semver = require('semver');
var tags = splitCommandResultToLines(shellExec("git tag", { silent: true }));
return tags.reduce(function(list, tag) {
if (semver.valid(tag)) {
list.push(tag);
}
return list;
}, []).sort(semver.compare);
}
function generateChangelog() {
var shell = require('shelljs'),
dateformat = require('dateformat');
// get most recent two tags
var tags = getVersionTags(),
rangeTags,
now = new Date(),
timestamp = dateformat(now, "mmmm d, yyyy");
var header;
if (tags.length > 1) {
rangeTags = tags.slice(tags.length - 2);
header = rangeTags[1] + " - " + timestamp + "\n";
}
else if (tags.length === 1) {
rangeTags = tags.slice(tags.length - 1);
header = rangeTags[0] + " - " + timestamp + "\n";
}
else {
rangeTags = [];
header = timestamp + "\n";
}
// output header
header.to("CHANGELOG.tmp");
// get log statements
var logs = shellExec("git log --pretty=format:\"* %s (%an)\" " + rangeTags.join(".."), {silent: true}).split(/\n/g);
logs = logs.filter(function(line) {
return line.indexOf("Merge pull request") === -1 && line.indexOf("Merge branch") === -1;
});
logs.push(""); // to create empty lines
logs.unshift("");
// output log statements
logs.join("\n").toEnd("CHANGELOG.tmp");
shell.cat("CHANGELOG.tmp", "CHANGELOG.md").to("CHANGELOG.md.tmp");
shell.rm("CHANGELOG.tmp");
shell.rm("CHANGELOG.md");
shell.mv("CHANGELOG.md.tmp", "CHANGELOG.md");
}
/**
* Creates a release version tag and pushes to origin.
* @param {String} type The type of release to do (patch, minor, major)
* @param {Function} cb Callback
* @returns {void}
*/
function release(type, cb) {
var testsStream = runTests();
testsStream.on('error', cb);
testsStream.on('end', function() {
try {
var newVersion = shellExec("npm version " + type, { silent: true }).trim();
generateChangelog();
// add changelog to commit
shellExec("git add CHANGELOG.md");
shellExec("git commit --amend --no-edit");
// replace existing tag
shellExec("git tag -f " + newVersion);
// push all the things
shellExec("git push origin master --tags");
shellExec("npm publish");
publishDocs(cb);
}
catch(e) {
cb(e);
}
});
}
function shellExec() {
var shell = require('shelljs');
var execResult = shell.exec.apply(shell, arguments);
if (execResult.code) {
throw new Error(execResult.output);
}
return execResult.output;
}
/**
* Generate API documentation in ./docs/ folder
*/
gulp.task('docs', 'Generate API documentation in ./docs/ folder', [], function() {
return generateDocs('./docs/');
});
/**
* Generate and publish API documentation to gh-pages branch
*/
gulp.task('publish-docs', 'Generate and publish API documentation to http://workfront.github.io/workfront-api/', [], function(cb) {
publishDocs(cb);
});
function runTests() {
var mocha = require('gulp-mocha');
return gulp.src('test/**/*.spec.js', {read: false})
.pipe(mocha({}));
}
/**
* Do patch release
*/
gulp.task('release-patch', 'Do patch release', [], function(cb) {
release('patch', cb);
});
/**
* Do minor release
*/
gulp.task('release-minor', 'Do minor release', [], function(cb) {
release('minor', cb);
});
/**
* Do major release
*/
gulp.task('release-major', 'Do major release', [], function(cb) {
release('major', cb);
});
/**
* Runs all tests
*/
gulp.task('test', 'Run all tests', [], runTests);
/**
* Runs all tests with coverage
*/
gulp.task('test-coverage', 'Run all tests and generate coverage data in '+COVERAGE_DIR+' folder', ['clean-coverage'], function(cb) {
var istanbul = require('gulp-istanbul');
gulp.src(['src/**/*.js'])
.pipe(istanbul()) // Covering files
.pipe(istanbul.hookRequire()) // Force `require` to return covered files
.on('finish', function () {
runTests()
.pipe(istanbul.writeReports()) // Creating the reports after tests runned
.on('end', cb);
});
});
/**
* This intended to be run only on Travis CI.
* Runs all tests with coverage, when upload coverage data to coveralls.io
*/
gulp.task('test-ci', false, ['test-coverage'], function() {
var coveralls = require('gulp-coveralls');
return gulp.src(COVERAGE_DIR + 'lcov.info')
.pipe(coveralls());
});