Skip to content

Commit db38216

Browse files
committed
refactor: refactor release tags
1 parent 44bd955 commit db38216

5 files changed

Lines changed: 84 additions & 70 deletions

File tree

lib/commands/package/update-metadata.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ export default class extends Command {
1818
"default": false,
1919
"schema": { "type": "boolean" },
2020
},
21-
"update-npm": {
22-
"short": "n",
23-
"description": "update npm package tags",
21+
"update-tags": {
22+
"short": "t",
23+
"description": "update release tags",
2424
"default": false,
2525
"schema": { "type": "boolean" },
2626
},
@@ -43,7 +43,7 @@ export default class extends Command {
4343
const res = await pkg.updateMetadata( {
4444
"updateDependabot": process.cli.options[ "update-dependabot" ],
4545
"updateRepository": process.cli.options[ "update-repository" ],
46-
"updateNpm": process.cli.options[ "update-npm" ],
46+
"updateTags": process.cli.options[ "update-tags" ],
4747
"commit": process.cli.options.commit,
4848
} );
4949

lib/commands/workspace/update-metadata.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ export default class extends Command {
2121
"default": false,
2222
"schema": { "type": "boolean" },
2323
},
24-
"update-npm": {
25-
"short": "n",
26-
"description": "update npm package tags",
24+
"update-tags": {
25+
"short": "t",
26+
"description": "update release tags",
2727
"default": false,
2828
"schema": { "type": "boolean" },
2929
},
@@ -75,7 +75,7 @@ export default class extends Command {
7575
res = await pkg.updateMetadata( {
7676
"updateDependabot": process.cli.options[ "update-dependabot" ],
7777
"updateRepository": process.cli.options[ "update-repository" ],
78-
"updateNpm": process.cli.options[ "update-npm" ],
78+
"updateTags": process.cli.options[ "update-tags" ],
7979
"commit": process.cli.options.commit,
8080
logger,
8181
} );

lib/package.js

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -401,24 +401,11 @@ export default class Package {
401401
} );
402402
}
403403

404-
async updateMetadata ( { updateDependabot, updateRepository, updateNpm, commit, logger } = {} ) {
404+
async updateMetadata ( { updateDependabot, updateRepository, updateTags, commit, logger } = {} ) {
405405
logger ||= globalThis.console;
406406

407407
var res;
408408

409-
// fix release tags
410-
if ( this.cliConfig?.release.enabled ) {
411-
res = await this.fixReleaseTags();
412-
413-
logger.log( "Fix release tags:", res.ok
414-
? ( res.data.updated
415-
? ansi.ok( " Updated " )
416-
: "Not modified" )
417-
: ansi.error( " " + res.statusText + " " ) );
418-
419-
if ( !res.ok ) return res;
420-
}
421-
422409
// configure upstream repository
423410
if ( updateRepository ) {
424411
res = await this.configureUpstreamRepository();
@@ -445,12 +432,30 @@ export default class Package {
445432
if ( !res.ok ) return res;
446433
}
447434

448-
// update npm package tags
449-
if ( updateNpm ) {
435+
// update release tags
436+
if ( updateTags ) {
437+
438+
// update release tags in repository
439+
if ( this.cliConfig?.release.enabled ) {
440+
res = await this.updateReleaseTags();
441+
442+
const statusText = Object.entries( res.data?.tags || {} )
443+
.map( ( [ tag, version ] ) => `${ tag }: ${ version || "-" }` )
444+
.join( ", " );
445+
446+
logger.log( "Update release tags:", res.ok
447+
? ( res.data.updated
448+
? ansi.ok( " Updated " ) + ", " + statusText
449+
: "Not modified, " + statusText )
450+
: ansi.error( " " + res.statusText + " " ) );
451+
452+
if ( !res.ok ) return res;
453+
}
454+
450455
for ( const pkg of [ this, ...this.subPackages ] ) {
451456
if ( pkg.isPrivate ) continue;
452457

453-
res = await pkg.npm.setTags( {
458+
res = await pkg.npm.updateTags( {
454459
logger,
455460
} );
456461

@@ -461,7 +466,7 @@ export default class Package {
461466
return result( 200 );
462467
}
463468

464-
async fixReleaseTags () {
469+
async updateReleaseTags () {
465470
var res,
466471
updated = false;
467472

@@ -471,30 +476,29 @@ export default class Package {
471476
} );
472477
}
473478

474-
res = await this.git.getReleases();
479+
res = await this.git.getReleaseTags();
475480
if ( !res.ok ) return res;
476-
477-
const releases = res.data,
478-
tags = releases.getReleaseTagsVersions( {
479-
"majorTags": true,
480-
} );
481+
const tags = res.data;
481482

482483
for ( const tag in tags ) {
483-
if ( !tags[ tag ] ) continue;
484484

485-
res = await this.git.getCommit( { "commitRef": tags[ tag ].versionString } );
486-
if ( !res.ok ) return res;
487-
const commit = res.data;
485+
// update tag
486+
if ( tags[ tag ].action === "update" ) {
487+
res = await this.git.exec( [ "tag", "--force", "--annotate", "--message", this.createReleaseTagAnnotation( tag ), tag, tags[ tag ].version ] );
488+
if ( !res.ok ) return res;
489+
490+
res = await this.git.exec( [ "push", "--atomic", "--force", "origin", tag ] );
491+
if ( !res.ok ) return res;
488492

489-
if ( !commit.tags.has( tag ) ) {
490-
const message = TAG_MESSAGES[ tag ] || `Latest stable release for the major branch: ${ tag }`;
493+
updated = true;
494+
}
491495

492-
// set tag
493-
res = await this.git.exec( [ "tag", "--force", "--annotate", "--message", message, tag, commit.hash ] );
496+
// delete tag
497+
else if ( tags[ tag ].action === "delete" ) {
498+
res = await this.git.exec( [ "tag", "--delete", tag ] );
494499
if ( !res.ok ) return res;
495500

496-
// push tag
497-
res = await this.git.exec( [ "push", "--atomic", "--force", "origin", tag ] );
501+
res = await this.git.exec( [ "push", "--delete", "origin", tag ] );
498502
if ( !res.ok ) return res;
499503

500504
updated = true;
@@ -503,6 +507,11 @@ export default class Package {
503507

504508
return result( 200, {
505509
updated,
510+
"tags": Object.entries( tags ).reduce( ( tags, [ tag, { version } ] ) => {
511+
if ( version ) {
512+
tags[ tag ] = version;
513+
}
514+
}, {} ),
506515
} );
507516
}
508517

@@ -1159,6 +1168,10 @@ export default class Package {
11591168
} );
11601169
}
11611170

1171+
createReleaseTagAnnotation ( tag ) {
1172+
TAG_MESSAGES[ tag ] || `Latest stable release for the branch: ${ tag }`;
1173+
}
1174+
11621175
clearCache () {
11631176
this.#clearCache();
11641177
}

lib/package/npm.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default class Npm {
5656
);
5757
if ( !res.ok ) return res;
5858

59-
res = await this.setTags( {
59+
res = await this.updateTags( {
6060
"log": true,
6161
repeatOnError,
6262
} );
@@ -70,7 +70,7 @@ export default class Npm {
7070
return result( 200 );
7171
}
7272

73-
async setTags ( { repeatOnError, logger } = {} ) {
73+
async updateTags ( { repeatOnError, logger } = {} ) {
7474
if ( this.pkg.isPrivate ) return result( [ 200, "Package is private" ] );
7575

7676
logger ||= globalThis.console;
@@ -83,7 +83,7 @@ export default class Npm {
8383
res = result( [ 500, "Package has no name" ] );
8484
}
8585
else {
86-
res = await this.#setTags();
86+
res = await this.#updateTags();
8787

8888
error = true;
8989
}
@@ -92,7 +92,7 @@ export default class Npm {
9292
.map( ( [ tag, version ] ) => `${ tag }: ${ version || "-" }` )
9393
.join( ", " );
9494

95-
logger.log( "Set npm tags:", res.ok
95+
logger.log( "Update npm tags:", res.ok
9696
? ( res.data.updated
9797
? ansi.ok( " Updated " ) + ", " + statusText
9898
: "Not modified, " + statusText )
@@ -216,49 +216,50 @@ export default class Npm {
216216
return result( 200 );
217217
}
218218

219-
async #setTags () {
219+
async #updateTags () {
220220
var res,
221221
updated = false;
222222

223-
res = await this.pkg.git.getReleases();
223+
res = await this.pkg.git.getReleaseTags();
224224
if ( !res.ok ) return res;
225225

226-
const releases = res.data,
227-
tags = releases.getReleaseTagsVersions( {
228-
"majorTags": false,
229-
} );
226+
const tags = res.data;
230227

231228
for ( const tag in tags ) {
229+
if ( tag !== "latest" && tag !== "next" ) continue;
230+
231+
if ( !tags[ tag ].version ) continue;
232+
232233
tags[ tag ] = tags[ tag ].version;
233234
}
234235

235-
const updates = { ...tags };
236+
const updates = {
237+
"latest": null,
238+
"next": null,
239+
...tags,
240+
};
236241

237242
res = await this.api.getPackageTags( this.pkg.name );
238243
if ( !res.ok ) return res;
239-
240244
const remoteTags = res.data;
241245

242-
for ( const tag in remoteTags ) {
243-
if ( !updates[ tag ] ) {
244-
updates[ tag ] = null;
245-
}
246-
else if ( remoteTags[ tag ] === updates[ tag ] ) {
247-
delete updates[ tag ];
248-
}
249-
}
250-
251246
for ( const [ tag, version ] of Object.entries( updates ) ) {
252-
if ( version ) {
247+
248+
// set
249+
if ( version && remoteTags[ tag ] !== version ) {
253250
res = await this.api.setPackageTag( this.pkg.name, version, tag );
254251
if ( !res.ok ) return res;
252+
253+
updated = true;
255254
}
256-
else {
255+
256+
// delete
257+
else if ( remoteTags[ tag ] && !version ) {
257258
res = await this.api.deletePackageTag( this.pkg.name, tag );
258259
if ( !res.ok ) return res;
259-
}
260260

261-
updated = true;
261+
updated = true;
262+
}
262263
}
263264

264265
return result( 200, {

lib/package/release.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export default class Publish {
246246
async () => {
247247
process.stdout.write( `Adding "${ this.#latestTag }" tag ... ` );
248248

249-
const res = await this.#pkg.git.exec( [ "tag", "--force", "--annotate", "--message", "Latest stable release", this.#latestTag ] );
249+
const res = await this.#pkg.git.exec( [ "tag", "--force", "--annotate", "--message", this.#pkg.createReleaseTagAnnotation( "latest" ), this.#latestTag ] );
250250

251251
console.log( res + "" );
252252

@@ -271,7 +271,7 @@ export default class Publish {
271271
async () => {
272272
process.stdout.write( `Adding "${ this.#nextTag }" tag ... ` );
273273

274-
const res = await this.#pkg.git.exec( [ "tag", "--force", "--annotate", "--message", "Next release", this.#nextTag ] );
274+
const res = await this.#pkg.git.exec( [ "tag", "--force", "--annotate", "--message", this.#pkg.createReleaseTagAnnotation( "next" ), this.#nextTag ] );
275275

276276
console.log( res + "" );
277277

@@ -296,7 +296,7 @@ export default class Publish {
296296
async () => {
297297
process.stdout.write( `Adding "${ this.#majorVersionStableTag }" tag ... ` );
298298

299-
const res = await this.#pkg.git.exec( [ "tag", "--force", "--annotate", "--message", `Latest stable release for the major branch: ${ this.#majorVersionStableTag }`, this.#majorVersionStableTag ] );
299+
const res = await this.#pkg.git.exec( [ "tag", "--force", "--annotate", "--message", this.createReleaseTagAnnotation( this.#majorVersionStableTag ), this.#majorVersionStableTag ] );
300300

301301
console.log( res + "" );
302302

0 commit comments

Comments
 (0)