forked from jquery/jquery-migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.js
282 lines (240 loc) · 7.03 KB
/
release.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
#!/usr/bin/env node
/*
* jQuery Migrate Plugin Release Management
*/
// Debugging variables
var debug = false,
skipRemote = false;
var fs = require("fs"),
child = require("child_process"),
path = require("path");
var releaseVersion,
nextVersion,
finalFiles,
isBeta,
pkg,
scpURL = "[email protected]:/var/www/html/code.jquery.com/",
cdnURL = "http://code.origin.jquery.com/",
repoURL = "[email protected]:jquery/jquery-migrate.git",
branch = "master",
// Windows needs the .cmd version but will find the non-.cmd
// On Windows, ensure the HOME environment variable is set
gruntCmd = process.platform === "win32" ? "grunt.cmd" : "grunt",
readmeFile = "README.md",
packageFile = "package.json",
pluginFile = "migrate.jquery.json",
devFile = "dist/jquery-migrate.js",
minFile = "dist/jquery-migrate.min.js",
releaseFiles = {
"jquery-migrate-VER.js": devFile,
"jquery-migrate-VER.min.js": minFile
};
steps(
initialize,
checkGitStatus,
updateReadme,
tagReleaseVersion,
gruntBuild,
makeReleaseCopies,
setNextVersion,
uploadToCDN,
pushToRemote,
exit
);
function initialize( next ) {
if ( process.argv[2] === "-d" ) {
process.argv.shift();
debug = true;
console.warn("=== DEBUG MODE ===" );
}
// First arg should be the version number being released; this is a proper subset
// of a full semver, see https://github.com/mojombo/semver/issues/32
// Examples: 1.0.1, 1.0.1-pre, 1.0.1-rc1, 1.0.1-rc1.1
var newver, oldver,
rsemver = /^(\d+)\.(\d+)\.(\d+)(?:-([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?$/,
version = rsemver.exec( process.argv[2] || "" ) || [],
major = version[1],
minor = version[2],
patch = version[3],
xbeta = version[4];
releaseVersion = process.argv[2];
isBeta = !!xbeta;
if ( !releaseVersion ) {
die( "Usage: release [ -d ] releaseVersion" );
}
if ( !version.length ) {
die( "'" + releaseVersion + "' is not a valid semver!" );
}
if ( xbeta === "pre" ) {
die( "Cannot release a 'pre' version!" );
}
if ( !(fs.existsSync || path.existsSync)( packageFile ) ) {
die( "No " + packageFile + " in this directory" );
}
pkg = JSON.parse( fs.readFileSync( packageFile ) );
log( "Current version is " + pkg.version + "; generating release " + releaseVersion );
version = rsemver.exec( pkg.version );
oldver = ( +version[1] ) * 10000 + ( +version[2] * 100 ) + ( +version[3] )
newver = ( +major ) * 10000 + ( +minor * 100 ) + ( +patch );
if ( newver < oldver ) {
die( "Next version is older than current version!" );
}
nextVersion = major + "." + minor + "." + ( isBeta ? patch : +patch + 1 ) + "-pre";
next();
}
//TODO: Check that remote doesn't have newer commits:
// git fetch repoURL
// git remote show repoURL
// (look for " BRANCH pushes to BRANCH (up to date)")
function checkGitStatus( next ) {
child.execFile( "git", [ "status" ], function( error, stdout, stderr ) {
var onBranch = ((stdout||"").match( /On branch (\S+)/ ) || [])[1];
if ( onBranch !== branch ) {
die( "Branches don't match: Wanted " + branch + ", got " + onBranch );
}
if ( /Changes to be committed/i.test( stdout ) ) {
die("Please commit changed files before attemping to push a release.");
}
if ( /Changes not staged for commit/i.test( stdout ) ) {
die("Please stash files before attempting to push a release.");
}
next();
});
}
function tagReleaseVersion( next ) {
updatePackageVersion( releaseVersion );
updatePluginVersion( releaseVersion );
git( [ "commit", "-a", "-m", "Tagging the " + releaseVersion + " release." ], function(){
git( [ "tag", releaseVersion ], next);
});
}
function updateReadme( next ) {
var readme = fs.readFileSync( readmeFile, "utf8" );
// Change version references from the old version to the new one;
// Only release versions should be updated which simplifies the regex
if ( isBeta ) {
log( "Skipping " + readmeFile + " update (beta release)" );
} else {
log( "Updating " + readmeFile );
readme = readme
.replace( /jquery-migrate-\d+\.\d+\.\d+/g, "jquery-migrate-" + releaseVersion );
if ( !debug ) {
fs.writeFileSync( readmeFile, readme );
}
}
next();
}
function gruntBuild( next ) {
exec( gruntCmd, [], function( error, stdout ) {
if ( error ) {
die( error + stderr );
}
log( stdout || "(no output)" );
next();
});
}
function makeReleaseCopies( next ) {
finalFiles = {};
Object.keys( releaseFiles ).forEach(function( key ) {
var builtFile = releaseFiles[ key ],
releaseFile = key.replace( /VER/g, releaseVersion );
copy( builtFile, releaseFile );
finalFiles[ releaseFile ] = builtFile;
});
next();
}
function setNextVersion( next ) {
updatePackageVersion( nextVersion, "master" );
git( [ "commit", "-a", "-m", "Updating the source version to " + nextVersion ], next );
}
function uploadToCDN( next ) {
var cmds = [];
Object.keys( finalFiles ).forEach(function( name ) {
cmds.push(
function( nxt ){
exec( "scp", [ name, scpURL ], nxt, skipRemote );
},
function( nxt ){
exec( "curl", [ cdnURL + name + "?reload" ], nxt, skipRemote );
}
);
});
cmds.push( next );
steps.apply( this, cmds );
}
function pushToRemote( next ) {
git( [ "push", "--tags", repoURL, branch ], next, skipRemote );
}
//==============================
function steps() {
var cur = 0,
steps = arguments;
(function next(){
process.nextTick(function(){
steps[ cur++ ]( next );
});
})();
}
function updatePackageVersion( ver, blobVer ) {
log( "Updating " + packageFile + " version to " + ver );
blobVer = blobVer || ver;
pkg.version = ver;
pkg.author.url = setBlobVersion( pkg.author.url, blobVer );
pkg.licenses[0].url = setBlobVersion( pkg.licenses[0].url, blobVer );
writeJsonSync( packageFile, pkg );
}
function updatePluginVersion( ver ) {
var plug;
log( "Updating " + pluginFile + " version to " + ver );
plug = JSON.parse( fs.readFileSync( pluginFile ) );
plug.version = ver;
plug.author.url = setBlobVersion( plug.author.url, ver );
plug.licenses[0].url = setBlobVersion( plug.licenses[0].url, ver );
plug.download = setBlobVersion( plug.download, ver );
writeJsonSync( pluginFile, plug );
}
function setBlobVersion( s, v ) {
return s.replace( /\/blob\/(?:(\d+\.\d+[^\/]+)|master)/, "/blob/" + v );
}
function writeJsonSync( fname, json ) {
if ( debug ) {
console.log( JSON.stringify( json, null, " " ) );
} else {
fs.writeFileSync( fname, JSON.stringify( json, null, "\t" ) + "\n" );
}
}
function copy( oldFile, newFile ) {
log( "Copying " + oldFile + " to " + newFile );
if ( !debug ) {
fs.writeFileSync( newFile, fs.readFileSync( oldFile, "utf8" ) );
}
}
function git( args, fn, skip ) {
exec( "git", args, fn, skip );
}
function exec( cmd, args, fn, skip ) {
if ( debug || skip ) {
log( "# " + cmd + " " + args.join(" ") );
fn();
} else {
log( cmd + " " + args.join(" ") );
child.execFile( cmd, args, { env: process.env },
function( err, stdout, stderr ) {
if ( err ) {
die( stderr || stdout || err );
}
fn();
}
);
}
}
function log( msg ) {
console.log( msg );
}
function die( msg ) {
console.error( "ERROR: " + msg );
process.exit( 1 );
}
function exit() {
process.exit( 0 );
}