forked from pcottle/learnGitBranching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
213 lines (179 loc) · 4.7 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
var { execSync } = require('child_process');
var { writeFileSync, readdirSync, readFileSync } = require('fs');
var glob = require('glob');
var _ = require('underscore');
var { src, dest, series, watch } = require('gulp');
var log = require('fancy-log');
var gHash = require('gulp-hash');
var gClean = require('gulp-clean');
var concat = require('gulp-concat');
var cleanCSS = require('gulp-clean-css');
var gTerser = require('gulp-terser');
var gJasmine = require('gulp-jasmine');
var { minify } = require('html-minifier');
var { SpecReporter } = require('jasmine-spec-reporter');
var gJshint = require('gulp-jshint');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var browserify = require('browserify');
var babelify = require('babelify');
_.templateSettings.interpolate = /\{\{(.+?)\}\}/g;
_.templateSettings.escape = /\{\{\{(.*?)\}\}\}/g;
_.templateSettings.evaluate = /\{\{-(.*?)\}\}/g;
// precompile for speed
var indexFile = readFileSync('src/template.index.html').toString();
var indexTemplate = _.template(indexFile);
var compliments = [
'Thanks to Hong4rc for the modern and amazing gulp workflow!',
'I hope you all have a great day :)'
];
var compliment = (done) => {
var index = Math.floor(Math.random() * compliments.length);
log(compliments[index]);
done();
};
const lintStrings = (done) => {
execSync('node src/js/intl/checkStrings');
done();
};
var destDir = './build/';
var buildIndex = function(done) {
log('Building index...');
// first find the one in here that we want
var buildFiles = readdirSync(destDir);
var jsRegex = /bundle-[\.\w]+\.js/;
var jsFile = buildFiles.find(function(name) {
return jsRegex.exec(name);
});
if (!jsFile) {
throw new Error('no hashed min file found!');
}
log('Found hashed js file: ' + jsFile);
var styleRegex = /main-[\.\w]+\.css/;
var styleFile = buildFiles.find(function(name) {
return styleRegex.exec(name);
});
if (!styleFile) {
throw new Error('no hashed css file found!');
}
log('Found hashed style file: ' + styleFile);
// output these filenames to our index template
var outputIndex = indexTemplate({
jsFile,
styleFile,
});
if (process.env.NODE_ENV === 'production') {
outputIndex = minify(outputIndex, {
minifyJS: true,
collapseWhitespace: true,
processScripts: ['text/html'],
removeComments: true,
});
}
writeFileSync('index.html', outputIndex);
done();
};
var getBundle = function() {
return browserify({
entries: [...glob.sync('src/**/*.js'), ...glob.sync('src/**/*.jsx')],
debug: true,
})
.transform(babelify, { presets: ['@babel/preset-react'] })
.bundle()
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(gHash());
};
var clean = function () {
return src(destDir, { read: false, allowEmpty: true })
.pipe(gClean());
};
var jshint = function() {
return src([
'gulpfile.js',
'__tests__/',
'src/'
])
.pipe(gJshint())
.pipe(gJshint.reporter('default'));
};
var ifyBuild = function() {
return getBundle()
.pipe(dest(destDir));
};
var miniBuild = function() {
process.env.NODE_ENV = 'production';
return getBundle()
.pipe(gTerser())
.pipe(dest(destDir));
};
var style = function() {
var chain = src('src/style/*.css')
.pipe(concat('main.css'));
if (process.env.NODE_ENV === 'production') {
chain = chain.pipe(cleanCSS());
}
return chain.pipe(gHash())
.pipe(dest(destDir));
};
var jasmine = function() {
return src('__tests__/*.spec.js')
.pipe(gJasmine({
config: {
verbose: true,
random: false,
},
reporter: new SpecReporter(),
}));
};
var gitAdd = function(done) {
execSync('git add build/');
done();
};
var gitDeployMergeMain = function(done) {
execSync('git checkout gh-pages && git merge main -m "merge main"');
done();
};
var gitDeployPushOrigin = function(done) {
execSync('git commit -am "rebuild for prod"; ' +
'git push origin gh-pages --force && ' +
'git branch -f trunk gh-pages && ' +
'git checkout main'
);
done();
};
var fastBuild = series(clean, ifyBuild, style, buildIndex, jshint);
var build = series(
clean,
miniBuild, style, buildIndex,
gitAdd, jasmine, jshint,
lintStrings, compliment
);
var deploy = series(
clean,
jasmine,
jshint,
gitDeployMergeMain,
build,
gitDeployPushOrigin,
compliment
);
var lint = series(jshint, compliment);
var watching = function() {
return watch([
'gulpfile.js',
'__tests__/git.spec.js',
'src/js/**/*.js',
'src/js/**/**/*.js',
'src/levels/**/*.js'
], series([fastBuild , jasmine, jshint, lintStrings]));
};
module.exports = {
default: build,
lint,
fastBuild,
watching,
build,
test: jasmine,
deploy,
};