@@ -33,38 +33,27 @@ const PACKAGE_LOCK_PATH = path.join(__dirname, '..', 'package-lock.json');
3333 * original state back.
3434 */
3535async function removeProblematicOptionalDepsFromPackageLock ( ) {
36- const TEMP_PACKAGE_LOCK_PATH = path . join (
37- __dirname ,
38- '..' ,
39- 'original-package-lock.json' ,
40- ) ;
41-
4236 const packageLockContent = JSON . parse (
4337 await fs . readFile ( PACKAGE_LOCK_PATH , 'utf-8' ) ,
4438 ) ;
4539
46- if (
47- ! packageLockContent . packages ?. [ 'node_modules/@vscode/vsce-sign' ] ?. [
48- 'optionalDependencies'
49- ]
50- ) {
40+ const vsceSignPackage =
41+ packageLockContent . packages ?. [ 'node_modules/@vscode/vsce-sign' ] ;
42+
43+ if ( ! vsceSignPackage || ! vsceSignPackage . optionalDependencies ) {
5144 console . info ( 'No problematic optional dependencies to fix' ) ;
5245 return ;
5346 }
5447
55- packageLockContent . packages [ 'node_modules/@vscode/vsce-sign' ] [
56- 'optionalDependencies'
57- ] = { } ;
48+ // Temporarily remove the optional dependencies
49+ vsceSignPackage [ 'optionalDependencies' ] = { } ;
5850
59- await fs . rename ( PACKAGE_LOCK_PATH , TEMP_PACKAGE_LOCK_PATH ) ;
51+ // We write the actual package-lock path but restoring of the original file is
52+ // handled by npm hooks.
6053 await fs . writeFile (
6154 PACKAGE_LOCK_PATH ,
6255 JSON . stringify ( packageLockContent , null , 2 ) ,
6356 ) ;
64-
65- return async function restoreOriginalPackageLock ( ) {
66- return await fs . rename ( TEMP_PACKAGE_LOCK_PATH , PACKAGE_LOCK_PATH ) ;
67- } ;
6857}
6958
7059async function snykTest ( cwd ) {
@@ -105,35 +94,28 @@ async function snykTest(cwd) {
10594}
10695
10796async function main ( ) {
108- let revertPackageLockChanges ;
109- try {
110- const rootPath = path . resolve ( __dirname , '..' ) ;
111- await fs . mkdir ( path . join ( rootPath , `.sbom` ) , { recursive : true } ) ;
112- revertPackageLockChanges =
113- await removeProblematicOptionalDepsFromPackageLock ( ) ;
114- const results = await snykTest ( rootPath ) ;
97+ const rootPath = path . resolve ( __dirname , '..' ) ;
98+ await fs . mkdir ( path . join ( rootPath , `.sbom` ) , { recursive : true } ) ;
99+ revertPackageLockChanges =
100+ await removeProblematicOptionalDepsFromPackageLock ( ) ;
101+ const results = await snykTest ( rootPath ) ;
115102
116- await fs . writeFile (
117- path . join ( rootPath , `.sbom/snyk-test-result.json` ) ,
118- JSON . stringify ( results , null , 2 ) ,
119- ) ;
103+ await fs . writeFile (
104+ path . join ( rootPath , `.sbom/snyk-test-result.json` ) ,
105+ JSON . stringify ( results , null , 2 ) ,
106+ ) ;
120107
121- await execFile (
122- 'npx' ,
123- [
124- 'snyk-to-html' ,
125- '-i' ,
126- path . join ( rootPath , '.sbom/snyk-test-result.json' ) ,
127- '-o' ,
128- path . join ( rootPath , `.sbom/snyk-test-result.html` ) ,
129- ] ,
130- { cwd : rootPath } ,
131- ) ;
132- } finally {
133- if ( revertPackageLockChanges ) {
134- await revertPackageLockChanges ( ) ;
135- }
136- }
108+ await execFile (
109+ 'npx' ,
110+ [
111+ 'snyk-to-html' ,
112+ '-i' ,
113+ path . join ( rootPath , '.sbom/snyk-test-result.json' ) ,
114+ '-o' ,
115+ path . join ( rootPath , `.sbom/snyk-test-result.html` ) ,
116+ ] ,
117+ { cwd : rootPath } ,
118+ ) ;
137119}
138120
139121main ( ) ;
0 commit comments