-
Notifications
You must be signed in to change notification settings - Fork 36
/
gulpfile.js
294 lines (254 loc) · 9.35 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
'use strict';
var eslint = require('gulp-eslint');
var gulp = require('gulp');
var nodemon = require('gulp-nodemon');
var debug = require('debug')('gulp');
var todo = require('gulp-todo');
var mocha = require('gulp-mocha');
var _ = require('lodash');
var runSequence = require('run-sequence');
var conventionalChangelog = require('gulp-conventional-changelog');
var conventionalGithubReleaser = require('conventional-github-releaser');
var bump = require('gulp-bump');
var gutil = require('gulp-util');
var git = require('gulp-git');
var fs = require('fs');
var config = require('./config');
var argv = require('minimist');
gulp.task('lint', function() {
return gulp.src(['./*.js', './**/*.js', '!./node_modules/**', '!./node_modules/*.js', '!./template/*.js'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('default', function() {
var stream = nodemon({ script: 'app.js', env: { 'NODE_ENV': 'development', 'DEBUG': 'gulp' }, tasks: ['lint', 'test'] });
stream
.on('restart', function() {
debug('restarted!');
})
.on('crash', function() {
debug('Application has crashed!\n');
stream.emit('restart', 10); // restart the server in 10 seconds
});
});
gulp.task('test', function() {
// Override RATE LIMIT HERE FOR UNIT TEST
// process.env.RATE_LIMIT = 10;
process.env.SECURE_MODE = true;
process.env.NO_CACHE = 'no';
process.env.NODE_ENV = 'test';
gulp.src('./test', { read: false })
// `gulp-mocha` needs filepaths so you can't have any plugins before it
.pipe(mocha({
reporter: 'spec'
}));
});
// Remember to pass argument '--name TheServiceName' or '-n TheServiceName' to the service creation command
// If you want to use an API as a database model, pass the base url and the endpoint. '--baseurl http://google.com' or '--b http://google.com'
// '--endpoint users' or '--e users'
// Note that the name must be singular
gulp.task('service', function(done) {
var args = argv(process.argv.slice(2));
var name;
var baseurl;
var endpoint;
var isSQL;
baseurl = args.baseurl;
endpoint = args.endpoint;
if (!baseurl) {
baseurl = args.b;
}
if (!endpoint) {
endpoint = args.e;
}
isSQL = args.sql;
name = args.name;
if (!name) {
name = args.n;
}
if (!name) {
throw new Error('Please, pass the service name using the "-n" argument or "--name" argument');
}
var namePlural = _.lowerCase(name) + 's';
var nameCapitalise = _.capitalize(name);
var nameCapitalisePlural = _.capitalize(name) + 's';
var nameLowerCase = _.lowerCase(name);
// Create the Route
fs.readFile('./template/route.tmpl', function(err, data) {
if (err) {
throw err;
}
var tpl = _.template(data);
var result = tpl({ service: nameCapitalise, object: nameLowerCase });
fs.writeFile('./routes/' + namePlural + '.js', result, function(err) {
if (err) {
throw err;
}
console.log('Route created at ./routes/' + namePlural + '.js');
});
});
// Create the Route Unit Test
fs.readFile(isSQL ? './template/route_sql_test.tmpl' : './template/route_test.tmpl', function(err, data) {
if (err) {
throw err;
}
var tpl = _.template(data);
var result = tpl({ service: nameCapitalise, object: nameLowerCase });
fs.writeFile('./test/routes/' + namePlural + '.js', result, function(err) {
if (err) {
throw err;
}
console.log('Route unit test created at ./test/routes/' + namePlural + '.js');
});
});
// Create the Model
if (baseurl && endpoint) {
fs.readFile('./template/model_api.tmpl', function(err, data) {
if (err) {
throw err;
}
var tpl = _.template(data);
var result = tpl({ baseurl: baseurl, endpoint: endpoint });
fs.writeFile('./models/' + nameCapitalisePlural + '.js', result, function(err) {
if (err) {
throw err;
}
console.log('Model created at ./models/' + nameCapitalisePlural + '.js');
});
});
}
else {
fs.readFile(isSQL ? './template/model_sql.tmpl' : './template/model.tmpl', function(err, data) {
if (err) {
throw err;
}
var tpl = _.template(data);
var result = tpl({ service: nameCapitalise, object: nameLowerCase });
fs.writeFile('./models/' + nameCapitalisePlural + '.js', result, function(err) {
if (err) {
throw err;
}
console.log('Model created at ./models/' + nameCapitalisePlural + '.js');
});
});
}
// Create the Model Unit Test
fs.readFile(isSQL ? './template/model_sql_test.tmpl' : './template/model_test.tmpl', function(err, data) {
if (err) {
throw err;
}
var tpl = _.template(data);
var result = tpl({ service: nameCapitalise, object: nameLowerCase });
fs.writeFile('./test/models/' + namePlural + '.js', result, function(err) {
if (err) {
throw err;
}
console.log('Model unit test created at ./test/models/' + namePlural + '.js');
});
});
// Create the controller
fs.readFile(isSQL ? './template/controller_sql.tmpl' : './template/controller.tmpl', function(err, data) {
if (err) {
throw err;
}
var tpl = _.template(data);
var result = tpl({ service: nameCapitalise, object: nameLowerCase });
fs.writeFile('./controllers/' + nameCapitalisePlural + '.js', result, function(err) {
if (err) {
throw err;
}
console.log('Controller created at ./controllers/' + nameCapitalisePlural + '.js');
});
});
// Create the controller Unit test
fs.readFile(isSQL ? './template/controller_sql_test.tmpl' : './template/controller_test.tmpl', function(err, data) {
if (err) {
throw err;
}
var tpl = _.template(data);
var result = tpl({ service: nameCapitalise, object: nameLowerCase });
fs.writeFile('./test/controllers/' + namePlural + '.js', result, function(err) {
if (err) {
throw err;
}
console.log('Controller unit test created at ./test/controllers/' + namePlural + '.js');
});
});
return done();
});
// generate a todo.md from your javascript files
gulp.task('todo', function() {
gulp.src(['./*.js', './**/*.js', '!./node_modules/**', '!./node_modules/*.js'])
.pipe(todo())
.pipe(gulp.dest('./'));
// -> Will output a TODO.md with your todos
});
gulp.task('sanity', gulp.series('lint', 'test', 'todo'));
// Release
gulp.task('changelog', function() {
return gulp.src('./CHANGELOG.md', {
buffer: false
})
.pipe(conventionalChangelog({
preset: 'angular' // Or to any other commit message convention you use.
}))
.pipe(gulp.dest('./'));
});
gulp.task('github-release', function(done) {
conventionalGithubReleaser({
type: 'oauth',
token: config.gitOAuthToken // change this to your own GitHub token or use an environment variable
}, {
preset: 'angular' // Or to any other commit message convention you use.
}, done);
});
// Remember to pass argument '-r patch/minor/major' to the release command
gulp.task('bump-version', function() {
var args = argv(process.argv.slice(2));
// We hardcode the version change type to 'patch' but it may be a good idea to
// use minimist (https://www.npmjs.com/package/minimist) to determine with a
// command argument whether you are doing a 'major', 'minor' or a 'patch' change.
if (!args.r) {
throw new Error('The release type is not defined! Please pass the -r switch with a release type argument (patch/minor/major)');
}
else {
return gulp.src(['./package.json'])
.pipe(bump({ type: args.r }).on('error', gutil.log))
.pipe(gulp.dest('./'));
}
});
gulp.task('commit-changes', function() {
return gulp.src('.')
.pipe(git.add())
.pipe(git.commit('[Prerelease] Bumped version number'));
});
gulp.task('push-changes', function(cb) {
git.push('origin', 'dev', cb);
});
gulp.task('create-new-tag', function(cb) {
var getPackageJsonVersion = function() {
// 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;
};
var version = getPackageJsonVersion();
git.tag(version, 'Created Tag for version: ' + version, function(error) {
if (error) {
return cb(error);
}
git.push('origin', 'dev', { args: '--tags' }, cb);
});
});
gulp.task('release_done', function(cb) {
console.log('RELEASE FINISHED SUCCESSFULLY');
cb();
});
gulp.task('release', gulp.series(
'bump-version',
'changelog',
'commit-changes',
'push-changes',
'create-new-tag',
'github-release',
'release_done'));