Skip to content

Commit d33dce8

Browse files
chore: use npm hooks for backup and restore
1 parent ee29149 commit d33dce8

File tree

2 files changed

+30
-46
lines changed

2 files changed

+30
-46
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@
6464
"check-vsix-size": "ts-node ./scripts/check-vsix-size.ts",
6565
"release-draft": "node ./scripts/release-draft.js",
6666
"reformat": "prettier --write .",
67+
"presnyk-test": "echo \"Creating backup for package-lock.json.\"; cp package-lock.json original-package-lock.json",
6768
"snyk-test": "node scripts/snyk-test.js",
69+
"postsnyk-test": "echo \"Restoring original package-lock.json.\"; mv original-package-lock.json package-lock.json",
6870
"generate-icon-font": "ts-node ./scripts/generate-icon-font.ts",
6971
"generate-vulnerability-report": "mongodb-sbom-tools generate-vulnerability-report --snyk-reports=.sbom/snyk-test-result.json --dependencies=.sbom/dependencies.json --fail-on=high",
7072
"create-vulnerability-tickets": "mongodb-sbom-tools generate-vulnerability-report --snyk-reports=.sbom/snyk-test-result.json --dependencies=.sbom/dependencies.json --create-jira-issues",

scripts/snyk-test.js

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,27 @@ const PACKAGE_LOCK_PATH = path.join(__dirname, '..', 'package-lock.json');
3333
* original state back.
3434
*/
3535
async 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

7059
async function snykTest(cwd) {
@@ -105,35 +94,28 @@ async function snykTest(cwd) {
10594
}
10695

10796
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);
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

139121
main();

0 commit comments

Comments
 (0)