Skip to content

Commit ad46d2c

Browse files
feat(cli): add cache command (#513)
1 parent 7ac13c6 commit ad46d2c

12 files changed

Lines changed: 253 additions & 17 deletions

File tree

README.md

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

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

bin/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ prog
120120
.describe(i18n.getTokenSync("cli.commands.config.desc"))
121121
.action(commands.config.editConfigFile);
122122

123+
prog
124+
.command("cache")
125+
.option("-l, --list", i18n.getTokenSync("cli.commands.cache.option_list"), false)
126+
.option("-c, --clear", i18n.getTokenSync("cli.commands.cache.option_clear"), false)
127+
.option("-f, --full", i18n.getTokenSync("cli.commands.cache.option_full"), false)
128+
.describe(i18n.getTokenSync("cli.commands.cache.desc"))
129+
.action(commands.cache.main);
130+
123131
prog.parse(process.argv);
124132

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

docs/cli/cache.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## 📝 Command `cache`
2+
3+
The `cache` command allows you to manage NodeSecure cache, which is used for the packages navigation and the search page.
4+
5+
## 📜 Syntax
6+
7+
```bash
8+
$ nsecure cache [options]
9+
```
10+
11+
## ⚙️ Available Options
12+
13+
| Name | Shortcut | Default Value | Description |
14+
|---|---|---|---|
15+
| `--list` | `-l` | `false` | Display the cache contents in JSON format. Use with `--full` to include scanned payloads stored on disk. |
16+
| `--clear` | `-c` | `false` | Remove cached entries. Use with `--full` to also delete scanned payloads stored on disk. |
17+
| `--full` | `-f` | `false` | Extend the scope of `--list` or `--clear` to include scanned payloads stored on disk.|

i18n/english.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ const cli = {
6565
configCreate: {
6666
desc: "Init your Nodesecure config file",
6767
option_cwd: "Create config file at the cwd"
68+
},
69+
cache: {
70+
desc: "Manage NodeSecure cache",
71+
missingAction: "No valid action specified. Use --help to see options.",
72+
option_list: "List cache files",
73+
option_clear: "Clear the cache",
74+
option_full: "Clear or list the full cache, including payloads",
75+
cacheTitle: "NodeSecure Cache:",
76+
scannedPayloadsTitle: "Scanned payloads available on disk:",
77+
cleared: "Cache cleared successfully!"
6878
}
6979
},
7080
startHttp: {

i18n/french.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ const cli = {
6565
configCreate: {
6666
desc: "Initialiser le fichier de configuration Nodesecure",
6767
option_cwd: "Créer le fichier dans le dossier courant"
68+
},
69+
cache: {
70+
desc: "Gérer le cache de NodeSecure",
71+
missingAction: "Aucune action valide spécifiée. Utilisez --help pour voir les options.",
72+
option_list: "Lister les fichiers du cache",
73+
option_clear: "Nettoyer le cache",
74+
option_full: "Nettoyer ou lister le cache complet, y compris les payloads",
75+
cacheTitle: "Cache NodeSecure:",
76+
scannedPayloadsTitle: "Payloads scannés disponibles sur le disque:",
77+
cleared: "Cache nettoyé avec succès !"
6878
}
6979
},
7080
startHttp: {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
"build": "node ./esbuild.config.js",
1919
"test": "npm run test-only && npm run lint",
2020
"test-only": "glob -c \"node --no-warnings --test-concurrency 1 --test\" \"test/**/*.test.js\"",
21-
"coverage": "c8 --reporter=lcov npm run test",
22-
"clear:cache": "node ./scripts/clear-cache.js"
21+
"coverage": "c8 --reporter=lcov npm run test"
2322
},
2423
"files": [
2524
"bin",
@@ -94,6 +93,7 @@
9493
"@openally/result": "^1.3.0",
9594
"@polka/send-type": "^0.5.2",
9695
"@topcli/cliui": "^1.1.0",
96+
"@topcli/pretty-json": "^1.0.0",
9797
"@topcli/prompts": "^2.0.0",
9898
"@topcli/spinner": "^3.0.0",
9999
"cacache": "^19.0.1",

scripts/clear-cache.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/cache.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ class _AppCache {
8888
}
8989
}
9090

91-
async #initDefaultPayloadsList() {
91+
async #initDefaultPayloadsList(options = {}) {
92+
const { logging = true } = options;
93+
9294
if (this.startFromZero) {
9395
const payloadsList = {
9496
mru: [],
@@ -99,7 +101,9 @@ class _AppCache {
99101
root: null
100102
};
101103

102-
logger.info("[cache|init](startFromZero)");
104+
if (logging) {
105+
logger.info("[cache|init](startFromZero)");
106+
}
103107
await cacache.put(CACHE_PATH, `${this.prefix}${kPayloadsCache}`, JSON.stringify(payloadsList));
104108

105109
return;
@@ -119,13 +123,22 @@ class _AppCache {
119123
root: formatted
120124
};
121125

122-
logger.info(`[cache|init](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`);
126+
if (logging) {
127+
logger.info(`[cache|init](dep: ${formatted}|version: ${version}|rootDependencyName: ${payload.rootDependencyName})`);
128+
}
123129
await cacache.put(CACHE_PATH, `${this.prefix}${kPayloadsCache}`, JSON.stringify(payloadsList));
124130
this.updatePayload(formatted, payload);
125131
}
126132

127133
async initPayloadsList(options = {}) {
128-
const { logging = true } = options;
134+
const {
135+
logging = true,
136+
reset = false
137+
} = options;
138+
139+
if (reset) {
140+
await cacache.rm.all(CACHE_PATH);
141+
}
129142

130143
try {
131144
// prevent re-initialization of the cache
@@ -138,7 +151,7 @@ class _AppCache {
138151
}
139152
const packagesInFolder = this.availablePayloads();
140153
if (packagesInFolder.length === 0) {
141-
await this.#initDefaultPayloadsList();
154+
await this.#initDefaultPayloadsList({ logging });
142155

143156
return;
144157
}

src/commands/cache.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Import Node.js Dependencies
2+
import { styleText } from "node:util";
3+
import { setImmediate } from "node:timers/promises";
4+
5+
// Import Third-party Dependencies
6+
import prettyJson from "@topcli/pretty-json";
7+
import * as i18n from "@nodesecure/i18n";
8+
9+
// Import Internal Dependencies
10+
import { appCache } from "../cache.js";
11+
12+
export async function main(options) {
13+
const {
14+
list,
15+
clear,
16+
full
17+
} = options;
18+
19+
await i18n.getLocalLang();
20+
21+
if (!(list || clear)) {
22+
console.log(styleText("red", i18n.getTokenSync("cli.commands.cache.missingAction")));
23+
process.exit(1);
24+
}
25+
26+
if (list) {
27+
listCache(full);
28+
}
29+
if (clear) {
30+
await setImmediate();
31+
await clearCache(full);
32+
}
33+
}
34+
35+
async function listCache(full) {
36+
const paylodsList = await appCache.payloadsList();
37+
console.log(styleText(["underline"], i18n.getTokenSync("cli.commands.cache.cacheTitle")));
38+
prettyJson(paylodsList);
39+
40+
if (full) {
41+
console.log(styleText(["underline"], i18n.getTokenSync("cli.commands.cache.scannedPayloadsTitle")));
42+
try {
43+
const payloads = appCache.availablePayloads();
44+
prettyJson(payloads);
45+
}
46+
catch {
47+
prettyJson([]);
48+
}
49+
}
50+
}
51+
52+
async function clearCache(full) {
53+
if (full) {
54+
appCache.availablePayloads().forEach((pkg) => {
55+
appCache.removePayload(pkg);
56+
});
57+
}
58+
59+
await appCache.initPayloadsList({ logging: false, reset: true });
60+
61+
console.log(styleText("green", i18n.getTokenSync("cli.commands.cache.cleared")));
62+
}

src/commands/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * as scanner from "./scanner.js";
66
export * as config from "./config.js";
77
export * as scorecard from "./scorecard.js";
88
export * as report from "./report.js";
9+
export * as cache from "./cache.js";

0 commit comments

Comments
 (0)