commentme 1.0.0

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.
@@ -0,0 +1,233 @@
1
+ import path from "path";
2
+
3
+ // Map file extensions to comment patterns
4
+ const commentPatterns = {
5
+ // Hash (#) - Python, Ruby, Shell, etc.
6
+ py: { line: "#", block: { start: '"""', end: '"""' } },
7
+ rb: { line: "#", block: { start: "=begin", end: "=end" } },
8
+ sh: { line: "#", block: null },
9
+ yml: { line: "#", block: null },
10
+ yaml: { line: "#", block: null },
11
+ pl: { line: "#", block: { start: "=pod", end: "=cut" } },
12
+ r: { line: "#", block: null },
13
+ coffee: { line: "#", block: { start: "###", end: "###" } },
14
+
15
+ // Double slash (//) - JavaScript, C++, Java, etc.
16
+ js: { line: "//", block: { start: "/*", end: "*/" } },
17
+ jsx: { line: "//", block: { start: "/*", end: "*/" } },
18
+ ts: { line: "//", block: { start: "/*", end: "*/" } },
19
+ tsx: { line: "//", block: { start: "/*", end: "*/" } },
20
+ cpp: { line: "//", block: { start: "/*", end: "*/" } },
21
+ c: { line: "//", block: { start: "/*", end: "*/" } },
22
+ h: { line: "//", block: { start: "/*", end: "*/" } },
23
+ java: { line: "//", block: { start: "/*", end: "*/" } },
24
+ cs: { line: "//", block: { start: "/*", end: "*/" } },
25
+ go: { line: "//", block: { start: "/*", end: "*/" } },
26
+ rs: { line: "//", block: { start: "/*", end: "*/" } },
27
+ php: { line: "//", block: { start: "/*", end: "*/" } },
28
+ swift: { line: "//", block: { start: "/*", end: "*/" } },
29
+ scala: { line: "//", block: { start: "/*", end: "*/" } },
30
+ kt: { line: "//", block: { start: "/*", end: "*/" } },
31
+ dart: { line: "//", block: { start: "/*", end: "*/" } },
32
+
33
+ // Tags (<!-- -->) - HTML, XML, etc.
34
+ html: { line: "<!--", block: { start: "<!--", end: "-->" } },
35
+ xml: { line: "<!--", block: { start: "<!--", end: "-->" } },
36
+ htm: { line: "<!--", block: { start: "<!--", end: "-->" } },
37
+ xhtml: { line: "<!--", block: { start: "<!--", end: "-->" } },
38
+ md: { line: "<!--", block: { start: "<!--", end: "-->" } },
39
+ markdown: { line: "<!--", block: { start: "<!--", end: "-->" } },
40
+
41
+ // Dashes (--) - SQL, Haskell, etc.
42
+ sql: { line: "--", block: { start: "/*", end: "*/" } },
43
+ hs: { line: "--", block: { start: "{-", end: "-}" } },
44
+ lua: { line: "--", block: { start: "--[[", end: "]]" } },
45
+ elm: { line: "--", block: { start: "{--", end: "--}" } },
46
+ vhdl: { line: "--", block: null },
47
+
48
+ // Semicolon (;) - Assembly, Lisp, etc.
49
+ asm: { line: ";", block: null },
50
+ s: { line: ";", block: null },
51
+ clj: { line: ";", block: { start: "#|", end: "|#" } },
52
+ cljs: { line: ";", block: { start: "#|", end: "|#" } },
53
+ lisp: { line: ";", block: { start: "#|", end: "|#" } },
54
+
55
+ // Percent (%) - LaTeX, Erlang, etc.
56
+ tex: { line: "%", block: null },
57
+ erl: { line: "%", block: null },
58
+ hrl: { line: "%", block: null },
59
+
60
+ // Other patterns
61
+ css: { line: null, block: { start: "/*", end: "*/" } },
62
+ scss: { line: "//", block: { start: "/*", end: "*/" } },
63
+ sass: { line: "//", block: { start: "/*", end: "*/" } },
64
+ less: { line: "//", block: { start: "/*", end: "*/" } },
65
+ };
66
+
67
+ export function getCommentPattern(filePath) {
68
+ const ext = path.extname(filePath).slice(1).toLowerCase();
69
+ return commentPatterns[ext] || commentPatterns.js; // Default to JS if unknown
70
+ }
71
+
72
+ export function detectComments(code, pattern) {
73
+ const comments = [];
74
+ const lines = code.split("\n");
75
+
76
+ let charOffset = 0;
77
+ let inBlockComment = false;
78
+ let blockStart = null;
79
+ let blockStartLine = null;
80
+ let blockText = [];
81
+
82
+ for (let i = 0; i < lines.length; i++) {
83
+ const line = lines[i];
84
+ const lineNum = i + 1;
85
+ const lineStartChar = charOffset;
86
+
87
+ // Handle block comments first (they take precedence)
88
+ if (pattern.block) {
89
+ const blockStartPattern = pattern.block.start;
90
+ const blockEndPattern = pattern.block.end;
91
+
92
+ if (!inBlockComment) {
93
+ // Look for block start
94
+ const startIndex = line.indexOf(blockStartPattern);
95
+ if (startIndex !== -1) {
96
+ // Double check it's not in a string
97
+ const beforeStart = line.substring(0, startIndex);
98
+ if (!isInString(beforeStart)) {
99
+ inBlockComment = true;
100
+ blockStart = startIndex;
101
+ blockStartLine = lineNum;
102
+ blockText = [line];
103
+
104
+ // Check if it ends on the same line
105
+ const endIndex = line.indexOf(blockEndPattern, startIndex + blockStartPattern.length);
106
+ if (endIndex !== -1) {
107
+ const commentText = line.substring(startIndex + blockStartPattern.length, endIndex).trim();
108
+ comments.push({
109
+ start: lineStartChar + startIndex,
110
+ end: lineStartChar + endIndex + blockEndPattern.length,
111
+ text: commentText,
112
+ lineStart: lineNum,
113
+ lineEnd: lineNum,
114
+ isBlock: true,
115
+ isInline: startIndex > 0 && line.substring(0, startIndex).trim().length > 0
116
+ });
117
+ inBlockComment = false;
118
+ blockText = [];
119
+ }
120
+ }
121
+ }
122
+ } else {
123
+ // Already inside a block comment, look for end
124
+ blockText.push(line);
125
+ const endIndex = line.indexOf(blockEndPattern);
126
+ if (endIndex !== -1) {
127
+ const firstLine = blockText[0];
128
+ const lastLine = blockText[blockText.length - 1];
129
+ const firstLineStart = firstLine.indexOf(blockStartPattern);
130
+
131
+ let commentText = "";
132
+ if (blockText.length === 1) {
133
+ commentText = firstLine.substring(firstLineStart + blockStartPattern.length, endIndex).trim();
134
+ } else {
135
+ commentText = firstLine.substring(firstLineStart + blockStartPattern.length).trim();
136
+ for (let j = 1; j < blockText.length - 1; j++) {
137
+ commentText += "\n" + blockText[j].trim();
138
+ }
139
+ commentText += "\n" + lastLine.substring(0, endIndex).trim();
140
+ }
141
+
142
+ comments.push({
143
+ start: getCharPosition(lines, blockStartLine, firstLineStart),
144
+ end: lineStartChar + endIndex + blockEndPattern.length,
145
+ text: commentText.trim(),
146
+ lineStart: blockStartLine,
147
+ lineEnd: lineNum,
148
+ isBlock: true,
149
+ isInline: firstLineStart > 0 && firstLine.substring(0, firstLineStart).trim().length > 0
150
+ });
151
+ inBlockComment = false;
152
+ blockText = [];
153
+ }
154
+ }
155
+ }
156
+
157
+ // Handle line comments only if NOT inside a block comment
158
+ if (!inBlockComment && pattern.line) {
159
+ const commentMarker = pattern.line;
160
+ const commentIndex = line.indexOf(commentMarker);
161
+
162
+ if (commentIndex !== -1) {
163
+ const beforeComment = line.substring(0, commentIndex);
164
+ if (!isInString(beforeComment)) {
165
+ const commentText = line.substring(commentIndex + commentMarker.length).trim();
166
+ if (!commentText.startsWith("#ref")) {
167
+ const isInline = commentIndex > 0 && beforeComment.trim().length > 0;
168
+ comments.push({
169
+ start: lineStartChar + commentIndex,
170
+ end: lineStartChar + line.length,
171
+ text: commentText,
172
+ lineStart: lineNum,
173
+ lineEnd: lineNum,
174
+ isBlock: false,
175
+ isInline: isInline
176
+ });
177
+ }
178
+ }
179
+ }
180
+ }
181
+
182
+ charOffset += line.length + 1;
183
+ }
184
+
185
+ return comments;
186
+ }
187
+
188
+ function getCharPosition(lines, lineNum, column) {
189
+ let pos = 0;
190
+ for (let i = 0; i < lineNum - 1; i++) {
191
+ pos += lines[i].length + 1; // +1 for newline
192
+ }
193
+ return pos + column;
194
+ }
195
+
196
+ function isInString(text) {
197
+ // Simple check: count quotes (not perfect but works for most cases)
198
+ let inSingle = false;
199
+ let inDouble = false;
200
+
201
+ for (let i = 0; i < text.length; i++) {
202
+ if (text[i] === "'" && !inDouble) inSingle = !inSingle;
203
+ if (text[i] === '"' && !inSingle) inDouble = !inDouble;
204
+ }
205
+
206
+ return inSingle || inDouble;
207
+ }
208
+
209
+ export function formatComment(text, pattern, isBlock) {
210
+ if (isBlock && pattern.block) {
211
+ return `${pattern.block.start} ${text} ${pattern.block.end}`;
212
+ } else if (pattern.line) {
213
+ return `${pattern.line} ${text}`;
214
+ }
215
+ return text;
216
+ }
217
+
218
+ export function formatReferenceComment(key, pattern, isBlock = false) {
219
+ if (isBlock && pattern.block) {
220
+ return `${pattern.block.start} #ref ${key} ${pattern.block.end}`;
221
+ }
222
+
223
+ if (pattern.line) {
224
+ return `${pattern.line} #ref ${key}`;
225
+ }
226
+
227
+ // For block-only languages, use block if line is not available
228
+ if (pattern.block) {
229
+ return `${pattern.block.start} #ref ${key} ${pattern.block.end}`;
230
+ }
231
+
232
+ return `// #ref ${key}`;
233
+ }
@@ -0,0 +1 @@
1
+ export const API_BASE_URL = "https://commentme-web-backend.onrender.com";
@@ -0,0 +1,9 @@
1
+ import { getSession } from "./session.js";
2
+
3
+ export function getCurrentUserId() {
4
+ const session = getSession();
5
+ if (!session) {
6
+ throw new Error("Not authenticated");
7
+ }
8
+ return session.userId;
9
+ }
@@ -0,0 +1,51 @@
1
+ export function promptPassword(promptText = "Password: ") {
2
+ return new Promise(resolve => {
3
+ const stdin = process.stdin;
4
+ const stdout = process.stdout;
5
+
6
+ stdout.write(promptText);
7
+
8
+ stdin.setRawMode(true);
9
+ stdin.resume();
10
+ stdin.setEncoding("utf8");
11
+
12
+ let password = "";
13
+
14
+ function onData(char) {
15
+ // ENTER / RETURN
16
+ if (char === "\n" || char === "\r") {
17
+ stdout.write("\n");
18
+ stdin.setRawMode(false);
19
+ stdin.pause();
20
+ stdin.removeListener("data", onData);
21
+ resolve(password);
22
+ return;
23
+ }
24
+
25
+ // CTRL + C
26
+ if (char === "\u0003") {
27
+ stdout.write("\n");
28
+ process.exit(1);
29
+ }
30
+
31
+ // BACKSPACE (Linux/macOS)
32
+ if (char === "\u007F") {
33
+ if (password.length > 0) {
34
+ password = password.slice(0, -1);
35
+ stdout.write("\b \b");
36
+ }
37
+ return;
38
+ }
39
+
40
+ // Ignore other control characters
41
+ if (char < " ") return;
42
+
43
+ // Normal character
44
+ password += char;
45
+ stdout.write("*");
46
+ }
47
+
48
+ stdin.on("data", onData);
49
+ });
50
+ }
51
+
@@ -0,0 +1,22 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import os from "os";
4
+
5
+ const SESSION_PATH = path.join(os.homedir(), ".commentme-session.json");
6
+
7
+ export function saveSession(sessionData) {
8
+ fs.writeFileSync(SESSION_PATH, JSON.stringify(sessionData, null, 2));
9
+ }
10
+
11
+ export function getSession() {
12
+ if (!fs.existsSync(SESSION_PATH)) return null;
13
+ try {
14
+ return JSON.parse(fs.readFileSync(SESSION_PATH, "utf8"));
15
+ } catch (e) {
16
+ return null;
17
+ }
18
+ }
19
+
20
+ export function clearSession() {
21
+ if (fs.existsSync(SESSION_PATH)) fs.unlinkSync(SESSION_PATH);
22
+ }