Skip to content

Commit 994b50f

Browse files
committed
refactor: refactor branch status
1 parent 7a6cf18 commit 994b50f

2 files changed

Lines changed: 104 additions & 24 deletions

File tree

lib/commands/ls.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default class extends Command {
6666
: "-" ) },
6767
"pushed": { "title": ansi.hl( "NOT\nPUSHED" ), "width": 8, "align": "end", "headerAlign": "center" },
6868
"last": { "title": ansi.hl( "LAST\nRELEASE" ), "width": 20, "align": "end", "headerAlign": "center" },
69-
"current": { "title": ansi.hl( "CURRENT\nRELEASE" ), "width": 20, "align": "end", "headerAlign": "center" },
69+
"current": { "title": ansi.hl( "CURRENT\nRELEASE" ), "width": 23, "align": "end", "headerAlign": "center" },
7070
"unreleased": { "title": ansi.hl( "UNRELEASED\nCOMMITS" ), "width": 12, "align": "end", "headerAlign": "center" },
7171
},
7272
} ).pipe( process.stdout );
@@ -121,7 +121,9 @@ export default class extends Command {
121121
? ansi.error( ` ${ currentBranchPushStatus } ` )
122122
: " - ",
123123
"last": this.#formatVersion( status.releases.lastRelease, pkg.isReleaseEnabled ),
124-
"current": this.#formatVersion( status.currentRelease, pkg.isReleaseEnabled ),
124+
"current": this.#formatVersion( status.currentRelease, pkg.isReleaseEnabled ) + ( status.currentRelease && status.releases?.lastRelease.eq( status.currentRelease )
125+
? " ✅️"
126+
: " " ),
125127
"unreleased": pkg.isReleaseEnabled
126128
? ( status.currentReleaseDistance
127129
? ansi.error( ` ${ status.currentReleaseDistance } ` )

lib/commands/status.js

Lines changed: 100 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ansi from "#core/ansi";
2+
import Table from "#core/text/table";
23
import Command from "#lib/command";
34

45
export default class extends Command {
@@ -42,14 +43,14 @@ Last release (next): ${ ansi.hl( status.releases.lastRelease || "-" ) }
4243
Is dirty: ${ status.isDirty
4344
? ansi.error( " DIRTY " )
4445
: ansi.ok( " COMMITED " ) }
45-
Branch sync. status: ${ Object.keys( status.branchStatus )
46-
.map( branch => branch + ": " + this.#getBranchStatusText( status.branchStatus[ branch ] ) )
47-
.join( "\n" + " ".repeat( 25 ) ) }
4846
Unreleased commits: ${ pkg.isReleaseEnabled
4947
? ( status.currentReleaseDistance
5048
? ansi.error( " " + status.currentReleaseDistance + " " )
5149
: ansi.ok( " RELEASED " ) )
5250
: "-" }
51+
52+
Branch status:
53+
${ await this.#createBranchStatus( pkg, status.branchStatus ) }
5354
`.trim() );
5455

5556
if ( upstream ) {
@@ -80,27 +81,104 @@ Clone wiki: ${ ansi.hl( upstream.wikiSshCloneUrl ) }
8081
}
8182

8283
// private
83-
#getBranchStatusText ( status ) {
84-
if ( status.upstream ) {
85-
if ( status.synchronized ) {
86-
return "OK";
87-
}
88-
else {
89-
const text = [];
90-
91-
if ( status.ahead ) {
92-
text.push( "push " + ansi.error( ` ${ status.ahead } ` ) );
93-
}
84+
async #createBranchStatus ( pkg, branchStatus ) {
85+
const table = new Table( {
86+
"ansi": true,
87+
"columns": {
88+
"branch": {
89+
"title": ansi.hl( "BRANCH" ),
90+
"headerAlign": "center",
91+
"headerValign": "end",
92+
"width": 30,
93+
},
94+
"pushStatus": {
95+
"title": ansi.hl( "PUSH STATUS" ),
96+
"headerAlign": "center",
97+
"headerValign": "end",
98+
"width": 20,
99+
"align": "end",
100+
"format": status => {
101+
if ( status.upstream ) {
102+
if ( status.synchronized ) {
103+
return " - ";
104+
}
105+
else {
106+
const text = [];
107+
108+
if ( status.ahead ) {
109+
text.push( "push: " + ansi.error( ` ${ status.ahead } ` ) );
110+
}
111+
112+
if ( status.behind ) {
113+
text.push( "pull: " + ansi.error( ` ${ status.behind } ` ) );
114+
}
115+
116+
return text.join( ", " );
117+
}
118+
}
119+
else {
120+
return ansi.dim( "NOT TRACKED" );
121+
}
122+
},
123+
},
124+
"currentRelease": {
125+
"title": ansi.hl( "CURRENT\nRELEASE" ),
126+
"headerAlign": "center",
127+
"headerValign": "end",
128+
"width": 15,
129+
"align": "end",
130+
"format": status => {
131+
if ( pkg.isReleaseEnabled ) {
132+
if ( status.currentRelease ) {
133+
return `${ status.currentRelease.versionString }${ status.releases.lastRelease.eq( status.currentRelease )
134+
? " ✅️"
135+
: " " }`;
136+
}
137+
else {
138+
return " - " + " ";
139+
}
140+
}
141+
else {
142+
return ansi.dim( " DISABLED " );
143+
}
144+
},
145+
},
146+
"unreleasedCommits": {
147+
"title": ansi.hl( "UNRELEASED\nCOMMITS" ),
148+
"headerAlign": "center",
149+
"headerValign": "end",
150+
"width": 15,
151+
"align": "end",
152+
"format": status => {
153+
if ( pkg.isReleaseEnabled ) {
154+
if ( status.currentReleaseDistance ) {
155+
return ansi.error( ` ${ status.currentReleaseDistance } ` );
156+
}
157+
else {
158+
return " - ";
159+
}
160+
}
161+
else {
162+
return ansi.dim( " DISABLED " );
163+
}
164+
},
165+
},
166+
},
167+
} );
94168

95-
if ( status.behind ) {
96-
text.push( "pull " + ansi.error( ` ${ status.behind } ` ) );
97-
}
169+
for ( const branch of Object.keys( branchStatus ).sort() ) {
170+
const res = await pkg.git.getCurrentRelease( { "commitRef": branch } );
98171

99-
return text.join( ", " );
100-
}
101-
}
102-
else {
103-
return "NOT TRACKED";
172+
table.write( {
173+
branch,
174+
"pushStatus": branchStatus[ branch ],
175+
"currentRelease": res.data,
176+
"unreleasedCommits": res.data,
177+
} );
104178
}
179+
180+
table.end();
181+
182+
return table.content + "✅️ - latest release";
105183
}
106184
}

0 commit comments

Comments
 (0)