@@ -124,35 +124,35 @@ const SyncSpaces = {
124124
125125 for ( var i = 0 ; i < sourceFolders . length ; i ++ ) {
126126 const folder = sourceFolders [ i ]
127- const folderId = folder . id
128- delete folder . id
129- delete folder . created_at
130-
131- if ( folder . parent_id ) {
132- // Parent child resolving
133- if ( ! syncedFolders [ folderId ] ) {
134- const folderSlug = folder . full_slug . split ( '/' )
135- const parentFolderSlug = folderSlug . splice ( 0 , folderSlug . length - 1 ) . join ( '/' )
136-
137- const existingFolders = await this . client . get ( `spaces/${ this . targetSpaceId } /stories` , {
138- with_slug : parentFolderSlug
139- } )
140-
141- if ( existingFolders . data . stories . length ) {
142- folder . parent_id = existingFolders . data . stories [ 0 ] . id
143- } else {
144- folder . parent_id = 0
145- }
146- } else {
147- folder . parent_id = syncedFolders [ folderId ]
148- }
149- }
150127
151128 try {
152- const folderResult = await this . client . get ( 'spaces/' + this . sourceSpaceId + '/stories/' + folderId )
129+ const folderResult = await this . client . get ( 'spaces/' + this . sourceSpaceId + '/stories/' + folder . id )
153130 const sourceFolder = folderResult . data . story
154131 const existingFolder = await this . client . get ( 'spaces/' + this . targetSpaceId + '/stories' , { with_slug : folder . full_slug } )
155132 const folderData = await this . getStoryWithTranslatedSlugs ( sourceFolder , existingFolder . data . stories ? existingFolder . data . stories [ 0 ] : null )
133+ delete folderData . id
134+ delete folderData . created_at
135+
136+ if ( folder . parent_id ) {
137+ // Parent child resolving
138+ if ( ! syncedFolders [ folder . id ] ) {
139+ const folderSlug = folder . full_slug . split ( '/' )
140+ const parentFolderSlug = folderSlug . splice ( 0 , folderSlug . length - 1 ) . join ( '/' )
141+
142+ const parentFolders = await this . client . get ( `spaces/${ this . targetSpaceId } /stories` , {
143+ with_slug : parentFolderSlug
144+ } )
145+
146+ if ( parentFolders . data . stories . length ) {
147+ folderData . parent_id = parentFolders . data . stories [ 0 ] . id
148+ } else {
149+ folderData . parent_id = 0
150+ }
151+ } else {
152+ folderData . parent_id = syncedFolders [ folder . id ]
153+ }
154+ }
155+
156156 const payload = {
157157 story : folderData ,
158158 force_update : '1'
@@ -162,16 +162,16 @@ const SyncSpaces = {
162162 if ( existingFolder . data . stories . length === 1 ) {
163163 console . log ( `Folder ${ folder . name } already exists` )
164164 createdFolder = await this . client . put ( 'spaces/' + this . targetSpaceId + '/stories/' + existingFolder . data . stories [ 0 ] . id , payload )
165- console . log ( chalk . green ( '✓' ) + `Folder ${ folder . name } updated` )
165+ console . log ( chalk . green ( '✓' ) + ` Folder ${ folder . name } updated` )
166166 } else {
167167 createdFolder = await this . client . post ( 'spaces/' + this . targetSpaceId + '/stories' , payload )
168- console . log ( chalk . green ( '✓' ) + `Folder ${ folder . name } created` )
168+ console . log ( chalk . green ( '✓' ) + ` Folder ${ folder . name } created` )
169169 }
170170 if ( createdFolder . data . story . uuid !== folder . uuid ) {
171171 await this . client . put ( 'spaces/' + this . targetSpaceId + '/stories/' + createdFolder . data . story . id + '/update_uuid' , { uuid : folder . uuid } )
172172 }
173173
174- syncedFolders [ folderId ] = createdFolder . data . story . id
174+ syncedFolders [ folder . id ] = createdFolder . data . story . id
175175 } catch ( e ) {
176176 console . error (
177177 chalk . red ( 'X' ) + ` Folder ${ folder . name } Sync failed: ${ e . message } `
0 commit comments