1
+ /* eslint-disable camelcase */
2
+ const fs = require ( 'fs' ) ;
3
+ const path = require ( 'path' ) ;
4
+ const { execSync } = require ( 'child_process' ) ;
5
+ const findMonorepoRoot = require ( '../utils/find-monorepo-root.js' ) ;
6
+
7
+ /**
8
+ * Identifies packages that need to be published to the CDN
9
+ * @param {Array } publishedPackages - Array of published packages from changesets
10
+ * @returns {Array } - Array of packages that need CDN publishing
11
+ */
12
+ async function identifyCdnPackages ( publishedPackages ) {
13
+ // Get workspace info to locate package directories
14
+ const monorepoRoot = findMonorepoRoot ( ) ;
15
+ const workspaceOutput = execSync ( 'yarn workspaces list --json' , { cwd : monorepoRoot } ) . toString ( ) ;
16
+ const workspaces = workspaceOutput . trim ( ) . split ( '\n' ) . map ( line => JSON . parse ( line ) ) ;
17
+
18
+ // Find packages that need CDN publishing
19
+ const cdnPackages = [ ] ;
20
+
21
+ for ( const pkg of publishedPackages ) {
22
+ // Find workspace location for this package
23
+ const workspace = workspaces . find ( ws => ws . name === pkg . name ) ;
24
+ if ( ! workspace ) continue ;
25
+
26
+ // Read package.json to check for CDN configuration
27
+ try {
28
+ const packageJsonPath = path . join ( monorepoRoot , workspace . location , 'package.json' ) ;
29
+ const packageJson = JSON . parse ( fs . readFileSync ( packageJsonPath , 'utf8' ) ) ;
30
+
31
+ // Validate required CDN configuration
32
+ if ( packageJson . pieMetadata && packageJson . pieMetadata . cdnPublish === true ) {
33
+ // Validate all required CDN properties are present
34
+ if ( ! packageJson . pieMetadata . cdnSourceFolder ) {
35
+ console . error ( `Missing required cdnSourceFolder for ${ pkg . name } . Skipping CDN publishing.` ) ;
36
+ continue ;
37
+ }
38
+
39
+ if ( ! packageJson . pieMetadata . cdnContentType ) {
40
+ console . error ( `Missing required cdnContentType for ${ pkg . name } . Skipping CDN publishing.` ) ;
41
+ continue ;
42
+ }
43
+
44
+ cdnPackages . push ( {
45
+ name : pkg . name ,
46
+ version : pkg . version ,
47
+ location : path . join ( monorepoRoot , workspace . location ) ,
48
+ cdnSourceFolder : packageJson . pieMetadata . cdnSourceFolder ,
49
+ cdnContentType : packageJson . pieMetadata . cdnContentType
50
+ } ) ;
51
+ }
52
+ } catch ( error ) {
53
+ console . error ( `Error processing ${ pkg . name } :` , error ) ;
54
+ }
55
+ }
56
+
57
+ return cdnPackages ;
58
+ }
59
+
60
+ /**
61
+ * Publishes packages to the CDN
62
+ * @param {Array } cdnPackages - Array of packages to publish to the CDN
63
+ */
64
+ async function publishToCdn ( cdnPackages ) {
65
+ console . log ( 'Publishing packages to CDN:' , cdnPackages ) ;
66
+
67
+ for ( const pkg of cdnPackages ) {
68
+ try {
69
+ // Get package name
70
+ const packageName = pkg . name . replace ( '@justeattakeaway/' , '' ) ;
71
+
72
+ // Build the path to the source folder
73
+ const sourcePath = path . join ( pkg . location , pkg . cdnSourceFolder ) ;
74
+
75
+ // Check if the source folder exists
76
+ if ( ! fs . existsSync ( sourcePath ) ) {
77
+ console . log ( `Source directory '${ pkg . cdnSourceFolder } ' not found for ${ pkg . name } , skipping upload` ) ;
78
+ continue ;
79
+ }
80
+
81
+ console . log ( `Uploading ${ pkg . name } to S3 from ${ sourcePath } ...` ) ;
82
+ execSync (
83
+ `aws s3 sync ${ sourcePath } / s3://$PIE_CDN_BUCKET_NAME/${ packageName } /v${ pkg . version } / --region $AWS_REGION --content-type "${ pkg . cdnContentType } "` ,
84
+ { stdio : 'inherit' }
85
+ ) ;
86
+
87
+ console . log ( `Successfully published ${ pkg . name } @${ pkg . version } to CDN` ) ;
88
+ } catch ( error ) {
89
+ console . error ( `Error processing ${ pkg . name } :` , error ) ;
90
+ throw error ;
91
+ }
92
+ }
93
+ }
94
+
95
+ /**
96
+ * @param {Object } options - Options object
97
+ * @param {Array } options.publishedPackages - Array of published packages from changesets
98
+ */
99
+ module . exports = async ( { publishedPackages } ) => {
100
+ try {
101
+ // Identify packages for CDN publishing
102
+ const cdnPackages = await identifyCdnPackages ( publishedPackages ) ;
103
+
104
+ if ( cdnPackages . length === 0 ) {
105
+ console . log ( 'No packages need CDN publishing' ) ;
106
+ return ;
107
+ }
108
+
109
+ await publishToCdn ( cdnPackages ) ;
110
+
111
+ } catch ( error ) {
112
+ console . error ( 'Error publishing to CDN:' , error ) ;
113
+ throw error ;
114
+ }
115
+ } ;
0 commit comments