1- // This script retrieves the pathPrefix from the gatsby- config.js  file. 
1+ // This script retrieves the pathPrefix from the config.md  file and validates it against the pathPrefix from devsite-paths.json . 
22// It serves as an example for how to set up external javascript functions 
33// outside workflow .yml files when they get too big or complex to keep them inline. 
44
55// Documentation for the actions/github-script: 
66// https://github.com/actions/github-script#run-a-separate-file 
77
8- module . exports  =  async  ( {  core } )  =>  { 
9-   const  {  pathPrefix }  =  await  require ( '../../gatsby-config.js' ) ; 
8+ const  CONFIG_PATH  =  `./src/pages/config.md` ; 
9+ const  DEVSITE_STAGE_HOST  =  `https://main--adp-devsite-stage--adobedocs.aem.page` ; 
10+ const  DEVSITE_PROD_HOST  =  `https://main--adp-devsite--adobedocs.aem.live` ; 
11+ const  DEVSITE_PATHNAME  =  `/franklin_assets/devsitepaths.json` ; 
1012
11-   if  ( ! pathPrefix )  { 
12-     core . setFailed ( 
13-       `The pathPrefix in the site's gatsby-config.js file is missing. 
13+ module . exports  =  async  ( {  core,  isStage,  isProd } )  =>  { 
14+     const  fs  =  await  require ( 'fs' ) ; 
15+     if  ( ! fs . existsSync ( CONFIG_PATH ) )  { 
16+       core . setFailed ( 
17+         `The site's config.md file is missing. 
18+    
19+         To fix this, either create one in ./src/pages, or auto-generate one from the site's gatsby-config.md file by building navigation file.` 
20+       ) ; 
21+       return ; 
22+     } 
1423
15-       To fix this, open your gatsby-config.js file, and add it to the config object: 
24+     const  string  =  fs . readFileSync ( CONFIG_PATH ) . toString ( )  ??  "" ; 
25+     const  lines  =  string . split ( '\n' ) ; 
1626
17-       module.exports = { 
18-         pathPrefix: "/commerce/frontend-core/", 
19-         ... 
20-       }` 
21-     ) ; 
22-   }  else  if  ( pathPrefix  ===  '/' )  { 
23-     core . setFailed ( 
24-       `The pathPrefix in the site's gatsby-config.js file is set to "/". This is not allowed. 
27+     // find the pathPrefix key 
28+     const  keyIndex  =  lines . findIndex ( line  =>  line . includes ( "pathPrefix:" ) ) ; 
2529
26-       To fix this, change the pathPrefix to include a name that starts and ends with "/": 
30+     if  ( keyIndex  <  0 )  { 
31+       core . setFailed ( 
32+         `The pathPrefix in the site's config.md file is missing. 
2733
28-       pathPrefix: "/commerce/frontend - core/" 
34+         To fix this, open your config.md file, and add it to the config object: 
35+ 
36+         - pathPrefix: 
37+         ...` 
38+       ) ; 
39+       return ; 
40+     } 
2941
30-       This name identifies the site within the developer.adobe.com domain: 
31-       https://developer.adobe.com/document-services/<PATH_TO_FILES>. 
32-       ` 
33-     ) ; 
34-   }  else  { 
35-     if  ( ! pathPrefix . startsWith ( '/' )  ||  ! pathPrefix . endsWith ( '/' ) )  { 
42+     // find the pathPrefix value 
43+     const  line  =  lines . slice ( keyIndex  +  1 ) ?. find ( line  =>  line . trimStart ( ) . startsWith ( "-" ) )  ??  "" ; 
44+   
45+     // remove whitespace at start, remove dash (i.e. first non-whitespace character), and remove whitespace at start and end 
46+     const  pathPrefix  =  line . trimStart ( ) . substring ( 1 ) . trim ( ) ; 
47+ 
48+     if  ( ! pathPrefix )  { 
3649      core . setFailed ( 
37-         `The pathPrefix in the site's gatsby- config.js  file does not start or end with "/" . 
50+         `The pathPrefix in the site's config.md  file is missing . 
3851
39-         To fix this, change the pathPrefix to include a name that starts and ends with "/". 
40-         For example: "/document-services/" or "/commerce/cloud-tools/". 
52+         To fix this, open your config.md file, and add it to the config object: 
4153
42-         This is required by convention because of the way we construct site URLs.  
43-         For example: https://developer.adobe.com + /document-services/ + path/to/files/.  
44-         ` 
54+         - pathPrefix:  
55+             - /commerce/frontend-core/  
56+         ... ` 
4557      ) ; 
58+     }  else  if  ( pathPrefix  ===  '/' )  { 
59+         core . setFailed ( 
60+             `The pathPrefix in the site's config.md file is set to "/". This is not allowed. 
61+ 
62+             To fix this, change the pathPrefix to include a name that starts and ends with "/". 
63+ 
64+             For example: "/commerce/frontend - core/" 
65+ 
66+             This name identifies the site within the developer.adobe.com domain: 
67+             https://developer.adobe.com/document-services/<PATH_TO_FILES>. 
68+             ` 
69+         ) ; 
70+     }  else  if  ( ! pathPrefix . startsWith ( '/' )  ||  ! pathPrefix . endsWith ( '/' ) )  { 
71+         core . setFailed ( 
72+             `The pathPrefix in the site's config.md file does not start or end with "/". 
73+ 
74+             pathPrefix: "${ pathPrefix }  " 
75+ 
76+             To fix this, change the pathPrefix to include a name that starts and ends with "/". 
77+             For example: "/document-services/" or "/commerce/cloud-tools/". 
78+ 
79+             This is required by convention because of the way we construct site URLs. 
80+             For example: https://developer.adobe.com + /document-services/ + path/to/files/. 
81+             ` 
82+         ) ; 
83+     } 
84+ 
85+     // TODO: devsitepaths pathPrefix currently do not have a trailing slash 
86+     // will need to refactor all path prefix listings to include them 
87+     // but for now checked with a popped trailing slash 
88+ 
89+     const  poppedPathPrefix  =  pathPrefix . substring ( 0 ,  pathPrefix . length - 1 ) ; 
90+     // must convert values to boolean from string 
91+     if ( isStage . toLowerCase ( )  ===  'true' )  { 
92+       const  stageEntries  =  await  ( await  fetch ( `${ DEVSITE_STAGE_HOST } ${ DEVSITE_PATHNAME }  ` ) ) . json ( ) ; 
93+       const  stageEntry  =  stageEntries ?. data ?. find ( entry  =>  entry . pathPrefix  ===  poppedPathPrefix ) ; 
94+ 
95+       if ( ! stageEntry )  { 
96+         core . setFailed ( 
97+           `The pathPrefix in the site's config.md file was not found in the STAGE gdrive's devsitepaths.json. 
98+ 
99+           pathPrefix from config.md: "${ pathPrefix }  " 
100+           devsitepath.json location: "${ DEVSITE_STAGE_HOST } ${ DEVSITE_PATHNAME }  " 
101+ 
102+           To fix this, make sure the pathPrefix listed in the config.md is in the devsitepath.json location. 
103+           ` 
104+         ) ; 
105+       } 
46106    } 
47-   } 
48-   core . setOutput ( 'path_prefix' ,  pathPrefix ) ; 
49- } ; 
107+ 
108+     // must convert values to boolean from string 
109+     if ( isProd . toLowerCase ( )  ===  'true' )  { 
110+       const  prodEntries  =  await  ( await  fetch ( `${ DEVSITE_PROD_HOST } ${ DEVSITE_PATHNAME }  ` ) ) . json ( ) ; 
111+       const  prodEntry  =  prodEntries ?. data ?. find ( entry  =>  entry . pathPrefix  ===  poppedPathPrefix ) ; 
112+ 
113+       if ( ! prodEntry )  { 
114+         core . setFailed ( 
115+           `The pathPrefix in the site's config.md file was not found in the PROD gdrive's devsitepaths.json. 
116+ 
117+           pathPrefix from config.md: "${ pathPrefix }  " 
118+           devsitepath.json location: "${ DEVSITE_PROD_HOST } ${ DEVSITE_PATHNAME }  " 
119+ 
120+           To fix this, make sure the pathPrefix listed in the config.md is in the devsitepath.json location. 
121+           ` 
122+         ) ; 
123+       } 
124+     } 
125+ 
126+     core . setOutput ( 'path_prefix' ,  poppedPathPrefix ) ; 
127+ } 
0 commit comments