@@ -33,38 +33,27 @@ const PACKAGE_LOCK_PATH = path.join(__dirname, '..', 'package-lock.json');
33
33
* original state back.
34
34
*/
35
35
async function removeProblematicOptionalDepsFromPackageLock ( ) {
36
- const TEMP_PACKAGE_LOCK_PATH = path . join (
37
- __dirname ,
38
- '..' ,
39
- 'original-package-lock.json' ,
40
- ) ;
41
-
42
36
const packageLockContent = JSON . parse (
43
37
await fs . readFile ( PACKAGE_LOCK_PATH , 'utf-8' ) ,
44
38
) ;
45
39
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 ) {
51
44
console . info ( 'No problematic optional dependencies to fix' ) ;
52
45
return ;
53
46
}
54
47
55
- packageLockContent . packages [ 'node_modules/@vscode/vsce-sign' ] [
56
- 'optionalDependencies'
57
- ] = { } ;
48
+ // Temporarily remove the optional dependencies
49
+ vsceSignPackage [ 'optionalDependencies' ] = { } ;
58
50
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.
60
53
await fs . writeFile (
61
54
PACKAGE_LOCK_PATH ,
62
55
JSON . stringify ( packageLockContent , null , 2 ) ,
63
56
) ;
64
-
65
- return async function restoreOriginalPackageLock ( ) {
66
- return await fs . rename ( TEMP_PACKAGE_LOCK_PATH , PACKAGE_LOCK_PATH ) ;
67
- } ;
68
57
}
69
58
70
59
async function snykTest ( cwd ) {
@@ -105,35 +94,28 @@ async function snykTest(cwd) {
105
94
}
106
95
107
96
async 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 ) ;
115
102
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
+ ) ;
120
107
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
+ ) ;
137
119
}
138
120
139
121
main ( ) ;
0 commit comments