-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.babel.js
More file actions
177 lines (156 loc) · 4.41 KB
/
gulpfile.babel.js
File metadata and controls
177 lines (156 loc) · 4.41 KB
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
import gulp from 'gulp';
import eslint from 'gulp-eslint';
import babel from 'gulp-babel';
import sourcemaps from 'gulp-sourcemaps';
import sloc from 'gulp-sloc';
import bumpVersion from 'gulp-bump';
import runSequence from 'run-sequence';
import del from 'del';
import todo from 'gulp-todo';
import git from 'gulp-git-streamed-wrapper';
import fs from 'fs';
/* ************************************************************************* */
/* Helpers */
/* ************************************************************************* */
/*
* Get any file synchronously
*/
const getFile = (filePath, enc = 'utf8') => JSON.parse(fs.readFileSync(filePath, enc));
/*
* Generate and add todo task before committing.
*/
gulp.task('todo', () => gulp.src('src/**/*.js')
.pipe(todo())
.pipe(gulp.dest('./')));
/*
* CLean whatever directory is passed in.
*/
const clean = distGlob => (
del([
distGlob
])
);
/*
* Copy necessary files to the /dist folder for publishing after transpiling.
*/
const copy = (fileGlob, destDir) => gulp.src(fileGlob)
.pipe(gulp.dest(destDir));
/*
* Hook transpiling step to gulpSrc passed in.
*/
const transpile = (fileGlob, destDir) => {
const babelRC = JSON.parse(JSON.stringify(getFile('./.babelrc')));
delete babelRC.sourceMaps;
return gulp.src(fileGlob)
.pipe(sourcemaps.init())
.pipe(babel(babelRC))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(destDir));
};
/*
* Count lines. Not implemented.
*/
gulp.task('sloc', () => gulp.src(['src/**/*.js']).pipe(sloc()));
/*
* Linter. Not implemented.
*/
gulp.task('lint', () => (
gulp.src(['src/**/*.js', '!./node_modules/**'])
.pipe(eslint({
extends: 'airbnb/base',
parser: 'babel-eslint',
plugins: [
'babel'
],
rules: {
'comma-dangle': [2, 'never'],
'babel/object-shorthand': 1,
'new-cap': [2, { capIsNew: false }],
'arrow-body-style': 0
}
}))
.pipe(eslint.format())
.pipe(eslint.failAfterError())
));
/* ************************************************************************* */
/* Main Tasks */
/* ************************************************************************* */
/*
* Transpile src/* to /dist.
*/
gulp.task('transpile', () => transpile('src/**/*.js', 'dist'));
/*
* Clean/create the dist directory.
*/
gulp.task('clean', () => clean('./dist/*'));
/*
* Copy package.json and README.md to new dist directory.
*/
gulp.task('copy', () => (
copy(['./package.json', './README.md'], 'dist'))
);
/*
* Bump package.json.
* #These are not exposed in any npm script. Used by release tasks.
*/
const bump = (importance, tag) => gulp.src('./package.json')
.pipe(bumpVersion({ type: importance, preid: tag }))
.pipe(gulp.dest('./'));
/*
* Git commands used by release tasks
*/
const gitCB = err => {
if (err) {
throw err;
}
};
const commit = msg => git.commit(msg);
const add = glob => gulp.src(glob).pipe(git.add());
const describe = options => git.exec(options, gitCB);
const push = opts => git.push(null, null, opts);
/*
* After bumping, get the new version and create a tag of it, commit the tag and push with tags
*/
gulp.task('git-bump-tag', () => {
const pkg = getFile('./package.json');
const version = `v${pkg.version}`;
const commitMsg = `Release ${version}`;
const tagMsg = `Version ${pkg.version}`;
const describeOpts = { args: 'describe --tags --always --abbrev=1 --dirty=-d' };
return add('./package.json')
.pipe(commit(commitMsg))
.pipe(git.tag(version, tagMsg, gitCB))
.pipe(describe(describeOpts))
.pipe(push({ args: '--tags' }));
});
/*
* Release tasks.
*/
gulp.task('prerelease', () => bump('prerelease', 'beta'));
gulp.task('patch', () => bump('patch'));
gulp.task('major', () => bump('major'));
gulp.task('minor', () => bump('minor'));
/*
* Release task that update package.json according to what the release does
* according to SemVer and transpile the code.
*/
const release = importance => runSequence(
importance,
'git-bump-tag',
'clean',
['transpile', 'copy']
);
/*
* Core release tasks.
*/
gulp.task('release-patch', () => release('patch'));
gulp.task('release-minor', () => release('minor'));
gulp.task('release-major', () => release('major'));
gulp.task('release-prerelease', () => release('prerelease'));
/*
* Default build for main. No bump.
*/
gulp.task('default', () => runSequence(
'clean',
['transpile', 'copy']
));