forked from tabwrangler/tabwrangler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
169 lines (145 loc) · 4.51 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
/* eslint-env node */
const archiver = require('archiver');
const eslint = require('gulp-eslint');
const fs = require('fs');
const gulp = require('gulp');
const gutil = require('gulp-util');
const jest = require('gulp-jest').default;
const rimraf = require('rimraf');
const runSequence = require('run-sequence');
const watch = require('gulp-watch');
const webpack = require('webpack');
const packageJson = require('./package.json');
const webpackConfig = require('./webpack.config.js');
const webpackProductionConfig = require('./webpack.production.config.js');
const DIST_DIRECTORY = 'dist';
// Clean all release artifacts
gulp.task('clean', function(done) {
rimraf(`${__dirname}/${DIST_DIRECTORY}`, done);
});
// Copy any files that don't require pre-processing.
gulp.task('cp', function() {
const cpApp = gulp.src([
'app/css/**',
'app/img/**',
'app/lib/**',
'app/*.html',
], {base: 'app'})
.pipe(gulp.dest(DIST_DIRECTORY));
const cpRoot = gulp.src([
'app/manifest.json',
'MIT-LICENSE.txt',
'README.md',
])
.pipe(gulp.dest(DIST_DIRECTORY));
return [cpApp, cpRoot];
});
gulp.task('lint', function() {
return gulp.src(['**/*.js', `!${DIST_DIRECTORY}/**`, '!node_modules/**', '!coverage/**'])
// eslint() attaches the lint output to the "eslint" property
// of the file object so it can be used by other modules.
.pipe(eslint())
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
.pipe(eslint.failAfterError());
});
gulp.task('test', function () {
process.env.NODE_ENV = 'test';
return gulp.src('**/__tests__').pipe(jest(Object.assign({}, {
config: {
'transformIgnorePatterns': [
'<rootDir>/dist/', '<rootDir>/node_modules/',
],
'transform': {
'^.+\\.jsx?$': 'babel-jest',
},
'verbose': true,
},
})));
});
function webpackLog(stats) {
gutil.log('[webpack]', stats.toString({
chunks: false, // Limit chunk information output; it's slow and not too useful
colors: true,
}));
}
gulp.task('webpack', function(done) {
return webpack(
webpackConfig,
function(err, stats) {
if (err) throw new gutil.PluginError('webpack', err);
webpackLog(stats);
done();
}
);
});
gulp.task('webpack:production', function(done) {
return webpack(
webpackProductionConfig,
function(err, stats) {
if (err) throw new gutil.PluginError('webpack', err);
webpackLog(stats);
done();
}
);
});
gulp.task('webpack:watch', function(done) {
let firstRun = true;
return webpack(Object.assign({}, {watch: true}, webpackConfig), function(err, stats) {
if (err) throw new gutil.PluginError('webpack', err);
webpackLog(stats);
// Call Gulp's `done` callback only once per watch. Calling it more than once is an error.
if (firstRun) {
firstRun = false;
done();
}
});
});
// Watch and re-compile / re-lint when in development.
// eslint-disable-next-line no-unused-vars
gulp.task('watch', function(done) {
watch('app/**/!(*.js)', function() {
gulp.start('cp');
});
watch('app/**/*.js', function() {
gulp.start('lint');
gulp.start('test');
});
watch('__tests__/**/*.js', function() {
gulp.start('lint');
gulp.start('test');
});
gulp.start(['cp', 'lint', 'webpack:watch']);
});
gulp.task('archive', function(done) {
// create a file to stream archive data to.
const output = fs.createWriteStream(`${__dirname}/tabwrangler-${packageJson.version}.zip`);
const archive = archiver('zip');
// listen for all archive data to be written
output.on('close', function() {
console.log(`${archive.pointer()} total bytes`);
console.log('archiver has been finalized and the output file descriptor has closed.');
done();
});
// good practice to catch this error explicitly
archive.on('error', function(err) {
throw err;
});
// pipe archive data to the file
archive.pipe(output);
// append files from a directory
archive.directory(`${DIST_DIRECTORY}/`);
// finalize the archive (ie we are done appending files but streams have to finish yet)
archive.finalize();
});
gulp.task('release', function(done) {
runSequence('clean', 'cp', 'lint', 'test', 'webpack:production', 'archive', function() {
done();
});
});
gulp.task('default', function(done) {
runSequence('cp', 'lint', 'webpack', done);
});