# image-converter-cli

> Automatically convert images to a web-optimized format

Latest version **0.1.3** (published 2022-09-01) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install image-converter-cli
pnpm add image-converter-cli
yarn add image-converter-cli
bun add image-converter-cli
```

Provides the command `image-converter`.

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2022-09-01 |
| First published | 2022-08-16 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=12.4.0 |
| Dependencies | 4 |
| Unpacked size | 37.6 KB |
| Known vulnerabilities | 0 (+4 in 1 direct dependencies) |
| Install scripts | no |
| Author | Alexander Allen |
| Maintainers | alexandr0s |
| Keywords | image, converter, CLI, command-line, pre-commit, sharp, JPEG, PNG, BMP, WebP, AVIF, TIFF, GIF, SVG, JP2, DZI, TypeScript |

## Links

- npm: https://www.npmjs.com/package/image-converter-cli
- Repository: https://github.com/AlexanderTheGrey/image-converter-cli
- Homepage: https://github.com/AlexanderTheGrey/image-converter-cli#readme
- Issues: https://github.com/AlexanderTheGrey/image-converter-cli/issues
- npm.io page: https://npm.io/package/image-converter-cli

## Dependencies (4)

- [sharp](https://npm.io/package/sharp.md) ^0.30.7
- [globby](https://npm.io/package/globby.md) ^13.1.2
- [commander](https://npm.io/package/commander.md) ^9.4.0
- [@vingle/bmp-js](https://npm.io/package/@vingle/bmp-js.md) ^0.2.5

## Alternatives

- [@salesforce/cli](https://npm.io/package/@salesforce/cli.md) — 389.7K weekly downloads
- [@mintlify/cli](https://npm.io/package/@mintlify/cli.md) — 208.9K weekly downloads
- [@grafana/e2e-selectors](https://npm.io/package/@grafana/e2e-selectors.md) — 128.7K weekly downloads
- [mintlify](https://npm.io/package/mintlify.md) — 112.0K weekly downloads
- [@intlayer/cli](https://npm.io/package/@intlayer/cli.md) — 22.8K weekly downloads

## Recent versions

- 0.1.3 (latest) — 2022-09-01
- 0.1.2 — 2022-08-19
- 0.1.1 — 2022-08-16
- 0.1.0 — 2022-08-16

## README

# Image Converter CLI

A TypeScript command-line based image converter used to convert JPEG, PNG, GIF, TIFF, BMP, and SVG images to the web-optimized WebP format. It's primarily designed for use in pre-commit hooks.

## Install

```
npm install image-converter-cli
```

## Usage

### Command-line

```
image-converter-cli -i <image-glob> [OPTIONS]...
```

<span style="color:#2B65EC"></span>

### Module

<details>
  <summary><b><u><font size="+1">ECMAScript</font></u></b></summary>

```TypeScript
import { ImageConverter }

const argv = [
    "node",
    "image-converter",
    "-i",
    "<image-glob>",
    "OPTION 1",
    "OPTION 2",
    "OPTION 3..."
];

/*
If using staged files, populate .image-converter-temp/.staged_files

 If using exclusions file, populate .image-converter-temp/.converter_exclusions
 */

const imageConverterObj = new ImageConverter(argv);
await imageConverterObj.runProgram();
```

</details>

<br>

<details>
  <summary><b><u><font size="+1">CommonJS and ECMAScript using a dynamic import</font></u></b></summary>

```TypeScript
(async () => {
	const { imageConverter } = await import("ImageConverter");

    const argv = [
        "node",
        "image-converter",
        "-i",
        "<image-glob>",
        "OPTION 1",
        "OPTION 2",
        "OPTION 3..."
    ];

    /*
    If using staged files, populate .image-converter-temp/.staged_files

    If using exclusions file, populate .image-converter-temp/.converter_exclusions
    */

    const imageConverterObj = new ImageConverter(argv);
    await imageConverterObj.runProgram();
})();
```

</details>

<br>

## Use in a pre-commit hook

<details>
  <summary><b><u><font size="+1">Example of command-line use in a POSIX shell script (Apache-2.0 License)</font></u></b></summary>

```sh
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
reject_commit=0
code_change=0
converterDir=./.image-converter-temp
bold=$(tput bold)
red=$(tput setaf 1)
redbg=$(tput setab 1)
green=$(tput setaf 2)
blue=$(tput setaf 4)
bluebg=$(tput setab 4)
yellow=$(tput setaf 3)
cyan=$(tput setaf 6)
normal=$(tput sgr0)
mkdir -p "$converterDir/"
git diff --name-status --staged --diff-filter=AMR | awk -F '\t' '$1 ~ /(A|M)/ {print $2}; $1 ~ /R100/ {print $3};' > "$converterDir/.staged_files"
sed -e '/^[[:blank:]]*#.*$/d' -e '/^[[:blank:]]*$/d' "./.converterignore" > "$converterDir/.converter_exclusions"
image-converter -i './{src,public}/**/*.(jpeg|jpg|jpe|jif|jfif|jfi|png|gif|tiff|tif|bmp|webp|heif|heifs|heic|heics|avci|avcs|avif|avifs)' -c './{src,public}/**/*.(js|vue|html)'
rm -f "$converterDir/.staged_files" "$converterDir/.converter_exclusions"
if [ -e "$converterDir/.conversion" ]; then
    printf "\n%s\n" "${blue}Searching for non-WebP image extensions in code files...${normal}"
    rm -f "$converterDir/.conversion"
    current_date=$(date +"%Y-%m-%d_%H:%M:%S")
    mkdir -p "./.image_backup/$current_date"
    while read -r imagePath; do
        git restore --staged "$imagePath"
        mv -- "$imagePath" "./.image_backup/$current_date"
    done < "$converterDir/.converted_source_image_files"
    if [ -e "$converterDir/.code_search_files" ]; then
        sed -i.bak -E 's/[^a-zA-Z 0-9]/\\&/g' "$converterDir/.converted_source_image_files"
        rm -f "$converterDir/.converted_source_image_files.bak"
        while read -r imagePath; do
            basename="${imagePath##*/}"
            filenameTemp="${basename%.*}"
            filename="${filenameTemp%\\}"
            ext="${basename##*.}"
            while read -r codePath; do
                sed -i.bak -E "s/($filename).$ext/\1\.webp/g w $converterDir/.changes" "./$codePath"
                if [ -s "$converterDir/.changes" ]; then
                    printf "%s\n\n" "${cyan}Updated $codePath${normal}"
                    if [ $code_change -eq 0 ]; then
                        mkdir -p "./.code_backup/$current_date"
                        code_change=1
                    fi
                    mv -- "./$codePath.bak" "./.code_backup/$current_date/${codePath##*/}"
                else
                    rm -f "./$codePath.bak"
                fi
            done < "$converterDir/.code_search_files"
        done < "$converterDir/.converted_source_image_files"
    fi
    rm -f "$converterDir/.converted_source_image_files" "$converterDir/.converted_target_image_files" "$converterDir/.code_search_files" "$converterDir/.changes"
    if [ $code_change -eq 1 ]; then
        if ! [ -e "$converterDir/.converter_errors" ]; then
            printf "%s\n" "${green}Successfully converted images and changed their references in code.${normal}"
        else
            printf "%s\n" "${green}Successfully converted some images and changed their references in code.${normal}"
        fi
        printf "\n%s\n%s\n\n" "${cyan}Original image files located at:" "$PWD/.image_backup/$current_date/${normal}"
        printf "\n%s\n%s\n\n" "${cyan}Original code files located at:" "$PWD/.code_backup/$current_date/${normal}"
        printf "%s\n" "${bold}${bluebg}Review the converted images/code changes and re-commit.${normal}"
        printf "%s\n" "${bold}${blue}Use \"git diff\" to examine changes.${normal}"
    else
        printf "%s\n" "${yellow}No code files need updating.${normal}"
        if ! [ -e "$converterDir/.converter_errors" ]; then
            printf "\n%s\n" "${green}Successfully converted images.${normal}"
        else
            printf "\n%s\n" "${green}Successfully converted some images.${normal}"
        fi
        printf "\n%s\n%s\n\n" "${cyan}Original image files located at:" "$PWD/.image_backup/$current_date/${normal}"
        printf "%s\n" "${bold}${bluebg}Review the converted images and re-commit.${normal}"
    fi
    reject_commit=1
fi
if [ -e "$converterDir/.converter_errors" ]; then
    printf "\n%s\n" "${bold}${red}Errors occured:${normal}"
     while read -r errorImagePath; do
        printf "%s\n" "${red}$errorImagePath${normal}"
        git restore --staged $errorImagePath
     done < "$converterDir/.converter_errors"
    printf "%s\n\n" "${bold}${redbg}Fix errors and re-commit.${normal}"
    rm -f "$converterDir/.converter_errors"
    reject_commit=1
fi
rm -rf "$converterDir/"
if [ $reject_commit -eq 1 ]; then
    exit 1
else
    npx pretty-quick --staged
    npm run lint
    # Do anything else that normally would occur in the pre-commit hook
fi
```

</details>

<br>

## Ignoring Files

The root directory can contain a <span style="color:#2B65EC">.converterignore</span>, which uses the same syntax as <span style="color:#2B65EC">.gitignore</span>. The contents of this must be copied to <span style="color:#2B65EC">.image-converter-temp/.converter_exclusions</span> (with comments removed, if applicable) for the image converter to know about it.

## Temporary Files

A temporary folder, <span style="color:#2B65EC">.image-converter-temp</span> is created in the project's root directory. There are seven temporary files located within this directory to be used in the pre-commit hook:

## <span style="color:#2B65EC">.conversion</span>

A file created when a successful conversion happens.

## <span style="color:#2B65EC">.code_search_files</span>

Created only if the `-c, --code-glob` option was used. Contains the code files that the code glob found. The pre-commit script can search for relevant extensions and replace them.

## <span style="color:#2B65EC">.staged_files</span>

This file needs to be populated by your pre-commit script (if not overriden). POSIX shell example for getting all added, modified, and renamed files and copying them to <span style="color:#2B65EC">.staged_files</span> file within the temporary directory:

```sh
git diff --name-status --staged --diff-filter=AMR | awk -F '\t' '$1 ~ /(A|M)/ {print $2}; $1 ~ /R100/ {print $3};' > "./.image-converter-temp/.staged_files"
```

## <span style="color:#2B65EC">.converter_exclusions</span>

The contents of <span style="color:#2B65EC">.converterignore</span> should be copied to the image-conveter-temp directory by your pre-commit hook as <span style="color:#2B65EC">.converter_exclusions</span> (unless `--override-excluded-files` is used). POSIX shell example that removes all comment lines which start with "#" in <span style="color:#2B65EC">.converterignore</span> and copies the output to <span style="color:#2B65EC">.converter_exclusions</span> file within the temporary directory:

```sh
sed -e '/^[[:blank:]]*#.*$/d' -e '/^[[:blank:]]*$/d' "./.converterignore" > "./.image-converter-temp/.converter_exclusions"
```

## <span style="color:#2B65EC">.converted_source_image_files</span>

Contains the source images of successfully converted files.

## <span style="color:#2B65EC">.converted_target_image_files</span>

Contains the successfully converted files.

## <span style="color:#2B65EC">.converter_errors</span>

Contains all images that experienced errors when converting.

## CLI Options

### `-v, --version`

Output the version number.

### `-i, --image-glob <value>`

Glob to search image files.

### `-c, --code-glob <value>`

Glob to search code files.

### `--process-svg-files`

Process SVG files.

### `--override-staged-files`

Don't exclude unstaged files.

### `--override-excluded-files`

Don't exclude files from exclusions list.

### `--create-fallback-image`

Create a PNG/GIF file if a JPEG, PNG, GIF, TIFF, or BMP file doesn't exist.

### `-h, --help`

Display help for command.

## Example Run

```sh
image-converter -i './{src,public}/**/*.(jpeg|jpg|jpe|jif|jfif|jfi|png|gif|tiff|tif|bmp|webp|heif|heifs|heic|heics|avci|avcs|avif|avifs)' -c './{src,public}/**/*.(js|vue|html)'
```

#### Note: Glob options must be single quoted to avoid interpretation by the shell

---
_Source: https://npm.io/package/image-converter-cli · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
