-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy pathrelease.js
355 lines (306 loc) · 9.29 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
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
#!/usr/bin/env node
/*
* JQuery Migrate Plugin Release Management
*/
// Debugging variables
var dryrun = false,
skipRemote = false;
import fs from "node:fs";
import child from "node:child_process";
import path from "node:path";
import chalk from "chalk";
import enquirer from "enquirer";
import { build } from "./tasks/build.js";
var releaseVersion,
nextVersion,
isBeta,
pkg,
prompt = enquirer.prompt,
repoURL = "[email protected]:jquery/jquery-migrate.git",
branch = "main",
// Windows needs the .cmd version but will find the non-.cmd
// On Windows, also ensure the HOME environment variable is set
npmCmd = process.platform === "win32" ? "npm.cmd" : "npm",
readmeFile = "README.md",
packageFile = "package.json",
versionFile = path.join( "src", "version.js" ),
releaseDir = "CDN",
distDir = "dist";
steps(
initialize,
checkGitStatus,
buildRelease,
updateVersions,
tagReleaseVersion,
buildRelease,
makeReleaseCopies,
publishToNPM,
setNextVersion,
pushToRemote,
remindAboutCDN,
remindAboutSites,
exit
);
function initialize( next ) {
// -d dryrun mode, no commands are executed at all
if ( process.argv[ 2 ] === "-d" ) {
process.argv.shift();
dryrun = true;
console.warn( "=== DRY RUN MODE ===" );
}
// -r skip remote mode, no remote commands are executed
// (git push, npm publish, cdn copy)
// Reset with `git reset --hard HEAD~2 && git tag -d (version) && npm run build`
if ( process.argv[ 2 ] === "-r" ) {
process.argv.shift();
skipRemote = true;
console.warn( "=== SKIPREMOTE 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 ) {
log( "Usage: release [ -d -r ] releaseVersion" );
log( " -d Dry-run; no commands are executed at all" );
log( " -r Skip-remote; nothing is pushed externally" );
die( "Invalid args" );
}
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, "utf8" ) );
status( "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 ) {
if ( error ) {
throw error;
}
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 ) {
git( [ "commit", "-a", "--no-verify", "-m", "Tagging the " + releaseVersion + " release." ],
function() {
git( [ "tag", releaseVersion ], next );
}
);
}
function updateVersions( next ) {
updateSourceVersion( releaseVersion );
updateReadmeVersion( releaseVersion );
updatePackageVersion( releaseVersion );
next();
}
async function buildRelease( next ) {
await build( { version: releaseVersion } );
next();
}
function makeReleaseCopies( next ) {
if ( !fs.existsSync( releaseDir ) ) {
fs.mkdirSync( releaseDir );
}
var passThrough = function( t ) {
return t;
};
var releaseFiles = {
"jquery-migrate-VER.js": passThrough,
"jquery-migrate-VER.min.js": fixMinRef,
"jquery-migrate-VER.min.map": fixMapRef
};
Object.keys( releaseFiles ).forEach( function( key ) {
var distFile = key.replace( /-VER/g, "" ),
distPath = path.join( distDir, distFile ),
releaseFile = key.replace( /VER/g, releaseVersion ),
releasePath = path.join( releaseDir, releaseFile );
// Remove Windows CRLF if it's there, on *nix this is a no-op
log( "Processing " + distPath + " => " + releasePath );
var distText = fs.readFileSync( distPath, "utf8" ).replace( /\r\n/g, "\n" );
var releaseText = releaseFiles[ key ]( distText, releaseFile );
if ( !dryrun ) {
fs.writeFileSync( releasePath, releaseText );
}
} );
next();
}
async function publishToNPM( next ) {
const { input: otp } = await prompt( {
type: "input",
name: "input",
message: "Enter one-time password if you have 2FA enabled and press Enter.\n" +
"Otherwise, just press Enter."
} );
// Don't update "latest" if this is a beta
if ( isBeta ) {
exec( npmCmd, [
"publish",
"--tag",
"beta",
...( otp ? [ "--otp", otp ] : [] )
], next, skipRemote );
} else {
exec( npmCmd, [
"publish",
...( otp ? [ "--otp", otp ] : [] )
], next, skipRemote );
}
}
function setNextVersion( next ) {
updateSourceVersion( nextVersion );
updatePackageVersion( nextVersion, "main" );
git( [ "commit", "-a", "--no-verify", "-m", "Updating the source version to " + nextVersion ],
next );
}
function pushToRemote( next ) {
git( [ "push", "--tags", repoURL, branch ], next, skipRemote );
}
function remindAboutCDN( next ) {
console.log( chalk.red( "TODO: Update CDN with jquery-migrate." +
releaseVersion + " files (min and regular)" ) );
console.log( chalk.red( " clone codeorigin.jquery.com, git add files, commit, push" ) );
next();
}
function remindAboutSites( next ) {
console.log( chalk.red( "TODO: Update jquery.com download page to " + releaseVersion ) );
next();
}
//==============================
function steps() {
var cur = 0,
steps = arguments;
( function next() {
process.nextTick( function() {
steps[ cur++ ]( next );
} );
} )();
}
function updatePackageVersion( ver, blobVer ) {
status( "Updating " + packageFile + " version to " + ver );
blobVer = blobVer || ver;
pkg.version = ver;
pkg.author.url = setBlobVersion( pkg.author.url, blobVer );
writeJsonSync( packageFile, pkg );
}
function updateSourceVersion( ver ) {
var stmt = "\njQuery.migrateVersion = \"" + ver + "\";\n";
status( "Updating " + stmt.replace( /\n/g, "" ) );
if ( !dryrun ) {
fs.writeFileSync( versionFile, stmt );
}
}
function updateReadmeVersion() {
var readme = fs.readFileSync( readmeFile, "utf8" );
// Change version references from the old version to the new one.
// The regex can update beta versions in case it was changed manually.
if ( isBeta ) {
status( "Skipping " + readmeFile + " update (beta release)" );
} else {
status( "Updating " + readmeFile );
readme = readme.replace(
/jquery-migrate-\d+\.\d+\.\d+(?:-\w+)?/g,
"jquery-migrate-" + releaseVersion
);
if ( !dryrun ) {
fs.writeFileSync( readmeFile, readme );
}
}
}
function setBlobVersion( s, v ) {
return s.replace( /\/blob\/(?:(\d+\.\d+[^\/]+)|main)/, "/blob/" + v );
}
function writeJsonSync( fname, json ) {
if ( dryrun ) {
console.log( JSON.stringify( json, null, " " ) );
} else {
fs.writeFileSync( fname, JSON.stringify( json, null, "\t" ) + "\n" );
}
}
function fixMinRef( oldText ) {
var mapRef = new RegExp( "^//# sourceMappingURL=jquery-migrate.min.map\\n?", "m" );
// Remove the ref for now rather than try to fix it
var newText = oldText.replace( mapRef, "" );
if ( oldText === newText ) {
throw Error( "fixMinRef: Unable to find the sourceMappingURL" );
}
return newText;
}
function fixMapRef( oldText, newFile ) {
var mapJSON = JSON.parse( oldText );
var sources = mapJSON.sources;
if ( sources.join() !== "../src/migratemute.js,jquery-migrate.js" ) {
throw Error( "fixMapRef: Unexpected sources entry: " + sources );
}
// This file isn't published, not sure the best way to deal with that
sources[ 0 ] = "migratemute.js";
sources[ 1 ] = newFile.replace( /\.map$/, ".js" );
return JSON.stringify( mapJSON );
}
function git( args, fn, skip ) {
exec( "git", args, fn, skip );
}
function exec( cmd, args, fn, skip ) {
if ( dryrun || skip ) {
log( chalk.black.bgBlue( "# " + cmd + " " + args.join( " " ) ) );
fn();
} else {
log( chalk.green( cmd + " " + args.join( " " ) ) );
child.execFile( cmd, args, { env: process.env },
function( err, stdout, stderr ) {
if ( err ) {
die( stderr || stdout || err );
}
fn();
}
);
}
}
function status( msg ) {
console.log( chalk.black.bgGreen( msg ) );
}
function log( msg ) {
console.log( msg );
}
function die( msg ) {
console.error( chalk.red( "ERROR: " + msg ) );
process.exit( 1 );
}
function exit() {
process.exit( 0 );
}