devsplain 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.
package/bin/cli.js ADDED
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Requires the necessary modules for the script to run.
4
+ * @requires ../lib/llm.js - provides the getComments function
5
+ * @requires ../lib/config.js - provides the getConfig function
6
+ * @requires fs - provides file system functionality
7
+ */
8
+ const { getComments } = require('../lib/llm.js')
9
+ const { getConfig } = require('../lib/config.js')
10
+ const fs = require('fs');
11
+
12
+ /**
13
+ * Retrieves the file path from the command line arguments.
14
+ * @type {string}
15
+ */
16
+ const filepath = process.argv[2]
17
+
18
+ /**
19
+ * Checks if a file path was provided.
20
+ * If not, it logs the usage message and exits the process.
21
+ */
22
+ if (!filepath) {
23
+ console.log("usage: commie <file>")
24
+ process.exit(1)
25
+ }
26
+ else {
27
+ /**
28
+ * Defines an asynchronous function to comment the code in the provided file.
29
+ * @async
30
+ */
31
+ (async () => {
32
+ /**
33
+ * Retrieves the configuration settings.
34
+ * @type {object}
35
+ */
36
+ const config = await getConfig();
37
+
38
+ /**
39
+ * Reads the contents of the file at the provided file path.
40
+ * @type {string}
41
+ */
42
+ const data = fs.readFileSync(filepath, 'utf-8');
43
+
44
+ // Log a message to indicate that the file is being analyzed
45
+ console.log("Analyzing File...");
46
+
47
+ /**
48
+ * Retrieves the commented code using the getComments function.
49
+ * @type {string}
50
+ */
51
+ const commentedCode = await getComments(data, 'javascript', config)
52
+
53
+ // Write the commented code back to the file
54
+ fs.writeFileSync(filepath, commentedCode);
55
+
56
+ // Log a success message with the file path
57
+ console.log(`Successfully commented ${filepath}`);
58
+ })();
59
+ }
package/lib/config.js ADDED
@@ -0,0 +1,36 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const os = require('os');
4
+ const readline = require('readline');
5
+ const configPath = path.join(os.homedir(), '.commierc');
6
+
7
+ const rl=readline.createInterface({
8
+ input:process.stdin,
9
+ output:process.stdout
10
+ });
11
+ const askQuestion=(query)=>new Promise((resolve)=>rl.question(query,resolve))
12
+
13
+
14
+
15
+ async function getConfig(){
16
+ if(!fs.existsSync(configPath)){
17
+ console.log("Please Enter your api key");
18
+ const answer=await askQuestion("Paste it here: ");
19
+ rl.close()
20
+ const config = {
21
+ provider: 'groq',
22
+ apiKey: answer,
23
+ model: 'llama-3.3-70b-versatile',
24
+ baseUrl: 'https://api.groq.com/openai'
25
+ };
26
+ fs.writeFileSync(configPath,JSON.stringify(config, null,2))
27
+ return config
28
+ }
29
+ else {
30
+ rl.close();
31
+ const rawData = fs.readFileSync(configPath, 'utf8');
32
+ return JSON.parse(rawData);
33
+ }
34
+ }
35
+
36
+ module.exports = { getConfig };
package/lib/llm.js ADDED
@@ -0,0 +1,53 @@
1
+ async function getComments(code,language,config){
2
+ const prompt=
3
+ `
4
+ Add comments to this ${language} code. add JSDoc/docstrings block above functions and brief inline comments for complex logic. Return ONLY the commented code. NO MARKDOWN. NO EXPLANATION. NO PERSONAL MESSAGE.
5
+ ${code}
6
+ `
7
+
8
+ if(config.provider==='gemini'){
9
+ const url=`https://generativelanguage.googleapis.com/v1beta/models/${config.model}:generateContent?key=${config.apiKey}`;
10
+ const response = await fetch(url,
11
+ {
12
+ method:'POST',
13
+ headers:{'Content-Type':'application/json'},
14
+ body:JSON.stringify({
15
+ "contents":[{"parts":[{"text":prompt}]}]
16
+ })
17
+ }
18
+ );
19
+ const data=await response.json();
20
+ console.log("DEBUG API RESPONSE:", data);
21
+ if (data.error) {
22
+ console.error("\n API Error:", data.error.message);
23
+ process.exit(1);
24
+ }
25
+ let text = data.candidates[0].content.parts[0].text;
26
+ text = text.replace(/^```[\w]*\n/m, '').replace(/```$/m, '').trim();
27
+ return text;
28
+ }
29
+ else{
30
+ const url=`${config.baseUrl}/v1/chat/completions`;
31
+ const response=await fetch(url,{
32
+ method:'POST',
33
+ headers:{'Content-Type':'application/json','Authorization':`Bearer ${config.apiKey}`},
34
+ body:JSON.stringify({ "model": config.model, "messages": [{ "role": "user", "content": prompt }]
35
+ })
36
+
37
+ })
38
+ const data = await response.json();
39
+ console.log("DEBUG API RESPONSE:", data);
40
+ if (data.error) {
41
+ console.error("\n API Error:", data.error.message);
42
+ process.exit(1);
43
+ }
44
+ let text = data.choices[0].message.content;
45
+ text = text.replace(/^```[\w]*\n/m, '').replace(/```$/m, '').trim();
46
+ return text;
47
+
48
+
49
+ }
50
+
51
+ }
52
+
53
+ module.exports={getComments};
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "devsplain",
3
+ "version": "1.0.0",
4
+ "description": "An agent-agnostic CLI tool that automatically adds JSDoc and inline comments to your code using free LLMs.",
5
+ "author": "mwahaj36",
6
+ "license": "MIT",
7
+ "main": "bin/cli.js",
8
+ "type": "commonjs",
9
+ "bin": {
10
+ "devsplain": "./bin/cli.js"
11
+ },
12
+ "files": [
13
+ "bin",
14
+ "lib"
15
+ ],
16
+ "engines": {
17
+ "node": ">=18.0.0"
18
+ },
19
+ "keywords": [
20
+ "cli",
21
+ "ai",
22
+ "comments",
23
+ "jsdoc",
24
+ "documentation",
25
+ "gemini",
26
+ "groq",
27
+ "llm"
28
+ ],
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/mwahaj36/commie.git"
32
+ },
33
+ "bugs": {
34
+ "url": "https://github.com/mwahaj36/commie/issues"
35
+ },
36
+ "homepage": "https://github.com/mwahaj36/commie#readme",
37
+ "scripts": {
38
+ "test": "echo \"Error: no test specified\" && exit 1"
39
+ }
40
+ }