-
Notifications
You must be signed in to change notification settings - Fork 877
/
sync-gutenberg-version.js
79 lines (65 loc) · 2.57 KB
/
sync-gutenberg-version.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
const fetch = require( "node-fetch" );
module.exports = function( grunt ) {
/**
* Helper function to update the content of the file.
*
* @param {string} file Target file.
* @param {RegExp} pattern Pattern to search for.
* @param {string} version Version to insert.
*
* @returns {void}
*/
function setVersion( file, pattern, version ) {
const contents = grunt.file.read( file ).replace(
pattern,
version,
);
grunt.file.write( file, contents );
}
/**
* Reads the current Gutenberg version from the file.
*
* @param {string} file Target file.
* @param {RegExp} pattern Pattern to search for.
* @param {int} index Index to return.
*
* @returns {string} The version from the file.
*/
function getVersion( file, pattern, index ) {
return pattern.exec( grunt.file.read( file ) )[ index ];
}
grunt.registerTask(
"sync-gutenberg-version",
"Makes sure the Gutenberg-supported version is set to the latest release.",
async function() {
const done = this.async();
const targetFile = "admin/class-gutenberg-compatibility.php";
const gruntFlags = grunt.option.flags();
const noGitPush = gruntFlags.indexOf( "--no-git-push" ) !== -1;
const gutenbergInfo = await fetch( "https://api.wordpress.org/plugins/info/1.0/gutenberg.json" );
const gutenbergInfoJSON = await gutenbergInfo.json();
const currentVersion = getVersion( targetFile, /(\s?)(const CURRENT_RELEASE = ')(\d+(\.\d+){0,3})(';\n)/, 3 );
// Only change the file if the version does not match.
if ( currentVersion !== gutenbergInfoJSON.version ) {
setVersion( targetFile, /(\s?)(const CURRENT_RELEASE = ')(\d+(\.\d+){0,3})(';\n)/, "$1$2" + gutenbergInfoJSON.version + "$5" );
setVersion( targetFile, /(\s?)(const MINIMUM_SUPPORTED = ')(\d+(\.\d+){0,3})(';\n)/, "$1$2" + gutenbergInfoJSON.version + "$5" );
// Add the file to commit the version bump.
grunt.config( "gitadd.gutenbergFiles", { files: { src: [ targetFile ] } } );
grunt.task.run( "gitadd:gutenbergFiles" );
// Commit the version bump.
grunt.config( "gitcommit.gutenbergFiles", {
options: { message: "Bump Gutenberg supported version." },
files: { src: [ targetFile ] },
} );
grunt.task.run( "gitcommit:gutenbergFiles" );
// Push the commit to the branch.
if ( ! noGitPush ) {
grunt.verbose.writeln( "push changes to github" );
grunt.config( "gitpush.gutenbergVersion.options", { remote: "origin", upstream: true, branch: grunt.config.data.branchForRC } );
grunt.task.run( "gitpush:gutenbergVersion" );
}
}
return done();
},
);
};