@@ -15,6 +15,7 @@ export function calculateTime(startTime) {
15
15
}
16
16
17
17
async function saveAllToDa ( url , blob ) {
18
+ console . log ( "Starting DA import" )
18
19
const { destPath, editPath, route } = url ;
19
20
20
21
url . daHref = `https://da.live${ route } #/${ toOrg } /${ toRepo } ${ editPath } ` ;
@@ -32,6 +33,14 @@ async function saveAllToDa(url, blob) {
32
33
}
33
34
}
34
35
36
+ async function previewOrPublish ( { path, action} ) {
37
+ const previewUrl = `https://admin.hlx.page/${ action } /${ toOrg } /${ toRepo } /main/${ path } ` ;
38
+ const opts = { method : 'POST' } ;
39
+ const resp = await fetch ( previewUrl , opts ) ;
40
+ if ( ! resp . ok ) throw new Error ( `Failed to post to preview: ${ resp . statusText } ` )
41
+ console . log ( `Posted to ${ action } successfully ${ action } /${ toOrg } /${ toRepo } /main/${ path } ` ) ;
42
+ }
43
+
35
44
async function importUrl ( url ) {
36
45
const [ fromRepo , fromOrg ] = url . hostname . split ( '.' ) [ 0 ] . split ( '--' ) . slice ( 1 ) . slice ( - 2 ) ;
37
46
if ( ! ( fromRepo || fromOrg ) ) {
@@ -65,7 +74,8 @@ async function importUrl(url) {
65
74
66
75
try {
67
76
const resp = await fetch ( `${ url . origin } ${ srcPath } ` ) ;
68
- console . log ( "fetched resource from AEM at: " , `${ url . origin } ${ srcPath } ` )
77
+
78
+ console . log ( "fetched resource from AEM at:" , `${ url . origin } ${ srcPath } ` )
69
79
if ( resp . redirected && ! ( srcPath . endsWith ( '.mp4' ) || srcPath . endsWith ( '.png' ) || srcPath . endsWith ( '.jpg' ) ) ) {
70
80
url . status = 'redir' ;
71
81
throw new Error ( 'redir' ) ;
@@ -81,13 +91,13 @@ async function importUrl(url) {
81
91
content = new Blob ( [ html ] , { type : 'text/html' } ) ;
82
92
}
83
93
url . status = await saveAllToDa ( url , content ) ;
84
- console . log ( "imported resource to DA " + toOrg + "/" + toRepo + " | destination: " + url . destPath )
85
-
86
- console . log ( "TODO - preview and publish." )
94
+ console . log ( "imported resource to DA " + url . daHref ) ;
95
+ await previewOrPublish ( { path : pathname , action : 'preview' } ) ;
96
+ await previewOrPublish ( { path : pathname , action : 'live' } ) ;
97
+ console . log ( `Resource: https://main--${ toRepo } --${ toOrg } .aem.live${ url . pathname } ` ) ;
87
98
} catch ( e ) {
88
- console . log ( e )
99
+ console . log ( "Failed to import resource to DA " + toOrg + "/" + toRepo + " | destination: " + url . pathname , + " | error: " + e . message ) ;
89
100
if ( ! url . status ) url . status = 'error' ;
90
- // Do nothing
91
101
}
92
102
}
93
103
0 commit comments