@@ -59,10 +59,11 @@ console.log(
5959 `📦 Downloading Skia prebuilt binaries for ${ releaseTag } `
6060) ;
6161
62- const runCommand = ( command , args ) => {
62+ const runCommand = ( command , args , options = { } ) => {
6363 return new Promise ( ( resolve , reject ) => {
6464 const child = spawn ( command , args , {
6565 stdio : [ "ignore" , "inherit" , "inherit" ] ,
66+ ...options ,
6667 } ) ;
6768 child . on ( "error" , ( error ) => {
6869 reject ( error ) ;
@@ -77,6 +78,75 @@ const runCommand = (command, args) => {
7778 } ) ;
7879} ;
7980
81+ const runCommandWithOutput = ( command , args , options = { } ) => {
82+ return new Promise ( ( resolve , reject ) => {
83+ const child = spawn ( command , args , {
84+ stdio : [ "ignore" , "pipe" , "pipe" ] ,
85+ ...options ,
86+ } ) ;
87+ let stdout = "" ;
88+ let stderr = "" ;
89+ child . stdout . on ( "data" , ( data ) => {
90+ stdout += data . toString ( ) ;
91+ } ) ;
92+ child . stderr . on ( "data" , ( data ) => {
93+ stderr += data . toString ( ) ;
94+ } ) ;
95+ child . on ( "error" , ( error ) => {
96+ reject ( error ) ;
97+ } ) ;
98+ child . on ( "close" , ( code ) => {
99+ if ( code === 0 ) {
100+ resolve ( stdout . trim ( ) ) ;
101+ } else {
102+ reject ( new Error ( `Command ${ command } exited with code ${ code } : ${ stderr } ` ) ) ;
103+ }
104+ } ) ;
105+ } ) ;
106+ } ;
107+
108+ const skiaDir = path . resolve ( __dirname , "../../../externals/skia" ) ;
109+
110+ const checkoutSkiaBranch = async ( version ) => {
111+ const branchName = `chrome/${ version } ` ;
112+
113+ // Check if the skia directory exists and is a git repo
114+ // (won't exist when installed via npm - submodule is not included in the package)
115+ if ( ! fs . existsSync ( skiaDir ) || ! fs . existsSync ( path . join ( skiaDir , ".git" ) ) ) {
116+ return ;
117+ }
118+
119+ console . log ( `🔀 Checking out Skia branch: ${ branchName } ` ) ;
120+
121+ try {
122+ // Get current branch/commit
123+ const currentRef = await runCommandWithOutput ( "git" , [ "rev-parse" , "--abbrev-ref" , "HEAD" ] , { cwd : skiaDir } ) ;
124+
125+ if ( currentRef === branchName ) {
126+ console . log ( ` ✓ Already on branch ${ branchName } ` ) ;
127+ return ;
128+ }
129+
130+ // Fetch the branch from origin
131+ console . log ( ` Fetching branch ${ branchName } from origin...` ) ;
132+ try {
133+ await runCommand ( "git" , [ "fetch" , "origin" , `${ branchName } :${ branchName } ` ] , { cwd : skiaDir } ) ;
134+ } catch ( e ) {
135+ // Branch might already exist locally, try to update it
136+ await runCommand ( "git" , [ "fetch" , "origin" , branchName ] , { cwd : skiaDir } ) ;
137+ }
138+
139+ // Checkout the branch (use -f to discard local changes in the submodule)
140+ console . log ( ` Checking out ${ branchName } ...` ) ;
141+ await runCommand ( "git" , [ "checkout" , "-f" , branchName ] , { cwd : skiaDir } ) ;
142+
143+ console . log ( ` ✓ Successfully checked out ${ branchName } ` ) ;
144+ } catch ( error ) {
145+ console . error ( ` ⚠️ Failed to checkout branch ${ branchName } : ${ error . message } ` ) ;
146+ console . error ( " Headers may not match the prebuilt binaries!" ) ;
147+ }
148+ } ;
149+
80150const sleep = ( ms ) => new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
81151
82152const downloadToFile = ( url , destPath , maxRetries = 5 ) => {
@@ -359,6 +429,9 @@ const clearDirectory = (directory) => {
359429} ;
360430
361431const main = async ( ) => {
432+ // Ensure the skia submodule is on the correct branch for copying headers
433+ await checkoutSkiaBranch ( skiaVersion ) ;
434+
362435 // Check if binaries are installed and checksums match
363436 if ( areBinariesInstalled ( ) && verifyChecksums ( ) ) {
364437 console . log ( "✅ Prebuilt binaries already installed with matching checksums, skipping download" ) ;
0 commit comments