This repository was archived by the owner on Mar 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgulpfile.js
386 lines (348 loc) · 9.65 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* This file contains gulp (https://gulpjs.com/) tasks to run at build time.
* See also npm tasks in package.json.
*/
const fs = require("fs");
const argv = require("yargs").argv;
const path = require("path");
const runSequence = require("run-sequence");
const gulp = require("gulp");
const textSimple = require("gulp-text-simple");
const rename = require("gulp-rename");
const git = require("gulp-git");
const run = require("gulp-run");
const jsonEditor = require("gulp-json-editor");
const semver = require("semver");
const mjml2html = require("mjml");
const prettier = require('gulp-prettier');
const TYPESCRIPT_SOURCE_DIR = "lib";
const TEMPLATES_SOURCE_DIR = "templates/mjml";
const TEMPLATES_SOURCE = `${TEMPLATES_SOURCE_DIR}/*.mjml`;
const TEMPLATES_OUTPUT_DIR = `${TYPESCRIPT_SOURCE_DIR}/templates/html`;
const GIT_RELEASE_BRANCH = "master";
const GIT_ORIGIN = "origin";
// resolve the root directory of this project
const rootDir = path.resolve(argv.rootDir || "./") + "/";
// path to the root `package.json`
const packageJsonPath = path.join(rootDir, "package.json");
// the content of the root `package.json`
const currentPackageJson = JSON.parse(fs.readFileSync(packageJsonPath));
// the following values are calculated for every task but are only used during
// the release process, not super efficient but it's the easiest way to share
// them across tasks
const currentVersion = semver.parse(currentPackageJson.version);
const releaseVersionValue = `${currentVersion.major}.${currentVersion.minor}.${
currentVersion.patch
}`;
if (
process.env.RELEASE &&
!["major", "minor", "patch"].includes(process.env.RELEASE)
) {
throw new Error("RELEASE must be one between 'major', 'minor' or 'patch'");
}
const nextVersionValue = `${semver.inc(
releaseVersionValue,
// RELEASE can be "major", "minor" (default) or "patch"
process.env.RELEASE || "minor"
)}-SNAPSHOT`;
const funcpackBranchPrefix = "funcpack-release";
const releaseVersionFuncpackBranchName = `${funcpackBranchPrefix}-v${releaseVersionValue}`;
/**
* Transform a mjml template to a Typescript function outputting HTML.
*
* @param content the content of mjml template as string
* @param options options object (see gulp-text-simple plugin https://www.npmjs.com/package/gulp-text-simple)
*/
const toMjml = (content, options) => {
const name = path.basename(options.sourcePath);
return [
`// DO NOT EDIT THIS FILE`,
`// this file was auto generated from '${name}' by gulp generate:templates`,
`// tslint:disable-next-line:parameters-max-number`,
`export default function(`,
` title: string,`,
` headlineText: string,`,
` senderOrganizationName: string,`,
` senderServiceName: string,`,
` organizationFiscalCode: string,`,
` titleText: string,`,
` contentHtml: string,`,
` footerHtml: string`,
`): string {`,
` return \``,
`${mjml2html(content, { minify: true }).html}\`;`,
`}`,
""
].join("\n");
};
/**
* Generate Typescript template files from mjml (https://mjml.io/).
*/
gulp.task("generate:templates", () => {
return gulp
.src(TEMPLATES_SOURCE)
.pipe(textSimple(toMjml)())
.pipe(prettier())
.pipe(rename(filepath => (filepath.extname = ".ts")))
.pipe(gulp.dest(TEMPLATES_OUTPUT_DIR));
});
/**
* Run the build task
*/
gulp.task("yarn:build", () => {
return gulp.src(TYPESCRIPT_SOURCE_DIR).pipe(run(`yarn build`));
});
/**
* Run the lint task
*/
gulp.task("yarn:lint", () => {
return gulp.src(TYPESCRIPT_SOURCE_DIR).pipe(run(`yarn run lint`));
});
/**
* Run unit tests
*/
gulp.task("unit:test", () => {
return (
gulp
.src(TYPESCRIPT_SOURCE_DIR)
// for some unknow reason jest tests won't exit on win32 machine
// when run through gulp (running the tests directly through the jest CLI just works)
// that's the reason of the `forceExit` flag
.pipe(run("jest --coverage --runInBand"))
);
});
/**
* Run the test task
*/
gulp.task("test", cb => runSequence("yarn:lint", "unit:test", cb));
/**
* Package Azure Functions code and dependencines in a single file
*/
gulp.task("yarn:funcpack", () => {
return gulp
.src(TYPESCRIPT_SOURCE_DIR)
.pipe(run("yarn run funcpack pack -e webpack.config.js ./"));
});
/**
* Checks out the release branch
*/
gulp.task("git:checkout:release", cb => {
return git.checkout(GIT_RELEASE_BRANCH, {}, cb);
});
/**
* Fails if repository has untracked files
*/
gulp.task("git:check:untracked", cb => {
return git.exec(
{
args: "ls-files --other --exclude-standard"
},
(err, stdout) => {
if (err) {
throw err;
}
if (stdout.trim().length > 0) {
return cb("Repository must not have untracked files");
}
cb();
}
);
});
/**
* Fails if repository has modified files
*/
gulp.task("git:check:modified", cb => {
return git.exec(
{
args: "ls-files --modified --exclude-standard"
},
(err, stdout) => {
if (err) {
throw err;
}
if (stdout.trim().length > 0) {
return cb("Repository must not have modified files");
}
cb();
}
);
});
/**
* Fails if repository is not clean
*/
gulp.task("git:check:clean", ["git:check:untracked", "git:check:modified"]);
/**
* Fails if current branch is not GIT_RELEASE_BRANCH
*/
gulp.task("git:check:branch", cb => {
return git.exec(
{
args: "symbolic-ref HEAD"
},
(err, stdout) => {
if (err) {
throw err;
}
if (stdout.trim() !== `refs/heads/${GIT_RELEASE_BRANCH}`) {
return cb(`You must be on the ${GIT_RELEASE_BRANCH} branch`);
}
cb();
}
);
});
/**
* Push master to origin
*/
gulp.task("git:push:origin", cb => {
return git.push(GIT_ORIGIN, GIT_RELEASE_BRANCH, {}, cb);
});
/**
* Push the tags to origin
*/
gulp.task("git:push:tags", cb => {
return git.push(
GIT_ORIGIN,
GIT_RELEASE_BRANCH,
// push only annotated tags
{ args: "--follow-tags" },
cb
);
});
/**
* Bump the version to the release version
*/
gulp.task("release:bump:release", () => {
return gulp
.src(packageJsonPath)
.pipe(
jsonEditor({
version: releaseVersionValue
})
)
.pipe(gulp.dest(rootDir));
});
/**
* Commit package.json with updated release version
*/
gulp.task("release:git:commit:release", () => {
return gulp
.src(packageJsonPath)
.pipe(git.add())
.pipe(git.commit(`Bumped release ${releaseVersionValue}`));
});
/**
* Tag last commit with the release version
*/
gulp.task("release:git:tag:release", cb => {
const tag = `v${releaseVersionValue}`;
return git.tag(tag, `Created tag for version ${tag}`, cb);
});
/**
* Creates a new branch for storing funcpack assets
*/
gulp.task("release:git:checkout:funcpack", cb => {
return git.checkout(releaseVersionFuncpackBranchName, { args: "-b" }, cb);
});
/**
* Adds funcpack assets
*/
gulp.task("release:git:commit:funcpack", cb => {
return gulp
.src(["*/function.json", ".funcpack/index.js"])
.pipe(git.add({ args: "-f" })) // force because .gitignore contains *.js
.pipe(
git.commit(`Adds funcpack assets for release ${releaseVersionValue}`)
);
});
/**
* Pushes funcpack branch to origin
*/
gulp.task("release:git:push:funcpack", cb => {
return git.push(
GIT_ORIGIN,
releaseVersionFuncpackBranchName,
{ args: "-u" },
cb
);
});
/**
* Updates the remote "latest" funcpack branch to the current funcpack branch
*/
gulp.task("release:git:push:funcpack:latest", cb => {
return git.push(
GIT_ORIGIN,
`${releaseVersionFuncpackBranchName}:${funcpackBranchPrefix}-latest`,
{ args: "-f" },
cb
);
});
/**
* Bump the version to the snapshot version
*/
gulp.task("release:bump:next", () => {
return gulp
.src(packageJsonPath)
.pipe(
jsonEditor({
version: nextVersionValue
})
)
.pipe(gulp.dest(rootDir));
});
/**
* Commit package.json with updated snapshot version
*/
gulp.task("release:git:commit:next", () => {
return gulp
.src(packageJsonPath)
.pipe(git.add())
.pipe(git.commit(`Bumped release ${nextVersionValue}`));
});
gulp.task("release", function (cb) {
runSequence(
// check that the release is running on the GIT_RELEASE_BRANCH branch
"git:check:branch",
// check that the working directory is a git repository and
// the repository has no outstanding changes.
"git:check:clean",
// generate models, templates and compile typescript
"yarn:build",
// run tests
"test",
// bumps the version to the next release version:
// current version without the qualifier (eg. 1.2-SNAPSHOT -> 1.2)
"release:bump:release",
// commit the changes in package.json
"release:git:commit:release",
// tag the previous commit with v$version (eg. v1.2, v1.2.3).
"release:git:tag:release",
// checkout a new branch funcpack-release-vx.x.x
"release:git:checkout:funcpack",
// run funcpack
"yarn:funcpack",
// commits and pushes funcpack branch
"release:git:commit:funcpack",
"release:git:push:funcpack",
"release:git:push:funcpack:latest",
// check out the release branch (master)
"git:checkout:release",
// bumps the version to the next snapshot version:
// increase the minor version segment of the current version and set the
// qualifier to '-SNAPSHOT' (eg. 1.2.1-SNAPSHOT -> 1.3.0-SNAPSHOT)
"release:bump:next",
// commit the changes in package.json
"release:git:commit:next",
// push changes to origin
"git:push:origin",
"git:push:tags",
err => {
if (err) {
console.log(err.message);
} else {
console.log("RELEASE FINISHED SUCCESSFULLY");
}
cb(err);
}
);
});
gulp.task("default", ["generate:templates"]);