1
0

release.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #!/usr/bin/env node
  2. /*
  3. * jQuery Core Release Management
  4. */
  5. // Debugging variables
  6. var debug = false,
  7. skipRemote = false;
  8. var fs = require("fs"),
  9. child = require("child_process"),
  10. path = require("path"),
  11. archiver = require("archiver");
  12. var releaseVersion,
  13. nextVersion,
  14. isBeta,
  15. pkg,
  16. branch,
  17. scpURL = "jqadmin@code.origin.jquery.com:/var/www/html/code.jquery.com/",
  18. cdnURL = "http://code.origin.jquery.com/",
  19. repoURL = "git@github.com:jquery/jquery.git",
  20. // Windows needs the .cmd version but will find the non-.cmd
  21. // On Windows, ensure the HOME environment variable is set
  22. gruntCmd = process.platform === "win32" ? "grunt.cmd" : "grunt",
  23. devFile = "dist/jquery.js",
  24. minFile = "dist/jquery.min.js",
  25. mapFile = "dist/jquery.min.map",
  26. releaseFiles = {
  27. "jquery-VER.js": devFile,
  28. "jquery-VER.min.js": minFile,
  29. "jquery-VER.min.map": mapFile //,
  30. // Disable these until 2.0 defeats 1.9 as the ONE TRUE JQUERY
  31. // "jquery.js": devFile,
  32. // "jquery.min.js": minFile,
  33. // "jquery.min.map": mapFile,
  34. // "jquery-latest.js": devFile,
  35. // "jquery-latest.min.js": minFile,
  36. // "jquery-latest.min.map": mapFile
  37. },
  38. jQueryFilesCDN = [],
  39. googleFilesCDN = [
  40. "jquery.js", "jquery.min.js", "jquery.min.map"
  41. ],
  42. msFilesCDN = [
  43. "jquery-VER.js", "jquery-VER.min.js", "jquery-VER.min.map"
  44. ];
  45. steps(
  46. initialize,
  47. checkGitStatus,
  48. tagReleaseVersion,
  49. gruntBuild,
  50. makeReleaseCopies,
  51. setNextVersion,
  52. copyTojQueryCDN,
  53. buildGoogleCDN,
  54. buildMicrosoftCDN,
  55. pushToGithub,
  56. exit
  57. );
  58. function initialize( next ) {
  59. if ( process.argv[2] === "-d" ) {
  60. process.argv.shift();
  61. debug = true;
  62. console.warn("=== DEBUG MODE ===" );
  63. }
  64. // First arg should be the version number being released
  65. var newver, oldver,
  66. rsemver = /^(\d+)\.(\d+)\.(\d+)(?:-([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?$/,
  67. version = ( process.argv[3] || "" ).toLowerCase().match( rsemver ) || {},
  68. major = version[1],
  69. minor = version[2],
  70. patch = version[3],
  71. xbeta = version[4];
  72. branch = process.argv[2];
  73. releaseVersion = process.argv[3];
  74. isBeta = !!xbeta;
  75. if ( !branch || !major || !minor || !patch ) {
  76. die( "Usage: " + process.argv[1] + " branch releaseVersion" );
  77. }
  78. if ( xbeta === "pre" ) {
  79. die( "Cannot release a 'pre' version!" );
  80. }
  81. if ( !(fs.existsSync || path.existsSync)( "package.json" ) ) {
  82. die( "No package.json in this directory" );
  83. }
  84. pkg = JSON.parse( fs.readFileSync( "package.json" ) );
  85. console.log( "Current version is " + pkg.version + "; generating release " + releaseVersion );
  86. version = pkg.version.match( rsemver );
  87. oldver = ( +version[1] ) * 10000 + ( +version[2] * 100 ) + ( +version[3] )
  88. newver = ( +major ) * 10000 + ( +minor * 100 ) + ( +patch );
  89. if ( newver < oldver ) {
  90. die( "Next version is older than current version!" );
  91. }
  92. nextVersion = major + "." + minor + "." + ( isBeta ? patch : +patch + 1 ) + "-pre";
  93. next();
  94. }
  95. function checkGitStatus( next ) {
  96. git( [ "status" ], function( error, stdout, stderr ) {
  97. var onBranch = ((stdout||"").match( /On branch (\S+)/ ) || [])[1];
  98. if ( onBranch !== branch ) {
  99. dieIfReal( "Branches don't match: Wanted " + branch + ", got " + onBranch );
  100. }
  101. if ( /Changes to be committed/i.test( stdout ) ) {
  102. dieIfReal( "Please commit changed files before attemping to push a release." );
  103. }
  104. if ( /Changes not staged for commit/i.test( stdout ) ) {
  105. dieIfReal( "Please stash files before attempting to push a release." );
  106. }
  107. next();
  108. });
  109. }
  110. function tagReleaseVersion( next ) {
  111. updatePackageVersion( releaseVersion );
  112. git( [ "commit", "-a", "-m", "Tagging the " + releaseVersion + " release." ], function(){
  113. git( [ "tag", releaseVersion ], next, debug);
  114. }, debug);
  115. }
  116. function gruntBuild( next ) {
  117. exec( gruntCmd, [], function( error, stdout ) {
  118. if ( error ) {
  119. die( error + stderr );
  120. }
  121. console.log( stdout );
  122. next();
  123. }, false );
  124. }
  125. function makeReleaseCopies( next ) {
  126. Object.keys( releaseFiles ).forEach(function( key ) {
  127. var text,
  128. builtFile = releaseFiles[ key ],
  129. unpathedFile = key.replace( /VER/g, releaseVersion ),
  130. releaseFile = "dist/" + unpathedFile;
  131. // Beta releases don't update the jquery-latest etc. copies
  132. if ( !isBeta || key.indexOf( "VER" ) >= 0 ) {
  133. if ( /\.map$/.test( releaseFile ) ) {
  134. // Map files need to reference the new uncompressed name;
  135. // assume that all files reside in the same directory.
  136. // "file":"jquery.min.js","sources":["jquery.js"]
  137. text = fs.readFileSync( builtFile, "utf8" )
  138. .replace( /"file":"([^"]+)","sources":\["([^"]+)"\]/,
  139. "\"file\":\"" + unpathedFile.replace( /\.min\.map/, ".min.js" ) +
  140. "\",\"sources\":[\"" + unpathedFile.replace( /\.min\.map/, ".js" ) + "\"]" );
  141. fs.writeFileSync( releaseFile, text );
  142. } else if ( /\.min\.js$/.test( releaseFile ) ) {
  143. // Minified files point back to the corresponding map;
  144. // again assume one big happy directory.
  145. // "//@ sourceMappingURL=jquery.min.map"
  146. text = fs.readFileSync( builtFile, "utf8" )
  147. .replace( /\/\/@ sourceMappingURL=\S+/,
  148. "//@ sourceMappingURL=" + unpathedFile.replace( /\.js$/, ".map" ) );
  149. fs.writeFileSync( releaseFile, text );
  150. } else if ( builtFile !== releaseFile ) {
  151. copy( builtFile, releaseFile );
  152. }
  153. jQueryFilesCDN.push( releaseFile );
  154. }
  155. });
  156. next();
  157. }
  158. function setNextVersion( next ) {
  159. updatePackageVersion( nextVersion );
  160. git( [ "commit", "-a", "-m", "Updating the source version to " + nextVersion + "✓™" ], next, debug );
  161. }
  162. function copyTojQueryCDN( next ) {
  163. var cmds = [];
  164. jQueryFilesCDN.forEach(function( name ) {
  165. cmds.push(function( nxt ){
  166. exec( "scp", [ name, scpURL ], nxt, debug || skipRemote );
  167. });
  168. cmds.push(function( nxt ){
  169. exec( "curl", [ cdnURL + name + "?reload" ], nxt, debug || skipRemote );
  170. });
  171. });
  172. cmds.push( next );
  173. steps.apply( this, cmds );
  174. }
  175. function buildGoogleCDN( next ) {
  176. makeArchive( "googlecdn", googleFilesCDN, next );
  177. }
  178. function buildMicrosoftCDN( next ) {
  179. makeArchive( "mscdn", msFilesCDN, next );
  180. }
  181. function pushToGithub( next ) {
  182. git( [ "push", "--tags", repoURL, branch ], next, debug || skipRemote );
  183. }
  184. //==============================
  185. function steps() {
  186. var cur = 0,
  187. steps = arguments;
  188. (function next(){
  189. process.nextTick(function(){
  190. steps[ cur++ ]( next );
  191. });
  192. })();
  193. }
  194. function updatePackageVersion( ver ) {
  195. console.log( "Updating package.json version to " + ver );
  196. pkg.version = ver;
  197. if ( !debug ) {
  198. fs.writeFileSync( "package.json", JSON.stringify( pkg, null, "\t" ) + "\n" );
  199. }
  200. }
  201. function makeArchive( cdn, files, fn ) {
  202. if ( isBeta ) {
  203. console.log( "Skipping archive creation for " + cdn + "; " + releaseVersion + " is beta" );
  204. process.nextTick( fn );
  205. return;
  206. }
  207. console.log( "Creating production archive for " + cdn );
  208. var archive = archiver( "zip" ),
  209. md5file = "dist/" + cdn + "-md5.txt",
  210. output = fs.createWriteStream( "dist/" + cdn + "-jquery-" + releaseVersion + ".zip" );
  211. archive.on( "error", function( err ) {
  212. throw err;
  213. });
  214. output.on( "close", fn );
  215. archive.pipe( output );
  216. files = files.map(function( item ) {
  217. return "dist/" + item.replace( /VER/g, releaseVersion );
  218. });
  219. exec( "md5sum", files, function( err, stdout, stderr ) {
  220. fs.writeFileSync( md5file, stdout );
  221. files.push( md5file );
  222. files.forEach(function( file ) {
  223. archive.append( fs.createReadStream( file ), { name: file } );
  224. });
  225. archive.finalize();
  226. }, false );
  227. }
  228. function copy( oldFile, newFile, skip ) {
  229. console.log( "Copying " + oldFile + " to " + newFile );
  230. if ( !skip ) {
  231. fs.writeFileSync( newFile, fs.readFileSync( oldFile, "utf8" ) );
  232. }
  233. }
  234. function git( args, fn, skip ) {
  235. exec( "git", args, fn, skip );
  236. }
  237. function exec( cmd, args, fn, skip ) {
  238. if ( skip ) {
  239. console.log( "# " + cmd + " " + args.join(" ") );
  240. fn( "", "", "" );
  241. } else {
  242. console.log( cmd + " " + args.join(" ") );
  243. child.execFile( cmd, args, { env: process.env },
  244. function( err, stdout, stderr ) {
  245. if ( err ) {
  246. die( stderr || stdout || err );
  247. }
  248. fn.apply( this, arguments );
  249. }
  250. );
  251. }
  252. }
  253. function die( msg ) {
  254. console.error( "ERROR: " + msg );
  255. process.exit( 1 );
  256. }
  257. function dieIfReal( msg ) {
  258. if ( debug ) {
  259. console.log ( "DIE: " + msg );
  260. } else {
  261. die( msg );
  262. }
  263. }
  264. function exit() {
  265. process.exit( 0 );
  266. }