1
1
const path = require ( 'path' )
2
- const fs = require ( 'fs-extra' )
2
+ const fse = require ( 'fs-extra' )
3
+ const fs = require ( 'fs' )
4
+ const fsp = require ( 'fs/promises' )
3
5
const exec = require ( '../util/exec' )
4
6
const { remove } = require ( 'fs-extra' )
5
7
const logger = require ( '../util/logger' )
@@ -54,11 +56,23 @@ module.exports = (actionInfo) => {
54
56
} ,
55
57
async linkPackages ( { repoDir, nextSwcVersion } ) {
56
58
const pkgPaths = new Map ( )
59
+
60
+ /**
61
+ * @typedef {Object } PkgData
62
+ * @property {string } pkgDataPath Where the package.json file is located
63
+ * @property {string } pkg The folder name of the package
64
+ * @property {string } pkgPath The path to the package folder
65
+ * @property {any } pkgData The content of package.json
66
+ * @property {string } packedPkgPath The npm pack output .tgz file path
67
+ */
68
+
69
+ /** @type {Map<string, PkgData> } */
57
70
const pkgDatas = new Map ( )
71
+
58
72
let pkgs
59
73
60
74
try {
61
- pkgs = await fs . readdir ( path . join ( repoDir , 'packages' ) )
75
+ pkgs = await fsp . readdir ( path . join ( repoDir , 'packages' ) )
62
76
} catch ( err ) {
63
77
if ( err . code === 'ENOENT' ) {
64
78
require ( 'console' ) . log ( 'no packages to link' )
@@ -67,69 +81,81 @@ module.exports = (actionInfo) => {
67
81
throw err
68
82
}
69
83
70
- for ( const pkg of pkgs ) {
71
- const pkgPath = path . join ( repoDir , 'packages' , pkg )
72
- const packedPkgPath = path . join ( pkgPath , `${ pkg } -packed.tgz` )
73
-
74
- const pkgDataPath = path . join ( pkgPath , 'package.json' )
75
- if ( ! fs . existsSync ( pkgDataPath ) ) {
76
- require ( 'console' ) . log ( `Skipping ${ pkgDataPath } ` )
77
- continue
78
- }
79
- const pkgData = require ( pkgDataPath )
80
- const { name } = pkgData
81
-
82
- pkgDatas . set ( name , {
83
- pkgDataPath,
84
- pkg,
85
- pkgPath,
86
- pkgData,
87
- packedPkgPath,
84
+ await Promise . all (
85
+ pkgs . map ( async ( pkg ) => {
86
+ const pkgPath = path . join ( repoDir , 'packages' , pkg )
87
+ const packedPkgPath = path . join ( pkgPath , `${ pkg } -packed.tgz` )
88
+
89
+ const pkgDataPath = path . join ( pkgPath , 'package.json' )
90
+ if ( fs . existsSync ( pkgDataPath ) ) {
91
+ const pkgData = JSON . parse ( await fsp . readFile ( pkgDataPath ) )
92
+ const { name } = pkgData
93
+
94
+ pkgDatas . set ( name , {
95
+ pkgDataPath,
96
+ pkg,
97
+ pkgPath,
98
+ pkgData,
99
+ packedPkgPath,
100
+ } )
101
+ pkgPaths . set ( name , packedPkgPath )
102
+ } else {
103
+ require ( 'console' ) . log ( `Skipping ${ pkgDataPath } ` )
104
+ }
88
105
} )
89
- pkgPaths . set ( name , packedPkgPath )
90
- }
91
-
92
- for ( const pkg of pkgDatas . keys ( ) ) {
93
- const { pkgDataPath, pkgData } = pkgDatas . get ( pkg )
106
+ )
94
107
95
- for ( const pkg of pkgDatas . keys ( ) ) {
96
- const { packedPkgPath } = pkgDatas . get ( pkg )
108
+ for ( const [
109
+ pkg ,
110
+ { pkgDataPath, pkgData, pkgPath } ,
111
+ ] of pkgDatas . entries ( ) ) {
112
+ // update the current package dependencies to point to packed tgz path
113
+ for ( const [ pkg , { packedPkgPath } ] of pkgDatas . entries ( ) ) {
97
114
if ( ! pkgData . dependencies || ! pkgData . dependencies [ pkg ] ) continue
98
115
pkgData . dependencies [ pkg ] = packedPkgPath
99
116
}
100
117
101
118
// make sure native binaries are included in local linking
102
119
if ( pkg === '@next/swc' ) {
103
- if ( ! pkgData . files ) {
104
- pkgData . files = [ ]
105
- }
120
+ pkgData . files ||= [ ]
121
+
106
122
pkgData . files . push ( 'native' )
107
- require ( 'console' ) . log (
108
- 'using swc binaries: ' ,
109
- await exec ( `ls ${ path . join ( path . dirname ( pkgDataPath ) , 'native' ) } ` )
110
- )
111
- }
112
123
113
- if ( pkg === 'next' ) {
124
+ try {
125
+ const swcBinariesDirContents = await fsp . readdir (
126
+ path . join ( pkgPath , 'native' )
127
+ )
128
+ require ( 'console' ) . log (
129
+ 'using swc binaries: ' ,
130
+ swcBinariesDirContents . join ( ', ' )
131
+ )
132
+ } catch ( err ) {
133
+ if ( err . code === 'ENOENT' ) {
134
+ require ( 'console' ) . log ( 'swc binaries dir is missing!' )
135
+ }
136
+ throw err
137
+ }
138
+ } else if ( pkg === 'next' ) {
139
+ const nextSwcPkg = pkgDatas . get ( '@next/swc' )
140
+
114
141
console . log ( 'using swc dep' , {
115
142
nextSwcVersion,
116
- nextSwcPkg : pkgDatas . get ( '@next/swc' ) ,
143
+ nextSwcPkg,
117
144
} )
118
145
if ( nextSwcVersion ) {
119
146
Object . assign ( pkgData . dependencies , {
120
147
'@next/swc-linux-x64-gnu' : nextSwcVersion ,
121
148
} )
122
149
} else {
123
- if ( pkgDatas . get ( '@next/swc' ) ) {
124
- pkgData . dependencies [ '@next/swc' ] =
125
- pkgDatas . get ( '@next/swc' ) . packedPkgPath
150
+ if ( nextSwcPkg ) {
151
+ pkgData . dependencies [ '@next/swc' ] = nextSwcPkg . packedPkgPath
126
152
} else {
127
153
pkgData . files . push ( 'native' )
128
154
}
129
155
}
130
156
}
131
157
132
- await fs . writeFile (
158
+ await fsp . writeFile (
133
159
pkgDataPath ,
134
160
JSON . stringify ( pkgData , null , 2 ) ,
135
161
'utf8'
@@ -139,41 +165,47 @@ module.exports = (actionInfo) => {
139
165
// wait to pack packages until after dependency paths have been updated
140
166
// to the correct versions
141
167
await Promise . all (
142
- Array . from ( pkgDatas . keys ( ) ) . map ( async ( pkgName ) => {
143
- const { pkgPath, packedPkgPath } = pkgDatas . get ( pkgName )
144
-
145
- let cleanup = null
146
-
147
- if ( pkgName === '@next/swc' ) {
148
- // next-swc uses a gitignore to prevent the committing of native builds but it doesn't
149
- // use files in package.json because it publishes to individual packages based on architecture.
150
- // When we used yarn to pack these packages the gitignore was ignored so the native builds were packed
151
- // however npm does respect gitignore when packing so we need to remove it in this specific case
152
- // to ensure the native builds are packed for use in gh actions and related scripts
153
- await fs . rename (
154
- path . join ( pkgPath , 'native/.gitignore' ) ,
155
- path . join ( pkgPath , 'disabled-native-gitignore' )
156
- )
157
- cleanup = async ( ) => {
158
- await fs . rename (
159
- path . join ( pkgPath , 'disabled-native-gitignore' ) ,
160
- path . join ( pkgPath , ' native/. gitignore')
168
+ Array . from ( pkgDatas . entries ( ) ) . map (
169
+ async ( [ pkgName , { pkgPath, packedPkgPath } ] ) => {
170
+ /** @type { null | () => Promise<void> } */
171
+ let cleanup = null
172
+
173
+ if ( pkgName === '@next/swc' ) {
174
+ // next-swc uses a gitignore to prevent the committing of native builds but it doesn't
175
+ // use files in package.json because it publishes to individual packages based on architecture.
176
+ // When we used yarn to pack these packages the gitignore was ignored so the native builds were packed
177
+ // however npm does respect gitignore when packing so we need to remove it in this specific case
178
+ // to ensure the native builds are packed for use in gh actions and related scripts
179
+
180
+ const nativeGitignorePath = path . join (
181
+ pkgPath ,
182
+ 'native/.gitignore'
183
+ )
184
+ const renamedGitignorePath = path . join (
185
+ pkgPath ,
186
+ 'disabled- native- gitignore'
161
187
)
188
+
189
+ await fsp . rename ( nativeGitignorePath , renamedGitignorePath )
190
+ cleanup = async ( ) => {
191
+ await fsp . rename ( renamedGitignorePath , nativeGitignorePath )
192
+ }
162
193
}
163
- }
164
194
165
- const { stdout } = await execa ( 'npm' , [ 'pack' ] , {
166
- cwd : pkgPath ,
167
- env : {
168
- ...process . env ,
169
- COREPACK_ENABLE_STRICT : '0' ,
170
- } ,
171
- } )
172
- await fs . rename ( path . resolve ( pkgPath , stdout . trim ( ) ) , packedPkgPath )
173
- if ( cleanup ) {
174
- await cleanup ( )
195
+ const { stdout } = await execa ( 'pnpm' , [ 'pack' ] , {
196
+ cwd : pkgPath ,
197
+ env : {
198
+ ...process . env ,
199
+ COREPACK_ENABLE_STRICT : '0' ,
200
+ } ,
201
+ } )
202
+
203
+ return Promise . all ( [
204
+ fsp . rename ( path . resolve ( pkgPath , stdout . trim ( ) ) , packedPkgPath ) ,
205
+ cleanup ?. ( ) ,
206
+ ] )
175
207
}
176
- } )
208
+ )
177
209
)
178
210
return pkgPaths
179
211
} ,
0 commit comments