@@ -117,13 +117,30 @@ async function installPackage(packageInfo) {
117117 ) {
118118 // Always reapply Socket override files to ensure they're up-to-date.
119119 writeProgress ( '💾' )
120+
121+ // Save existing scripts and devDependencies before copying.
122+ const savedScripts = existingPkgJson . scripts
123+ const savedDevDeps = existingPkgJson . devDependencies
124+
120125 await copy ( overridePath , installedPath , {
121126 overwrite : true ,
122127 dereference : true ,
123128 filter : src =>
124129 ! src . includes ( 'node_modules' ) && ! src . endsWith ( '.DS_Store' ) ,
125130 } )
126131
132+ // Restore scripts and devDependencies after copying.
133+ const editablePkgJson = await readPackageJson ( installedPath , {
134+ editable : true ,
135+ } )
136+ if ( savedScripts ) {
137+ editablePkgJson . scripts = savedScripts
138+ }
139+ if ( savedDevDeps ) {
140+ editablePkgJson . devDependencies = savedDevDeps
141+ }
142+ await editablePkgJson . save ( )
143+
127144 // Check mark for cached with refreshed overrides.
128145 writeProgress ( '✓' )
129146 completePackage ( )
@@ -210,17 +227,18 @@ async function installPackage(packageInfo) {
210227 } )
211228
212229 // Merge back the test scripts and devDependencies if they existed.
213- const pkgJsonPath = path . join ( installedPath , 'package.json' )
214- const pkgJson = JSON . parse ( await fs . readFile ( pkgJsonPath , 'utf8' ) )
230+ const editablePkgJson = await readPackageJson ( installedPath , {
231+ editable : true ,
232+ } )
215233
216234 // Preserve devDependencies from original.
217235 if ( originalPkgJson . devDependencies ) {
218- pkgJson . devDependencies = originalPkgJson . devDependencies
236+ editablePkgJson . devDependencies = originalPkgJson . devDependencies
219237 }
220238
221239 // Preserve test scripts.
222240 if ( originalScripts ) {
223- pkgJson . scripts = pkgJson . scripts || { }
241+ editablePkgJson . scripts = editablePkgJson . scripts || { }
224242
225243 // Look for actual test runner in scripts.
226244 const additionalTestRunners = [ ...testRunners , 'test:stock' , 'test:all' ]
@@ -238,35 +256,35 @@ async function installPackage(packageInfo) {
238256
239257 // Use the actual test script or cleaned version.
240258 if ( actualTestScript && originalScripts [ actualTestScript ] ) {
241- pkgJson . scripts . test = cleanTestScript (
259+ editablePkgJson . scripts . test = cleanTestScript (
242260 originalScripts [ actualTestScript ] ,
243261 )
244262 // Also preserve the actual script if it's referenced.
245263 if ( actualTestScript !== 'test' ) {
246- pkgJson . scripts [ actualTestScript ] = cleanTestScript (
264+ editablePkgJson . scripts [ actualTestScript ] = cleanTestScript (
247265 originalScripts [ actualTestScript ] ,
248266 )
249267 }
250268 } else if ( originalScripts . test ) {
251269 // Fallback to simple test script if it exists.
252- pkgJson . scripts . test = cleanTestScript ( originalScripts . test )
270+ editablePkgJson . scripts . test = cleanTestScript ( originalScripts . test )
253271 }
254272
255273 // Preserve any test:* and tests-* scripts that might be referenced.
256274 for ( const [ key , value ] of Object . entries ( originalScripts ) ) {
257275 if (
258276 ( key . startsWith ( 'test:' ) || key . startsWith ( 'tests' ) ) &&
259- ! pkgJson . scripts [ key ]
277+ ! editablePkgJson . scripts [ key ]
260278 ) {
261- pkgJson . scripts [ key ] = cleanTestScript ( value )
279+ editablePkgJson . scripts [ key ] = cleanTestScript ( value )
262280 }
263281 }
264282 }
265283
266- await fs . writeFile ( pkgJsonPath , JSON . stringify ( pkgJson , null , 2 ) )
284+ await editablePkgJson . save ( )
267285
268286 // Check for test script.
269- const testScript = pkgJson . scripts ?. test
287+ const testScript = editablePkgJson . scripts ?. test
270288
271289 if ( ! testScript ) {
272290 writeProgress ( LOG_SYMBOLS . warn )
0 commit comments