Skip to content

Commit a690857

Browse files
committed
feat: add possibility to update npm package tags to metadata updater
1 parent fe8a1f3 commit a690857

4 files changed

Lines changed: 119 additions & 83 deletions

File tree

lib/commands/package/update-metadata.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,22 @@ export default class extends Command {
66
static cli () {
77
return {
88
"options": {
9-
"repository": {
9+
"update-dependabot": {
10+
"negatedShort": "D",
11+
"description": "do not update dependabot config",
12+
"default": true,
13+
"schema": { "type": "boolean" },
14+
},
15+
"update-repository": {
1016
"short": "r",
1117
"description": "configure upstream repository",
1218
"default": false,
1319
"schema": { "type": "boolean" },
1420
},
15-
"dependabot": {
16-
"negatedShort": "D",
17-
"description": "do not update dependabot config",
18-
"default": true,
21+
"update-npm": {
22+
"short": "n",
23+
"description": "update npm package tags",
24+
"default": false,
1925
"schema": { "type": "boolean" },
2026
},
2127
"commit": {
@@ -35,10 +41,10 @@ export default class extends Command {
3541
if ( !pkg ) return result( [ 500, "Unable to find root package" ] );
3642

3743
const res = await pkg.updateMetadata( {
38-
"repository": process.cli.options.repository,
39-
"dependabot": process.cli.options.dependabot,
44+
"updateDependabot": process.cli.options[ "update-dependabot" ],
45+
"updateRepository": process.cli.options[ "update-repository" ],
46+
"updateNpm": process.cli.options[ "update-npm" ],
4047
"commit": process.cli.options.commit,
41-
"log": true,
4248
} );
4349

4450
return res;

lib/commands/workspace/update-metadata.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ansi from "#core/ansi";
2+
import Logger from "#core/logger";
23
import ThreadsPoolQueue from "#core/threads/pool/queue";
34
import Command from "#lib/command";
45

@@ -8,16 +9,22 @@ export default class extends Command {
89
static cli () {
910
return {
1011
"options": {
11-
"repository": {
12+
"update-dependabot": {
13+
"negatedShort": "D",
14+
"description": "do not update dependabot config",
15+
"default": true,
16+
"schema": { "type": "boolean" },
17+
},
18+
"update-repository": {
1219
"short": "r",
1320
"description": "configure upstream repository",
1421
"default": false,
1522
"schema": { "type": "boolean" },
1623
},
17-
"dependabot": {
18-
"negatedShort": "D",
19-
"description": "do not update dependabot config",
20-
"default": true,
24+
"update-npm": {
25+
"short": "n",
26+
"description": "update npm package tags",
27+
"default": false,
2128
"schema": { "type": "boolean" },
2229
},
2330
"commit": {
@@ -61,14 +68,22 @@ export default class extends Command {
6168

6269
for ( const pkg of packages ) {
6370
threads.pushThread( async () => {
64-
const res = await pkg.updateMetadata( {
65-
"repository": process.cli.options.repository,
66-
"dependabot": process.cli.options.dependabot,
67-
"commit": process.cli.options.commit,
68-
"log": false,
69-
} );
71+
const logger = new Logger( {
72+
"stdout": "pipe",
73+
"stderr": "stdout",
74+
} ),
75+
res = await pkg.updateMetadata( {
76+
"updateDependabot": process.cli.options[ "update-dependabot" ],
77+
"updateRepository": process.cli.options[ "update-repository" ],
78+
"updateNpm": process.cli.options[ "update-npm" ],
79+
"commit": process.cli.options.commit,
80+
logger,
81+
} );
7082

71-
res.data.pkg = pkg;
83+
res.data = {
84+
pkg,
85+
"log": logger.flush(),
86+
};
7287

7388
return res;
7489
} );

lib/package.js

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -396,55 +396,51 @@ export default class Package {
396396
} );
397397
}
398398

399-
async updateMetadata ( { repository, dependabot, commit, log } = {} ) {
400-
var res,
401-
report = "";
399+
async updateMetadata ( { updateDependabot, updateRepository, updateNpm, commit, logger } = {} ) {
400+
logger ||= globalThis.console;
401+
402+
var res;
402403

403404
// configure upstream repository
404-
if ( repository ) {
405+
if ( updateRepository ) {
405406
res = await this.configureUpstreamRepository();
406407

407-
const reportText = "Configure upstream repository: " + ( res.ok
408+
logger.log( "Configure upstream repository:", res.ok
408409
? ( res.data.updated
409410
? ansi.ok( " Updated " )
410411
: "Not modified" )
411412
: ansi.error( " " + res.statusText + " " ) );
412413

413-
if ( log ) console.log( reportText );
414-
415-
report += reportText + "\n";
416-
417-
if ( !res.ok ) {
418-
return result( res, {
419-
"log": report,
420-
} );
421-
}
414+
if ( !res.ok ) return res;
422415
}
423416

424417
// update metadata
425418
{
426-
res = await this.#updateMetadata( { dependabot, commit } );
419+
res = await this.#updateMetadata( { updateDependabot, commit } );
427420

428-
const reportText = "Update metadata: " + ( res.ok
421+
logger.log( "Update metadata:", res.ok
429422
? ( res.data.updated
430423
? ansi.ok( " Updated " )
431424
: "Not modified" )
432425
: ansi.error( " " + res.statusText + " " ) );
433426

434-
if ( log ) console.log( reportText );
427+
if ( !res.ok ) return res;
428+
}
435429

436-
report += reportText + "\n";
430+
// XXX subpackages
431+
// update npm package tags
432+
if ( updateNpm ) {
433+
const packages = [ this, ...this.subPackages ];
437434

438-
if ( !res.ok ) {
439-
return result( res, {
440-
"log": report,
435+
for ( const pkg of packages ) {
436+
res = await pkg.npm.setTags( {
437+
logger,
441438
} );
439+
if ( !res.ok ) return res;
442440
}
443441
}
444442

445-
return result( 200, {
446-
"log": report,
447-
} );
443+
return result( 200 );
448444
}
449445

450446
async updateFilesMode () {
@@ -1114,7 +1110,7 @@ export default class Package {
11141110
this.#dependencies = undefined;
11151111
}
11161112

1117-
async #updateMetadata ( { dependabot, commit } = {} ) {
1113+
async #updateMetadata ( { updateDependabot, commit } = {} ) {
11181114
var res, updated;
11191115

11201116
// get git status
@@ -1202,7 +1198,7 @@ export default class Package {
12021198
}
12031199

12041200
// dependabot
1205-
if ( dependabot ) {
1201+
if ( updateDependabot ) {
12061202
res = await this.#updateDependabotConfig();
12071203

12081204
if ( !res.ok ) return res;

lib/package/npm.js

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ansi from "#core/ansi";
12
import NpmApi from "#core/api/npm";
23
import { TmpDir } from "#core/tmp";
34
import { repeatAction } from "#core/utils";
@@ -29,32 +30,36 @@ export default class Npm {
2930

3031
res = await repeatAction(
3132
async () => {
32-
var res;
33+
var res, error;
3334

3435
try {
35-
process.stdout.write( `Publishing npm package "${ this.pkg.name }" ...` );
36-
3736
res = await this.#publish( { commitRef, accessStatus } );
38-
39-
console.log( res + "" );
40-
41-
return res;
4237
}
4338
catch ( e ) {
4439
res = result.catch( e );
4540

46-
console.log( "Publishing npm package ...", res + "" );
41+
error = true;
42+
}
4743

44+
console.log( `Publish npm package "${ this.pkg.name }":`, res + "" );
45+
46+
if ( error ) {
4847
throw res;
4948
}
49+
else {
50+
return res;
51+
}
5052
},
5153
{
5254
repeatOnError,
5355
}
5456
);
5557
if ( !res.ok ) return res;
5658

57-
res = await this.setTags( { repeatOnError } );
59+
res = await this.setTags( {
60+
"log": true,
61+
repeatOnError,
62+
} );
5863
if ( !res.ok ) return res;
5964

6065
if ( accessStatus ) {
@@ -65,17 +70,21 @@ export default class Npm {
6570
return result( 200 );
6671
}
6772

68-
async setTags ( { repeatOnError } = {} ) {
73+
async setTags ( { repeatOnError, logger } = {} ) {
74+
logger ||= globalThis.console;
75+
6976
if ( this.pkg.isPrivate ) return result( [ 200, "Package is private" ] );
7077
if ( !this.pkg.name ) return result( [ 500, "Package has no name" ] );
7178

7279
return repeatAction(
7380
async () => {
74-
process.stdout.write( `Setting npm tags for "${ this.pkg.name }" ...` );
75-
7681
const res = await this.#setTags();
7782

78-
console.log( res + "" );
83+
logger.log( "Set npm tags:", res.ok
84+
? ( res.data.updated
85+
? ansi.ok( " Updated " )
86+
: "Not modified" )
87+
: ansi.error( " " + res.statusText + " " ) );
7988

8089
if ( !res.ok ) throw res;
8190

@@ -89,30 +98,33 @@ export default class Npm {
8998

9099
async setAccessStatus ( accessStatus, { repeatOnError } = {} ) {
91100
if ( this.pkg.isPrivate ) return result( [ 200, "Package is private" ] );
92-
if ( !this.pkg.name ) return result( [ 500, "Package has no name" ] );
93101

94-
if ( accessStatus && this.pkg.name.startsWith( "@" ) ) {
95-
const res = await repeatAction(
96-
async () => {
97-
process.stdout.write( `Setting npm access status for "${ this.pkg.name }"...` );
102+
return repeatAction(
103+
async () => {
104+
var res, error;
98105

99-
const res = await this.api.setAccessStatus( this.pkg.name, accessStatus );
106+
if ( !this.pkg.name?.startsWith( "@" ) ) {
107+
res = result( [ 500, "Package name is not valud" ] );
108+
}
109+
else {
110+
res = await this.api.setAccessStatus( this.pkg.name, accessStatus );
100111

101-
console.log( res + "" );
112+
error = true;
113+
}
102114

103-
if ( !res.ok ) throw res;
115+
console.log( `Set npm access status for "${ this.pkg.name }":`, res + "" );
104116

117+
if ( !res.ok && error ) {
118+
throw res;
119+
}
120+
else {
105121
return res;
106-
},
107-
{
108-
repeatOnError,
109122
}
110-
);
111-
112-
if ( !res.ok ) return res;
113-
}
114-
115-
return result( 200 );
123+
},
124+
{
125+
repeatOnError,
126+
}
127+
);
116128
}
117129

118130
// private
@@ -184,7 +196,8 @@ export default class Npm {
184196
}
185197

186198
async #setTags () {
187-
var res;
199+
var res,
200+
updated = false;
188201

189202
res = await this.api.getPackageTags( this.pkg.name );
190203
if ( !res.ok ) return res;
@@ -196,24 +209,30 @@ export default class Npm {
196209
if ( !res.ok ) return res;
197210
const commit = res.data;
198211

199-
let version;
212+
let tagVersion;
200213

201-
if ( commit?.isRelease ) version = commit.releaseVersion.version;
214+
if ( commit?.isRelease ) tagVersion = commit.releaseVersion.version;
202215

203-
if ( version ) {
204-
if ( versions[ tag ] !== version ) {
205-
res = await this.api.setPackageTag( this.pkg.name, version, tag );
216+
if ( tagVersion ) {
217+
if ( versions[ tag ] !== tagVersion ) {
218+
res = await this.api.setPackageTag( this.pkg.name, tagVersion, tag );
206219
if ( !res.ok ) return res;
220+
221+
updated = true;
207222
}
208223
}
209224
else {
210225
if ( versions[ tag ] ) {
211226
res = await this.api.deletePackageTag( this.pkg.name, tag );
212227
if ( !res.ok ) return res;
228+
229+
updated = true;
213230
}
214231
}
215232
}
216233

217-
return result( 200 );
234+
return result( 200, {
235+
updated,
236+
} );
218237
}
219238
}

0 commit comments

Comments
 (0)