commentme 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/commentme.js +8 -0
- package/package.json +1 -1
- package/sanitize-test.js +21 -0
- package/sanitize.js +62 -0
package/bin/commentme.js
CHANGED
|
@@ -9,6 +9,7 @@ import { deleteComment } from "../deletecoms.js";
|
|
|
9
9
|
import { removeCommentsFromFile as skim } from "../skimcoms.js";
|
|
10
10
|
import { unskimComments as unskim } from "../unskimcoms.js";
|
|
11
11
|
import { generateCommentsPerFunc, generateCommentsPerClass, generateCommentsPerLine, generateExplanation } from "../generate.js";
|
|
12
|
+
import { sanitizeFile } from "../sanitize.js";
|
|
12
13
|
// import { connectDB, disconnectDB } from "../config/db.js";
|
|
13
14
|
import { ensureAuth } from "../auth/authGuard.js";
|
|
14
15
|
import { logout } from "../auth/logout.js";
|
|
@@ -53,6 +54,7 @@ Commands:
|
|
|
53
54
|
commentme --unskim <file> Restore comments to a file
|
|
54
55
|
commentme --generate <file> Generate AI comments and docs for a file
|
|
55
56
|
commentme --explain <file> Generate a full markdown explanation of a code file
|
|
57
|
+
commentme --sanitize <file> Sanitize file for production (removes noisy comments)
|
|
56
58
|
commentme --set-key Set your own OpenRouter API key (stored securely)
|
|
57
59
|
commentme --clear-key Remove your saved API key
|
|
58
60
|
commentme --logout Log out from your session
|
|
@@ -153,6 +155,11 @@ Select generation type:
|
|
|
153
155
|
await generateExplanation(args[1]);
|
|
154
156
|
break;
|
|
155
157
|
|
|
158
|
+
case "--sanitize":
|
|
159
|
+
if (!args[1]) throw new Error("Usage: commentme --sanitize <file>");
|
|
160
|
+
await sanitizeFile(args[1]);
|
|
161
|
+
break;
|
|
162
|
+
|
|
156
163
|
case "--set-key": {
|
|
157
164
|
console.log("Paste your OpenRouter API key (input is hidden):");
|
|
158
165
|
const key = await promptPassword("🔑 API Key: ");
|
|
@@ -187,6 +194,7 @@ Commands:
|
|
|
187
194
|
commentme --unskim <file>
|
|
188
195
|
commentme --generate <file>
|
|
189
196
|
commentme --explain <file>
|
|
197
|
+
commentme --sanitize <file>
|
|
190
198
|
commentme --set-key
|
|
191
199
|
commentme --clear-key
|
|
192
200
|
commentme --logout
|
package/package.json
CHANGED
package/sanitize-test.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is a JSDoc block.
|
|
3
|
+
* It should be PRESERVED.
|
|
4
|
+
* @param {string} msg
|
|
5
|
+
*/
|
|
6
|
+
function testDoc(msg) {
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
console.log("This console.log should be PRESERVED.");
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
const x = 10;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Another JSDoc.
|
|
16
|
+
* Should be PRESERVED.
|
|
17
|
+
*/
|
|
18
|
+
return msg + x;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
testDoc("Hello");
|
package/sanitize.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import * as acorn from "acorn";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { getCurrentUserId } from "./utils/currentUser.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Sanitizes a file for production by removing:
|
|
8
|
+
* 1. Single-line comments (// ...)
|
|
9
|
+
* 2. Block comments (/* ... * /) EXCEPT JSDocs (/** ... * /)
|
|
10
|
+
*
|
|
11
|
+
* Note: console.log statements are preserved per user request.
|
|
12
|
+
*/
|
|
13
|
+
export async function sanitizeFile(filePath) {
|
|
14
|
+
if (!fs.existsSync(filePath)) {
|
|
15
|
+
throw new Error(`File not found: ${filePath}`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Check authentication
|
|
19
|
+
try {
|
|
20
|
+
getCurrentUserId();
|
|
21
|
+
} catch (e) {
|
|
22
|
+
console.error(e.message);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const code = fs.readFileSync(filePath, "utf8");
|
|
27
|
+
const comments = [];
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
acorn.parse(code, {
|
|
31
|
+
ecmaVersion: 2020,
|
|
32
|
+
sourceType: "module",
|
|
33
|
+
locations: true,
|
|
34
|
+
onComment: (isBlock, text, start, end) => {
|
|
35
|
+
const isJSDoc = isBlock && text.startsWith("*");
|
|
36
|
+
|
|
37
|
+
// If it's a single-line comment OR a non-JSDoc block comment, mark for removal
|
|
38
|
+
if (!isBlock || !isJSDoc) {
|
|
39
|
+
comments.push({ start, end });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
} catch (e) {
|
|
44
|
+
console.error(`❌ Error parsing ${filePath}: ${e.message}`);
|
|
45
|
+
console.log("⚠️ Falling back to manual sanitization might be risky. Skipping this file.");
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Sort comments in descending order to avoid offset issues while slicing
|
|
50
|
+
comments.sort((a, b) => b.start - a.start);
|
|
51
|
+
|
|
52
|
+
let result = code;
|
|
53
|
+
for (const comment of comments) {
|
|
54
|
+
result = result.slice(0, comment.start) + result.slice(comment.end);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Optional: Clean up empty lines that only contained comments
|
|
58
|
+
// result = result.replace(/^\s*[\r\n]/gm, "");
|
|
59
|
+
|
|
60
|
+
fs.writeFileSync(filePath, result, "utf8");
|
|
61
|
+
console.log(`✅ ${filePath} sanitized (comments removed, documentation preserved)`);
|
|
62
|
+
}
|