Skip to content

Commit 88722b5

Browse files
committed
refactor: refactor release
1 parent fc49959 commit 88722b5

8 files changed

Lines changed: 550 additions & 308 deletions

File tree

lib/commands/package.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ export default class extends Command {
88
"commands": {
99
"release": {
1010
"short": "R",
11-
"title": "Release and publish package",
11+
"title": "Release package",
1212
"module": () => new URL( "package/release.js", import.meta.url ),
1313
},
14+
"publish": {
15+
"short": "P",
16+
"title": "Publish package",
17+
"module": () => new URL( "package/publish.js", import.meta.url ),
18+
},
1419
"update-dependencies": {
1520
"short": "u",
1621
"title": "Update package dependencies",

lib/commands/package/publish.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import Command from "#lib/command";
2+
3+
export default class extends Command {
4+
5+
// static
6+
static cli () {
7+
return {
8+
"options": {
9+
"access": {
10+
"short": "a",
11+
"description": "Set package access level.",
12+
"schema": { "enum": [ "public", "private" ] },
13+
},
14+
"yes": {
15+
"short": "y",
16+
"description": `answer "YES" on all questions`,
17+
"default": false,
18+
"schema": { "type": "boolean" },
19+
},
20+
},
21+
"arguments": {
22+
"commit-reference": {
23+
"description": "Git commit reference to publish.",
24+
"default": "HEAD",
25+
"schema": { "type": "string" },
26+
},
27+
},
28+
};
29+
}
30+
31+
// public
32+
async run () {
33+
const pkg = this._findGitPackage();
34+
if ( !pkg ) return result( [ 500, "Unable to find root package" ] );
35+
36+
var res;
37+
38+
res = await pkg.npm.publish( {
39+
"commitRef": process.cli.arguments[ "commit-reference" ],
40+
"repeatOnError": !process.cli.options.yes,
41+
"accessStatus": process.cli.options.access,
42+
} );
43+
if ( !res.ok ) return res;
44+
45+
// get sub-packages
46+
const subPackages = pkg.subPackages;
47+
48+
// publish sub-packages
49+
for ( const pkg of subPackages ) {
50+
res = await pkg.npm.publish( {
51+
"commitRef": process.cli.arguments[ "commit-reference" ],
52+
"repeatOnError": !process.cli.options.yes,
53+
"accessStatus": process.cli.options.access,
54+
} );
55+
if ( !res.ok ) return res;
56+
}
57+
58+
return result( 200 );
59+
}
60+
}

lib/commands/package/release.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ export default class extends Command {
1212
"default": false,
1313
"schema": { "type": "boolean" },
1414
},
15+
"publish": {
16+
"short": "p",
17+
"description": "publish package",
18+
"default": false,
19+
"schema": { "type": "boolean" },
20+
},
1521
},
1622
"arguments": {
1723
"pre-release-tag": {
@@ -34,6 +40,7 @@ export default class extends Command {
3440

3541
const release = await pkg.release( {
3642
"preReleaseTag": process.cli.arguments[ "pre-release-tag" ],
43+
"publish": process.cli.options.publish,
3744
"yes": process.cli.options.yes,
3845
} );
3946

lib/npm.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,12 @@ export default class Npm {
7070
proc.once( "close", code => {
7171
var res;
7272

73-
if ( code ) {
74-
let data = Buffer.concat( stdout );
75-
data = JSON.parse( data );
73+
const data = JSON.parse( Buffer.concat( stdout ) );
7674

75+
if ( code ) {
7776
res = result( [ 500, data.error.summary ], data );
7877
}
7978
else {
80-
let data = Buffer.concat( stdout );
81-
data = JSON.parse( data );
82-
8379
res = result( 200, data );
8480
}
8581

@@ -145,10 +141,8 @@ export default class Npm {
145141
return res;
146142
}
147143

148-
async setPackageAccessStatus ( packageName, privateAccess, { cwd, registry } = {} ) {
149-
const args = [ "access", "set", "status=" + ( privateAccess
150-
? "private"
151-
: "public" ), packageName ];
144+
async setPackageAccessStatus ( packageName, accessStatus, { cwd, registry } = {} ) {
145+
const args = [ "access", "set", "status=" + accessStatus, packageName ];
152146

153147
const res = await this.exec( args, {
154148
cwd,
@@ -208,7 +202,7 @@ export default class Npm {
208202
} );
209203
}
210204

211-
async publish ( { executablesPatterns, packPath, "private": privateAccess, tag, cwd, registry } = {} ) {
205+
async publish ( { executablesPatterns, packPath, accessStatus, tag, cwd, registry } = {} ) {
212206
if ( !packPath ) {
213207
const res = await this.pack( {
214208
executablesPatterns,
@@ -223,8 +217,8 @@ export default class Npm {
223217

224218
const args = [ "publish" ];
225219

226-
if ( privateAccess != null ) {
227-
args.push( "--access", privateAccess
220+
if ( accessStatus ) {
221+
args.push( "--access", accessStatus === "private"
228222
? "restricted"
229223
: "public" );
230224
}

0 commit comments

Comments
 (0)