crapifyme
Version:
Ultra-fast developer productivity CLI tools - remove comments, logs, and more
164 lines (163 loc) • 6.8 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.base64Command = void 0;
const commander_1 = require("commander");
const path_1 = __importDefault(require("path"));
const shared_1 = require("../../shared");
const logic_1 = require("./logic");
exports.base64Command = new commander_1.Command('base64')
.description('Encode images to base64 format or decode base64 strings to files')
.argument('[file]', 'Image file to encode (shorthand for encode command)')
.option('--css-only', 'Output only CSS background-image format')
.option('--data-url-only', 'Output only data URL format')
.option('--raw', 'Output raw base64 string without data URL wrapper')
.option('--size-info', 'Show detailed size analysis (default: true)')
.option('--no-size-info', 'Hide size analysis')
.addHelpText('after', `
Examples:
$ crapifyme base64 image.png # Encode image to base64 (default)
$ crapifyme base64 encode image.jpg # Explicit encode
$ crapifyme base64 decode <base64> -o out.png # Decode base64 to file
$ crapifyme base64 image.svg --css-only # Output only CSS format
$ crapifyme base64 icon.ico --data-url-only # Output only data URL format
$ crapifyme base64 photo.jpg --raw # Output raw base64 only
$ crapifyme base64 image.png --no-size-info # Hide size analysis
Supported formats: png, jpg, jpeg, svg, gif, webp, bmp, ico, tiff, avif
`)
.action(async (file, options, command) => {
if (file) {
await handleEncode(file, options, command);
}
else {
command.help();
}
});
exports.base64Command
.command('encode')
.description('Encode image file to base64 format')
.argument('<file>', 'Image file to encode')
.option('--css-only', 'Output only CSS background-image format')
.option('--data-url-only', 'Output only data URL format')
.option('--raw', 'Output raw base64 string without data URL wrapper')
.option('--size-info', 'Show detailed size analysis (default: true)')
.option('--no-size-info', 'Hide size analysis')
.action(async (file, options, command) => {
await handleEncode(file, options, command);
});
exports.base64Command
.command('decode')
.description('Decode base64 string to file')
.argument('<base64-string>', 'Base64 string or data URL to decode')
.option('-o, --output <path>', 'Output file path (auto-generated if not specified)')
.action(async (base64String, options, command) => {
await handleDecode(base64String, options, command);
});
async function handleEncode(filePath, options, command) {
const globalOptions = command.parent?.parent?.opts() || {};
const logger = new shared_1.Logger(globalOptions.verbose, globalOptions.quiet, globalOptions.json);
try {
const processor = new logic_1.Base64Processor(logger);
processor.validateFilePath(filePath);
if (globalOptions.verbose) {
logger.info(`Encoding: ${filePath}`);
}
const result = await processor.encodeFile(filePath, options);
const stats = {
filesProcessed: 1,
bytesProcessed: result.originalSize,
operationsCompleted: 1,
errors: []
};
if (globalOptions.json) {
logger.json({
...result,
stats
});
}
else {
if (!options.quiet && !globalOptions.quiet) {
logger.success(`Encoded: ${path_1.default.basename(filePath)}`);
if (!options.noSizeInfo) {
console.log(` ┣ Original size: ${processor.formatSize(result.originalSize)}`);
console.log(` ┣ Base64 size: ${processor.formatSize(result.base64Size)}`);
console.log(` ┣ Overhead: ${result.overhead.toFixed(1)}%`);
console.log(` ┗ MIME type: ${result.mimeType}`);
console.log('');
}
}
if (options.raw) {
console.log(result.rawBase64);
}
else if (options.cssOnly) {
console.log(result.cssBackgroundImage);
}
else if (options.dataUrlOnly) {
console.log(result.dataUrl);
}
else {
console.log('Data URL:');
console.log(result.dataUrl);
console.log('');
console.log('CSS Background Image:');
console.log(result.cssBackgroundImage);
(0, shared_1.showComplete)();
}
}
process.exit(shared_1.ExitCode.Success);
}
catch (error) {
logger.error('Encoding failed', error);
process.exit(shared_1.ExitCode.Error);
}
}
async function handleDecode(base64String, options, command) {
const globalOptions = command.parent?.parent?.opts() || {};
const logger = new shared_1.Logger(globalOptions.verbose, globalOptions.quiet, globalOptions.json);
try {
const processor = new logic_1.Base64Processor(logger);
if (globalOptions.verbose) {
logger.info('Decoding base64 string');
}
const result = await processor.decodeBase64(base64String, options.output);
const stats = {
filesProcessed: 1,
bytesProcessed: result.originalSize,
operationsCompleted: 1,
errors: []
};
if (globalOptions.json) {
logger.json({
...result,
stats
});
}
else {
if (!globalOptions.quiet) {
(0, shared_1.showComplete)();
logger.success(`Decoded to: ${result.outputPath}`);
if (globalOptions.verbose) {
console.log(` ┣ Base64 size: ${processor.formatSize(result.originalSize)}`);
console.log(` ┣ Decoded size: ${processor.formatSize(result.decodedSize)}`);
if (result.mimeType) {
console.log(` ┣ MIME type: ${result.mimeType}`);
}
if (result.detectedFormat) {
console.log(` ┗ Format: ${result.detectedFormat}`);
}
}
}
else {
console.log(result.outputPath);
}
}
process.exit(shared_1.ExitCode.Success);
}
catch (error) {
logger.error('Decoding failed', error);
process.exit(shared_1.ExitCode.Error);
}
}
//# sourceMappingURL=index.js.map