A comprehensive library for mime-type mapping
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Shell interface for MIME type operations, supporting both type-to-extension and extension-to-type lookups with comprehensive flag support.
The mime command is automatically available after installing the package:
npm install mime
# or
npm install -g mime # for global accessGet MIME type for a file path or extension.
mime [path_or_extension]Usage Examples:
# File extensions
mime js # text/javascript
mime json # application/json
mime png # image/png
# File paths
mime script.js # text/javascript
mime data/config.json # application/json
mime images/photo.jpg # image/jpeg
mime package.tar.gz # application/gzip
# Complex paths
mime "path with spaces/file.txt" # text/plain
mime ./src/index.ts # text/typescriptGet default extension for a MIME type using the --reverse flag.
mime --reverse [mime_type]
mime -r [mime_type]Usage Examples:
# Standard MIME types
mime -r text/plain # txt
mime -r application/json # json
mime -r image/jpeg # jpeg
mime -r text/javascript # js
# MIME types with parameters (automatically handled)
mime -r "text/html; charset=utf-8" # html
mime --reverse "application/json; charset=utf-8" # jsonDisplay package version information.
mime --version
mime -v
mime --vOutput Example:
4.0.7Display the package name.
mime --name
mime -n
mime --nOutput Example:
mimeDisplay comprehensive usage help.
mime --help
mime -h
mime --hHelp Output:
mime - A comprehensive library for mime-type mapping
Usage:
mime [flags] [path_or_extension]
Flags:
--help, -h Show this message
--version, -v Display the version
--name, -n Print the name of the program
--reverse, -r Print the extension of the mime type
Note: the command will exit after it executes if a command is specified
The path_or_extension is the path to the file or the extension of the file.
Examples:
mime --help
mime --version
mime --name
mime -v
mime --reverse application/text
mime src/log.js
mime new.py
mime foo.shThe CLI uses standard exit codes for scripting integration:
# Success (type/extension found)
mime script.js # Exit code: 0, Output: text/javascript
mime -r text/plain # Exit code: 0, Output: txt
# Failure (type/extension not found)
mime unknown_extension # Exit code: 1, No output
mime -r unknown/mimetype # Exit code: 1, No output
# Info commands always succeed
mime --version # Exit code: 0
mime --help # Exit code: 0#!/bin/bash
# Check if file has expected MIME type
file="data.json"
expected="application/json"
actual=$(mime "$file")
if [ "$actual" = "$expected" ]; then
echo "File type correct: $actual"
else
echo "Unexpected file type: $actual (expected $expected)"
exit 1
fi# Process all JavaScript files in a directory
for file in src/*.js; do
type=$(mime "$file")
echo "$file: $type"
done
# Find files by MIME type
find . -type f | while read file; do
if [ "$(mime "$file" 2>/dev/null)" = "text/javascript" ]; then
echo "JavaScript file: $file"
fi
done# Process file based on MIME type
file="input.data"
mime_type=$(mime "$file")
case "$mime_type" in
"application/json")
echo "Processing JSON file..."
jq . "$file"
;;
"text/plain")
echo "Processing text file..."
cat "$file"
;;
"image/"*)
echo "Processing image file..."
# image processing logic
;;
*)
echo "Unknown file type: $mime_type"
exit 1
;;
esac# Check if file extension matches MIME type
check_file_consistency() {
local file="$1"
local mime_type=$(mime "$file")
local expected_ext=$(mime -r "$mime_type")
local actual_ext="${file##*.}"
if [ "$actual_ext" = "$expected_ext" ]; then
echo "✓ $file: consistent ($mime_type)"
else
echo "✗ $file: extension '$actual_ext' doesn't match type '$mime_type' (expected '$expected_ext')"
fi
}
# Usage
check_file_consistency "script.js" # ✓ script.js: consistent (text/javascript)
check_file_consistency "data.js" # ✗ data.js: extension 'js' doesn't match type 'application/json' (expected 'json')# No arguments (tries to get type of empty string)
mime # Exit code: 1, No output
# Invalid file paths (no extension detected)
mime "file_without_extension" # Exit code: 1, No output
mime "path/to/directory/" # Exit code: 1, No output# Invalid MIME type
mime -r "not-a-mime-type" # Exit code: 1, No output
mime -r "" # Exit code: 1, No output
# Missing argument for reverse lookup
mime -r # Uses last argument as MIME typeThe CLI fails silently (no error messages) to facilitate clean scripting:
# Check if command succeeded without capturing output
if mime "unknown.ext" >/dev/null 2>&1; then
echo "MIME type found"
else
echo "MIME type not found"
fiThe CLI is implemented as a thin wrapper around the main MIME library:
bin/cli.jssrc/mime_cli.tsmime libraryThe CLI processes arguments sequentially and exits immediately after executing any flag command, making it suitable for both interactive use and shell scripting.