@@ -18,8 +18,6 @@ export interface PushParams {
18
18
projectId : string ;
19
19
/** The branch ID to upload to. */
20
20
branchId : string ;
21
- /** The version to compute the file state changes against. Defaults to latest version. */
22
- latestVersion ?: number ;
23
21
/** The current file state. If not provided, it will be computed. */
24
22
fileState ?: ItemStatusManager ;
25
23
/** A list of gitignore rules. */
@@ -40,20 +38,19 @@ export async function push(params: PushParams): Promise<ItemStatusManager> {
40
38
targetDir,
41
39
projectId,
42
40
branchId,
43
- latestVersion,
44
41
fileState,
45
42
gitignoreRules,
46
43
dryRun = false ,
47
44
} = params ;
48
- latestVersion = latestVersion ?? await getLatestVersion ( projectId , branchId ) ;
45
+ const initialVersion = await getLatestVersion ( projectId , branchId ) ;
49
46
50
47
// Use provided status, or retrieve the status
51
48
if ( ! fileState || fileState . isEmpty ( ) ) {
52
49
fileState = await status ( {
53
50
targetDir,
54
51
projectId,
55
52
branchId,
56
- version : latestVersion ,
53
+ version : initialVersion ,
57
54
gitignoreRules,
58
55
} ) ;
59
56
}
@@ -64,7 +61,7 @@ export async function push(params: PushParams): Promise<ItemStatusManager> {
64
61
const existingItems = await listProjectItems (
65
62
projectId ,
66
63
branchId ,
67
- latestVersion ,
64
+ initialVersion ,
68
65
) ;
69
66
70
67
// Create a set of existing paths that already exist
@@ -76,30 +73,52 @@ export async function push(params: PushParams): Promise<ItemStatusManager> {
76
73
] ) ;
77
74
78
75
// Create all necessary directories first
79
- const createFilesPromise = createRequiredDirectories (
76
+ const versionAfterDirectories = await createRequiredDirectories (
80
77
projectId ,
81
78
branchId ,
82
79
fileState ,
83
80
existingDirs ,
84
- )
85
- . then ( ( ) =>
86
- Promise . all (
87
- fileState . created
88
- . filter ( ( f ) => f . type !== "directory" ) // Already created directories
89
- . map ( async ( file ) => {
90
- // Upload the file
91
- await sdk . projects . files . create (
92
- projectId ,
93
- {
94
- path : file . path ,
95
- content : await Deno . readTextFile ( join ( targetDir , file . path ) ) ,
96
- branch_id : branchId ,
97
- type : file . type as Exclude < ProjectItemType , "directory" > ,
98
- } ,
99
- ) ;
100
- } ) ,
101
- )
102
- ) ;
81
+ ) + initialVersion ;
82
+
83
+ // Rename files that were renamed locally
84
+ const renamePromises = fileState . renamed
85
+ . filter ( ( file ) => file . type !== "directory" )
86
+ . map ( async ( file ) => {
87
+ // We created the parent directory already, but not the file, so we must
88
+ // query the ID of the parent directory to set it as the new parent of the
89
+ // item
90
+ const parent = await getProjectItem (
91
+ projectId ,
92
+ branchId ,
93
+ versionAfterDirectories ,
94
+ dirname ( file . path ) ,
95
+ ) ;
96
+
97
+ await sdk . projects . files . update ( projectId , {
98
+ branch_id : branchId ,
99
+ name : basename ( file . path ) ,
100
+ type : file . type as ProjectFileType ,
101
+ content : file . content ,
102
+ parent_id : parent ?. id ,
103
+ path : file . oldPath ,
104
+ } ) ;
105
+ } ) ;
106
+
107
+ // Create all new files that were created (we already handled directories)
108
+ const createdPromises = fileState . created
109
+ . filter ( ( f ) => f . type !== "directory" ) // Already created directories
110
+ . map ( async ( file ) => {
111
+ // Upload the file
112
+ await sdk . projects . files . create (
113
+ projectId ,
114
+ {
115
+ path : file . path ,
116
+ content : await Deno . readTextFile ( join ( targetDir , file . path ) ) ,
117
+ branch_id : branchId ,
118
+ type : file . type as Exclude < ProjectItemType , "directory" > ,
119
+ } ,
120
+ ) ;
121
+ } ) ;
103
122
104
123
// Upload files that were modified locally
105
124
const modifiedPromises = fileState . modified
@@ -117,25 +136,6 @@ export async function push(params: PushParams): Promise<ItemStatusManager> {
117
136
) ;
118
137
} ) ;
119
138
120
- // Rename files that were renamed locally
121
- const renamePromises = fileState . renamed
122
- . filter ( ( file ) => file . type !== "directory" )
123
- . map ( async ( file ) => {
124
- await sdk . projects . files . update ( projectId , {
125
- branch_id : branchId ,
126
- name : basename ( file . path ) ,
127
- type : file . type as ProjectFileType ,
128
- content : file . content ,
129
- parent_id : ( await getProjectItem (
130
- projectId ,
131
- branchId ,
132
- latestVersion ,
133
- dirname ( file . path ) ,
134
- ) ) ?. id ,
135
- path : file . oldPath ,
136
- } ) ;
137
- } ) ;
138
-
139
139
// Delete files that exist on the server but not locally
140
140
const deletedPromises = fileState . deleted . map ( async ( file ) => {
141
141
await sdk . projects . files . delete ( projectId , {
@@ -150,7 +150,7 @@ export async function push(params: PushParams): Promise<ItemStatusManager> {
150
150
...modifiedPromises ,
151
151
...deletedPromises ,
152
152
...renamePromises ,
153
- createFilesPromise ,
153
+ ... createdPromises ,
154
154
] ) ;
155
155
156
156
return fileState ;
@@ -161,7 +161,7 @@ async function createRequiredDirectories(
161
161
branchId : string ,
162
162
fileState : ItemStatusManager ,
163
163
existingDirs : Set < string > ,
164
- ) : Promise < void > {
164
+ ) : Promise < number > {
165
165
// Get directories that need to be created
166
166
const dirsToCreate = fileState . created
167
167
. filter ( ( f ) => f . type === "directory" )
@@ -193,12 +193,16 @@ async function createRequiredDirectories(
193
193
} ) ;
194
194
195
195
// Create all necessary directories
196
+ let createdCount = 0 ;
196
197
for ( const path of sortedDirsToCreate ) {
197
198
await sdk . projects . files . create (
198
199
projectId ,
199
200
{ path, type : "directory" , branch_id : branchId } ,
200
201
) ;
201
202
// Add to existing dirs set after creation
202
203
existingDirs . add ( path ) ;
204
+ createdCount ++ ;
203
205
}
206
+
207
+ return createdCount ;
204
208
}
0 commit comments