@@ -11,6 +11,8 @@ import { StashAspect } from './stash.aspect';
11
11
import { StashCmd , StashListCmd , StashLoadCmd , StashSaveCmd } from './stash.cmd' ;
12
12
import { StashData } from './stash-data' ;
13
13
import { StashFiles } from './stash-files' ;
14
+ import { getBasicLog } from '@teambit/harmony.modules.get-basic-log' ;
15
+ import { RemoveAspect , RemoveMain } from '@teambit/remove' ;
14
16
15
17
type ListResult = {
16
18
id : string ;
@@ -23,33 +25,45 @@ export class StashMain {
23
25
constructor (
24
26
private workspace : Workspace ,
25
27
private checkout : CheckoutMain ,
26
- private snapping : SnappingMain
28
+ private snapping : SnappingMain ,
29
+ private remove : RemoveMain
27
30
) {
28
31
this . stashFiles = new StashFiles ( workspace ) ;
29
32
}
30
33
31
- async save ( options : { message ?: string ; pattern ?: string } ) : Promise < ComponentID [ ] > {
34
+ async save ( options : { message ?: string ; pattern ?: string ; includeNew ?: boolean } ) : Promise < ComponentID [ ] > {
32
35
const compIds = options ?. pattern ? await this . workspace . idsByPattern ( options ?. pattern ) : this . workspace . listIds ( ) ;
33
36
const comps = await this . workspace . getMany ( compIds ) ;
37
+ const newComps : Component [ ] = [ ] ;
34
38
const modifiedComps = compact (
35
39
await Promise . all (
36
40
comps . map ( async ( comp ) => {
37
- if ( ! comp . head ) return undefined ; // it's new
41
+ if ( ! comp . head ) {
42
+ // it's a new component
43
+ if ( options . includeNew ) newComps . push ( comp ) ;
44
+ return undefined ;
45
+ }
38
46
const isModified = await this . workspace . isModified ( comp ) ;
39
47
if ( isModified ) return comp ;
40
48
return undefined ;
41
49
} )
42
50
)
43
51
) ;
44
- if ( ! modifiedComps . length ) return [ ] ;
52
+ const allComps = [ ...modifiedComps , ...newComps ] ;
53
+ if ( ! allComps . length ) return [ ] ;
45
54
46
55
// per comp: create Version object, save it in the local scope and return the hash. don't save anything in the .bitmap
47
- const consumeComponents = modifiedComps . map ( ( comp ) => comp . state . _consumer ) ;
56
+ const consumeComponents = allComps . map ( ( comp ) => comp . state . _consumer ) ;
48
57
await this . snapping . _addFlattenedDependenciesToComponents ( consumeComponents ) ;
49
58
const hashPerId = await Promise . all (
50
- modifiedComps . map ( async ( comp ) => {
59
+ allComps . map ( async ( comp ) => {
51
60
const versionObj = await this . addComponentDataToRepo ( comp ) ;
52
- return { id : comp . id , hash : versionObj . hash ( ) . toString ( ) } ;
61
+ return {
62
+ id : comp . id ,
63
+ hash : versionObj . hash ( ) . toString ( ) ,
64
+ bitmapEntry : this . workspace . bitMap . getBitmapEntry ( comp . id ) . toPlainObject ( ) ,
65
+ isNew : ! comp . id . hasVersion ( ) ,
66
+ } ;
53
67
} )
54
68
) ;
55
69
await this . workspace . scope . legacyScope . objects . persist ( ) ;
@@ -63,8 +77,13 @@ export class StashMain {
63
77
skipNpmInstall : true ,
64
78
reset : true ,
65
79
} ) ;
80
+ // remove new components from the workspace
81
+ const newCompIds = newComps . map ( ( c ) => c . id ) ;
82
+ if ( newComps . length ) {
83
+ await this . remove . removeLocallyByIds ( newCompIds ) ;
84
+ }
66
85
67
- return modifiedCompIds ;
86
+ return [ ... modifiedCompIds , ... newCompIds ] ;
68
87
}
69
88
70
89
async list ( ) : Promise < ListResult [ ] > {
@@ -87,8 +106,14 @@ export class StashMain {
87
106
throw new BitError ( 'no stashed components found' ) ;
88
107
}
89
108
const stashData = await this . stashFiles . getStashData ( stashFile ) ;
90
- const compIds = stashData . stashCompsData . map ( ( c ) => c . id ) ;
91
- const versionPerId = stashData . stashCompsData . map ( ( c ) => c . id . changeVersion ( c . hash . toString ( ) ) ) ;
109
+ const stashModifiedCompsData = stashData . stashCompsData . filter ( ( c ) => ! c . isNew ) ;
110
+ const stashNewCompsData = stashData . stashCompsData . filter ( ( c ) => c . isNew ) ;
111
+ const compIds = stashModifiedCompsData . map ( ( c ) => c . id ) ;
112
+ const versionPerId = stashModifiedCompsData . map ( ( c ) => c . id . changeVersion ( c . hash . toString ( ) ) ) ;
113
+ const stashedBitmapEntries = stashNewCompsData . map ( ( s ) => ( {
114
+ ...s . bitmapEntry ,
115
+ id : s . id . changeVersion ( s . hash . toString ( ) ) ,
116
+ } ) ) ;
92
117
93
118
await this . checkout . checkout ( {
94
119
...checkoutProps ,
@@ -98,17 +123,21 @@ export class StashMain {
98
123
skipUpdatingBitmap : true ,
99
124
promptMergeOptions : true ,
100
125
loadStash : true ,
126
+ stashedBitmapEntries,
101
127
} ) ;
102
128
103
129
await this . stashFiles . deleteStashFile ( stashFile ) ;
104
130
105
- return compIds ;
131
+ return [ ... compIds , ... stashNewCompsData . map ( ( c ) => c . id ) ] ;
106
132
}
107
133
108
134
private async addComponentDataToRepo ( component : Component ) {
109
135
const previousVersion = component . getSnapHash ( ) ;
110
136
const consumerComponent = component . state . _consumer . clone ( ) as ConsumerComponent ;
111
137
consumerComponent . setNewVersion ( ) ;
138
+ if ( ! consumerComponent . log ) {
139
+ consumerComponent . log = await getBasicLog ( ) ;
140
+ }
112
141
const { version, files } =
113
142
await this . workspace . scope . legacyScope . sources . consumerComponentToVersion ( consumerComponent ) ;
114
143
if ( previousVersion ) {
@@ -126,10 +155,16 @@ export class StashMain {
126
155
}
127
156
128
157
static slots = [ ] ;
129
- static dependencies = [ CLIAspect , WorkspaceAspect , CheckoutAspect , SnappingAspect ] ;
158
+ static dependencies = [ CLIAspect , WorkspaceAspect , CheckoutAspect , SnappingAspect , RemoveAspect ] ;
130
159
static runtime = MainRuntime ;
131
- static async provider ( [ cli , workspace , checkout , snapping ] : [ CLIMain , Workspace , CheckoutMain , SnappingMain ] ) {
132
- const stashMain = new StashMain ( workspace , checkout , snapping ) ;
160
+ static async provider ( [ cli , workspace , checkout , snapping , remove ] : [
161
+ CLIMain ,
162
+ Workspace ,
163
+ CheckoutMain ,
164
+ SnappingMain ,
165
+ RemoveMain ,
166
+ ] ) {
167
+ const stashMain = new StashMain ( workspace , checkout , snapping , remove ) ;
133
168
const stashCmd = new StashCmd ( stashMain ) ;
134
169
stashCmd . commands = [ new StashSaveCmd ( stashMain ) , new StashLoadCmd ( stashMain ) , new StashListCmd ( stashMain ) ] ;
135
170
cli . register ( stashCmd ) ;
0 commit comments