auto-commit-msg icon indicating copy to clipboard operation
auto-commit-msg copied to clipboard

Creating a CLI to interface with the tool

Open Syakhisk opened this issue 2 years ago • 2 comments
trafficstars

Hey, great tools! Unfortunately I don't use vscode. Is there any plan to make this tool usable from the CLI? or do you have any pointers if I want to implement a CLI for this?

Syakhisk avatar Nov 05 '23 13:11 Syakhisk

Hi, thanks for the interest. There is actually a CLI option.

https://github.com/MichaelCurrin/auto-commit-msg/tree/master/shell#autofill-scripts

I am missing pieces that make it easy to bundle and install. However I can send you something so you can try it out yourself, for Linux.

Setup

Make sure /usr/local/bin is your PATH variable

echo $PATH

Then continue.

  1. Go to the v0.26.2 release https://github.com/MichaelCurrin/auto-commit-msg/releases/tag/v0.26.2
  2. Download the .vsix file
  3. Unzip it to for your user.
    sudo apt install unzip
    unzip auto-commit-msg-0.26.2.vsix -d ~/Downloads/auto-commit-msg
    
  4. Create the CLI entry point as below. as acm.sh in /usr/local/bin
  5. Make it executable
    chmod +x /usr/local/bin/acm.sh
    
  6. Test it generates a message. (no commit made here)
    $ cd my-repo
    $ git add ...
    $ acm.sh
    ...
    

Here is the script

#!/usr/bin/env bash
set -e

DIFF_FLAGS='--name-status --find-renames --find-copies --no-color'
CHANGES=$(git diff-index $DIFF_FLAGS HEAD)

MESSAGE=$(node ~/Downloads/auto-commit-msg/extension/out/cli.js "$CHANGES")
echo "$MESSAGE"

Usage

You can run it like this

git commit --edit -m "$(acm.sh)"

And that is of course easier if you add a Bash alias or a Git alias

Here for Git config:

[alias]
    c = '! git commit --edit -m "$(autofill.sh)"'

Then just

git c

MichaelCurrin avatar Nov 18 '23 19:11 MichaelCurrin

If you want an updated version, you'll have to download and unzip whenever there is a new release.

If you have any ideas or can help with making setup and update smoother, that would be great.

Ideas

Rollup

I did some experiments today but got stuck because of the a mix of ES modules and CJS modules and fighting with TypeScript and Rollup.

I found a way to bundle the out/cli.js file the (CLI entry point of my tool) as a single JS file with dependencies, using the Rollup tool and plugins. I need to make a change to a ton of imports to make them use .js explicitly for that to work properly.

Shell script distribution

I am thinking of a pipeline solution to make the .sh file installable with one or more JS files.

At the least using curl.

But there are more professional tools I've seen for making Bash tools easy to install and update.

NPM package

Another idea I had is to do away with the Bash + JS option and instead make my tool installable as an NPM package (I've seen NPM packages come as a binary that was compiled from Go for example).

Then you would install my tool as

npm i auto-commit-msg

And run like

git commit --edit -m "$(npx auto-commit-msg)"

And also this could be expanded to work on Windows if the .sh script I provided worked as .cmd / powershell

MichaelCurrin avatar Nov 18 '23 19:11 MichaelCurrin