devsplain 1.5.3 → 1.5.4

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.
@@ -3,10 +3,10 @@ const fs = require('fs');
3
3
  const path = require('path');
4
4
  const { spliceComments } = require('./cli');
5
5
 
6
- // Wrap logic in try-catch to prevent blocking the git commit process on failure
6
+ /** Main execution block to detect changes and process documentation generation */
7
7
  try {
8
+ // Prevent recursive loops if the previous commit was an automated documentation commit
8
9
  const lastCommitMsg = execSync('git log -1 --format=%s', { encoding: 'utf8' }).trim();
9
- // Avoid infinite loops if this hook triggered the current commit
10
10
  if (lastCommitMsg === 'docs: auto-generated comments by devsplain') {
11
11
  process.exit(0);
12
12
  }
@@ -15,16 +15,16 @@ try {
15
15
  if (!changedFilesStr) {
16
16
  process.exit(0);
17
17
  }
18
+ // Retrieve list of files modified in the latest commit
18
19
  const changedFiles = changedFilesStr.split(/\r?\n/);
19
20
 
20
- // Define supported file extensions for auto-documentation
21
21
  const validExtensions = [
22
22
  '.js', '.jsx', '.ts', '.tsx', '.html', '.css', '.scss', '.vue', '.svelte',
23
23
  '.py', '.java', '.c', '.cpp', '.cs', '.go', '.rb', '.php', '.rs',
24
24
  '.swift', '.kt', '.dart', '.sh'
25
25
  ];
26
26
 
27
- // Filter changed files to include only supported extensions that currently exist
27
+ // Filter for supported file extensions and ensure the file still exists
28
28
  const filesToComment = changedFiles.filter(file => {
29
29
  const ext = path.extname(file).toLowerCase();
30
30
  return validExtensions.includes(ext) && fs.existsSync(file);
@@ -36,6 +36,7 @@ try {
36
36
 
37
37
  console.log(`[devsplain] Found ${filesToComment.length} file(s) in the last commit to auto-comment.`);
38
38
 
39
+ // Parse command line arguments to determine documentation verbosity mode
39
40
  const args = process.argv.slice(2);
40
41
  let modeFlag = '';
41
42
  if (args.includes('--light')) modeFlag = ' --light';
@@ -43,15 +44,14 @@ try {
43
44
 
44
45
  let commentedAny = false;
45
46
 
46
- // Iterate through each changed file to check if content actually changed
47
+ // Process each valid file to determine if documentation needs updating
47
48
  for (const file of filesToComment) {
48
49
  try {
49
50
  const ext = path.extname(file).toLowerCase();
50
- // Retrieve current file content from filesystem
51
51
  const contentHead = fs.readFileSync(file, 'utf8');
52
52
  let contentPrev = '';
53
- // Attempt to fetch the version of the file from the previous commit for comparison
54
53
  try {
54
+ // Attempt to fetch the file content from the previous commit state for comparison
55
55
  contentPrev = execSync(`git show HEAD~1:"${file}"`, {
56
56
  encoding: 'utf8',
57
57
  stdio: ['ignore', 'pipe', 'ignore']
@@ -60,9 +60,10 @@ try {
60
60
  }
61
61
 
62
62
  if (contentPrev) {
63
- // Strip existing comments to determine if the logic changed or just documentation
63
+ // Strip comments from head and previous versions to detect if logic actually changed
64
64
  const cleanHead = spliceComments(contentHead, [], 'clean', ext);
65
65
  const cleanPrev = spliceComments(contentPrev, [], 'clean', ext);
66
+ // Skip processing if only comments were modified in the commit
66
67
  if (cleanHead === cleanPrev) {
67
68
  console.log(`[devsplain] Skipping ${file}: commit contains only comment changes.`);
68
69
  continue;
@@ -73,20 +74,20 @@ try {
73
74
 
74
75
  console.log(`[devsplain] Automatically commenting file: ${file}`);
75
76
  try {
76
- // Invoke the CLI tool to generate comments for the changed file
77
- execSync(`node bin/cli.js "${file}" --force${modeFlag}`, { stdio: 'inherit' });
77
+ // Execute the CLI generator for the specific file
78
+ const cliPath = path.join(__dirname, 'cli.js');
79
+ execSync(`node "${cliPath}" "${file}" --force${modeFlag}`, { stdio: 'inherit' });
78
80
  commentedAny = true;
79
81
  } catch (err) {
80
82
  console.warn(`[devsplain] Warning: Failed to comment ${file}: ${err.message}`);
81
83
  }
82
84
  }
83
85
 
84
- // If changes were made, stage and commit them automatically to the current branch
86
+ // If changes were made by the generator, stage and commit the result back to the repository
85
87
  if (commentedAny) {
86
88
  const status = execSync('git diff --name-only', { encoding: 'utf8' }).trim();
87
89
  if (status.length > 0) {
88
90
  console.log('[devsplain] Staging and committing auto-generated comments...');
89
- // Use --no-verify to prevent triggering this hook recursively during the commit
90
91
  execSync('git commit -am "docs: auto-generated comments by devsplain" --no-verify', { stdio: 'inherit' });
91
92
  console.log('[devsplain] Comments committed successfully! Rollback via: git reset --hard HEAD~1');
92
93
  }
package/bin/setup-hook.js CHANGED
@@ -4,12 +4,12 @@ const { execSync } = require('child_process');
4
4
  const readline = require('readline');
5
5
 
6
6
  /**
7
- * Initializes and installs Git pre-commit and post-commit hooks.
8
- * Prompts the user for documentation mode configuration.
7
+ * Orchestrates the installation of Git pre-commit and post-commit hooks.
8
+ * Detects the local .git directory and configures hooks with user-specified mode.
9
9
  */
10
10
  async function installHooks() {
11
11
  try {
12
- // Resolve the root .git directory path
12
+ // Determine the path to the .git directory
13
13
  const gitDir = execSync('git rev-parse --git-dir', { encoding: 'utf8' }).trim();
14
14
  const hooksDir = path.join(gitDir, 'hooks');
15
15
  if (!fs.existsSync(hooksDir)) {
@@ -17,13 +17,13 @@ async function installHooks() {
17
17
  }
18
18
 
19
19
  let modeChoice = '1';
20
- // Interactive mode requires a TTY terminal
20
+ // Interact with user via terminal to select documentation verbosity
21
21
  if (process.stdout.isTTY) {
22
22
  const rl = readline.createInterface({
23
23
  input: process.stdin,
24
24
  output: process.stdout
25
25
  });
26
- // Promisify the readline interface for async/await flow
26
+ // Promisify readline to allow async flow control
27
27
  const askQuestion = (query) => new Promise((resolve) => rl.question(query, resolve));
28
28
 
29
29
  console.log('\nSelect default commenting mode for Git commits:');
@@ -35,7 +35,7 @@ async function installHooks() {
35
35
  rl.close();
36
36
  }
37
37
 
38
- // Determine documentation style flags based on user input
38
+ // Map selected mode to command-line arguments for the post-commit script
39
39
  let modeArgs = '';
40
40
  if (modeChoice === '2') {
41
41
  modeArgs = ' --light';
@@ -43,7 +43,7 @@ async function installHooks() {
43
43
  modeArgs = ' --full';
44
44
  }
45
45
 
46
- // Write the pre-commit script to the git hooks directory
46
+ // Create the executable pre-commit script
47
47
  const preCommitHookPath = path.join(hooksDir, 'pre-commit');
48
48
  const preCommitContent = `#!/bin/sh
49
49
  # devsplain native pre-commit hook
@@ -56,15 +56,19 @@ npm test || exit 1
56
56
  fs.chmodSync(preCommitHookPath, 0o755);
57
57
  } catch (err) {}
58
58
 
59
- // Write the post-commit script that triggers documentation generation
59
+ // Locate the source script for post-commit actions
60
+ const postCommitScript = path.join(__dirname, 'post-commit.js').replace(/\\/g, '/');
61
+
62
+ // Create the executable post-commit script that calls the documentation engine
60
63
  const postCommitHookPath = path.join(hooksDir, 'post-commit');
61
64
  const postCommitContent = `#!/bin/sh
62
65
  # devsplain native post-commit hook
63
66
  echo "Auto-generating comments for files in the last commit..."
64
- node bin/post-commit.js${modeArgs} || exit 1
67
+ node "${postCommitScript}"${modeArgs} || exit 1
65
68
  `;
66
69
  fs.writeFileSync(postCommitHookPath, postCommitContent);
67
70
  try {
71
+ // Ensure the hook file is executable
68
72
  fs.chmodSync(postCommitHookPath, 0o755);
69
73
  } catch (err) {}
70
74
 
@@ -74,7 +78,6 @@ node bin/post-commit.js${modeArgs} || exit 1
74
78
  }
75
79
  }
76
80
 
77
- // Execute the function automatically if the file is run directly
78
81
  if (require.main === module) {
79
82
  installHooks();
80
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "devsplain",
3
- "version": "1.5.3",
3
+ "version": "1.5.4",
4
4
  "description": "An agent-agnostic CLI tool that automatically adds JSDoc and inline comments to your code using free LLMs.",
5
5
  "author": "mwahaj36",
6
6
  "license": "MIT",