commentme 1.0.7 → 1.0.9

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 CHANGED
@@ -9,14 +9,26 @@ 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";
15
16
  import { saveApiKey, clearApiKey } from "../utils/apiKeyManager.js";
16
17
  import { promptPassword } from "../utils/passwordPrompt.js";
17
18
  import dotenv from "dotenv";
19
+ import { readFileSync } from "fs";
20
+ import { dirname, join } from "path";
21
+ import { fileURLToPath } from "url";
22
+
18
23
  dotenv.config();
19
24
 
25
+ const __filename = fileURLToPath(import.meta.url);
26
+ const __dirname = dirname(__filename);
27
+
28
+ // Read version from package.json
29
+ const packageJson = JSON.parse(readFileSync(join(__dirname, "../package.json"), "utf8"));
30
+ const version = packageJson.version;
31
+
20
32
 
21
33
  function promptInput(defaultValue = "") {
22
34
  const rl = readline.createInterface({
@@ -39,10 +51,15 @@ async function main() {
39
51
  console.log("Command:", command);
40
52
 
41
53
  try {
42
- // Show help without connecting to DB or requiring auth
54
+ // Show help or version without connecting to DB or requiring auth
55
+ if (command === "--version" || command === "-v") {
56
+ console.log(`commentme version ${version}`);
57
+ return;
58
+ }
59
+
43
60
  if (command === "--help" || command === "-h" || !command) {
44
61
  console.log(`
45
- commentme CLI
62
+ commentme CLI (v${version})
46
63
 
47
64
  Commands:
48
65
  commentme --get line-7-7 <file> Get a specific comment by line range
@@ -53,9 +70,13 @@ Commands:
53
70
  commentme --unskim <file> Restore comments to a file
54
71
  commentme --generate <file> Generate AI comments and docs for a file
55
72
  commentme --explain <file> Generate a full markdown explanation of a code file
73
+ commentme --sanitize <file> Sanitize file for production (removes noisy comments)
56
74
  commentme --set-key Set your own OpenRouter API key (stored securely)
57
75
  commentme --clear-key Remove your saved API key
76
+ commentme --login Log in to your account
77
+ commentme --signup Create a new account
58
78
  commentme --logout Log out from your session
79
+ commentme --version Show version number
59
80
  commentme --help Show this help message
60
81
  `);
61
82
  return;
@@ -153,6 +174,11 @@ Select generation type:
153
174
  await generateExplanation(args[1]);
154
175
  break;
155
176
 
177
+ case "--sanitize":
178
+ if (!args[1]) throw new Error("Usage: commentme --sanitize <file>");
179
+ await sanitizeFile(args[1]);
180
+ break;
181
+
156
182
  case "--set-key": {
157
183
  console.log("Paste your OpenRouter API key (input is hidden):");
158
184
  const key = await promptPassword("🔑 API Key: ");
@@ -174,7 +200,7 @@ Select generation type:
174
200
 
175
201
  default:
176
202
  console.log(`
177
- commentme CLI
203
+ commentme CLI (v${version})
178
204
 
179
205
  Commands:
180
206
  commentme --login
@@ -187,9 +213,12 @@ Commands:
187
213
  commentme --unskim <file>
188
214
  commentme --generate <file>
189
215
  commentme --explain <file>
216
+ commentme --sanitize <file>
190
217
  commentme --set-key
191
218
  commentme --clear-key
192
219
  commentme --logout
220
+ commentme --version
221
+ commentme --help
193
222
  `);
194
223
  }
195
224
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "commentme",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "A CLI tool to manage, redact and unredact your comments keeping the codebase clutterfree.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -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
+ }