Skip to content

Commit c7cb8ab

Browse files
authored
feat(cli): add extract integrity command (#543)
1 parent ab143c8 commit c7cb8ab

File tree

8 files changed

+69
-0
lines changed

8 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ For complete details on each command, refer to the following documents:
111111
- [`config create`](./docs/cli/config.md)
112112
- [`config`](./docs/cli/config.md)
113113
- [`cache`](./docs/cli/cache.md)
114+
- [`extract integrity`](./docs/cli/extract-integrity.md)
114115

115116
Each link provides access to the full documentation for the command, including additional details, options, and usage examples.
116117

bin/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ prog
128128
.describe(i18n.getTokenSync("cli.commands.cache.desc"))
129129
.action(commands.cache.main);
130130

131+
prog
132+
.command("extract integrity [spec]")
133+
.describe(i18n.getTokenSync("cli.commands.extractIntegrity.desc"))
134+
.option("-t, --token", i18n.getTokenSync("cli.commands.extractIntegrity.option_token"))
135+
.action(commands.extractIntegrity.main);
136+
131137
prog.parse(process.argv);
132138

133139
function defaultScannerCommand(name, options = {}) {

docs/cli/extract-integrity.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 📂 Command `extract integrity`
2+
3+
The `extract integrity` extract the integrity of a package from its manifest and tarball and compare the two integrities if different from one another.
4+
5+
## 📜 Syntax
6+
7+
```bash
8+
$ nsecure extract integrity [spec]
9+
```
10+
11+
## ⚙️ Available Options
12+
13+
| Name | Shortcut | Default Value | Description |
14+
|---|---|---|---|
15+
| `--token` | `-t` | undefined | NPM token. |

i18n/english.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ const cli = {
7676
cacheTitle: "NodeSecure Cache:",
7777
scannedPayloadsTitle: "Scanned payloads available on disk:",
7878
cleared: "Cache cleared successfully!"
79+
},
80+
extractIntegrity: {
81+
desc: "Extract the integrity of a package from its manifest and tarball and compare the two integrities if different from one another.",
82+
option_token: "NPM token"
7983
}
8084
},
8185
startHttp: {

i18n/french.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ const cli = {
7676
cacheTitle: "Cache NodeSecure:",
7777
scannedPayloadsTitle: "Payloads scannés disponibles sur le disque:",
7878
cleared: "Cache nettoyé avec succès !"
79+
},
80+
extractIntegrity: {
81+
desc: "Extraire l'intégrité d'un paquet à partir de son manifeste et du tarball et comparer les deux intégrités si elles sont différentes.",
82+
option_token: "Jeton NPM"
7983
}
8084
},
8185
startHttp: {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
"@topcli/spinner": "^3.0.0",
109109
"cacache": "^19.0.1",
110110
"chokidar": "^4.0.3",
111+
"diff": "^8.0.2",
111112
"dotenv": "^17.0.0",
112113
"filenamify": "^6.0.0",
113114
"glob": "^11.0.1",

src/commands/extract-integrity.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Import Third-party Dependencies
2+
import kleur from "kleur";
3+
import { diffChars } from "diff";
4+
import { packumentVersion } from "@nodesecure/npm-registry-sdk";
5+
import { tarball } from "@nodesecure/scanner";
6+
7+
export async function main(spec, options) {
8+
const [pkgName, pkgVersion] = spec.split("@");
9+
const { dist: { tarball: location, shasum: manifestIntegrity } } = await packumentVersion(pkgName, pkgVersion, {
10+
token: options.token
11+
});
12+
const manifestManager = await tarball.extractAndResolve(location, {
13+
spec
14+
});
15+
const tarballIntegrity = manifestManager.integrity;
16+
if (manifestIntegrity === tarballIntegrity) {
17+
console.log(`integrity: ${manifestIntegrity}`);
18+
19+
return;
20+
}
21+
22+
console.log(`manifest integrity: ${manifestIntegrity}`);
23+
console.log(`tarball integrity: ${tarballIntegrity}`);
24+
process.stdout.write("integrity diff: ");
25+
for (const { added, removed, value } of diffChars(manifestIntegrity, tarballIntegrity)) {
26+
if (added) {
27+
process.stdout.write(kleur.green().bold(`+${value}`));
28+
}
29+
else if (removed) {
30+
process.stdout.write(kleur.red().bold(`-${value}`));
31+
}
32+
else {
33+
process.stdout.write(value);
34+
}
35+
}
36+
console.log("\n");
37+
}

src/commands/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * as config from "./config.js";
77
export * as scorecard from "./scorecard.js";
88
export * as report from "./report.js";
99
export * as cache from "./cache.js";
10+
export * as extractIntegrity from "./extract-integrity.js";

0 commit comments

Comments
 (0)