-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathGruntfile.js
105 lines (90 loc) · 3.65 KB
/
Gruntfile.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
// @ts-ignore
module.exports = (grunt) => {
grunt.initConfig({
vars: {
srcPath: './',
buildPath: '<%= vars.srcPath %>dist/'
},
copy: {
main: {
files: [
{
expand: true,
src: [
'<%= vars.srcPath %>areas/**',
'<%= vars.srcPath %>countries/**',
'<%= vars.srcPath %>states/**',
],
dest: '<%= vars.buildPath %>'
}
]
}
},
shell: {
"bump-version": {
command: function() {
grunt.task.run('validate-version');
var targetVersion = grunt.option('target');
return `npm pkg set version=${targetVersion} && echo 'Version bumped to ${targetVersion} successfully.'`;
}
},
"history": {
// TODO Filter out some commits like Release: * ones
command: 'git log $(git describe --tags --abbrev=0)..HEAD --format="* %s (%h) by %aN;" --no-merges',
options: {
stdout: false,
// @ts-ignore
callback: function (err, stdout, stderr, cb) {
grunt.option('history', stdout);
cb();
}
}
},
}
});
grunt.registerTask('validate-version', function() {
var targetVersion = grunt.option('target');
if (!/^\d+\.\d+\.\d+$/.test(targetVersion)) {
grunt.log.error('Target version has to be in x.y.z format.');
return false;
}
});
grunt.registerTask('write-changelog', function() {
grunt.task.requires('validate-version');
grunt.task.requires('shell:history');
var history = grunt.option('history');
var changelogPath = grunt.option('changelog') || 'CHANGELOG.md';
var prefixVersion = grunt.option('version-prefix') || '## v';
var target = grunt.option('target');
var escapedTarget = target.replaceAll('.', '\\.');
var regex = new RegExp(`^(?<title>#*?\\s*?v?\\s*?${escapedTarget}\\s*)(?<currentChangelog>(?:(?:(?!#).)*$)*)`, 'gms');
var changelog = grunt.file.read(changelogPath);
if (changelog.match(regex)) {
// @ts-ignore
changelog = changelog.replace(regex, function(match, p1, p2, offset, string, groups) {
return [
groups.title,
history
].join('');
})
}
else {
changelog = changelog.trim() + `\n\n${prefixVersion}${target}\n\n` + history;
}
grunt.file.write(changelogPath, changelog);
});
grunt.registerTask('update-changelog', ['validate-version', 'shell:history', 'write-changelog']);
grunt.registerTask('log-date-time', function () {
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date + ' ' + time;
// @ts-ignore
grunt.log.ok(dateTime['green']);
});
grunt.registerTask('build', ['copy', 'log-date-time']);
grunt.registerTask('release', ['validate-version', 'shell:bump-version', 'build', 'update-changelog']);
grunt.registerTask('default', ['build']);
grunt.loadNpmTasks('grunt-shell');
grunt.loadNpmTasks('grunt-contrib-copy');
}