|
8 | 8 | const { program } = require('commander'); |
9 | 9 | const fs = require('fs'); |
10 | 10 | const path = require('path'); |
11 | | -const { loadSettings, resolveSettings, validateSettings, generateDefaultConfig } = require('./settings'); |
| 11 | +const { minimatch } = require('minimatch'); |
| 12 | +const { loadSettings, resolveSettings, validateSettings, validateOptimizerSettings, generateDefaultConfig } = require('./settings'); |
12 | 13 | const { convertEpub, getOutputPath, cleanup } = require('./converter'); |
| 14 | +const { optimizeEpub } = require('./optimizer'); |
13 | 15 |
|
14 | 16 | program |
15 | 17 | .name('epub-to-xtc') |
@@ -87,6 +89,152 @@ program |
87 | 89 | console.log('\nImportant: Edit the file to set font.path to your TTF/OTF font file.'); |
88 | 90 | }); |
89 | 91 |
|
| 92 | +program |
| 93 | + .command('optimize <input>') |
| 94 | + .description('Optimize EPUB file(s) for e-paper devices') |
| 95 | + .option('-o, --output <path>', 'Output file or directory') |
| 96 | + .option('-c, --config <path>', 'Path to settings JSON file') |
| 97 | + .action(async (input, options) => { |
| 98 | + try { |
| 99 | + const settings = loadSettings(options.config); |
| 100 | + const opts = settings.optimizer || {}; |
| 101 | + |
| 102 | + const errors = validateOptimizerSettings(settings); |
| 103 | + if (errors.length > 0) { |
| 104 | + console.error('Configuration errors:'); |
| 105 | + errors.forEach(e => console.error(` - ${e}`)); |
| 106 | + process.exit(1); |
| 107 | + } |
| 108 | + |
| 109 | + const inputPath = path.resolve(input); |
| 110 | + |
| 111 | + if (!fs.existsSync(inputPath)) { |
| 112 | + console.error(`Input not found: ${inputPath}`); |
| 113 | + process.exit(1); |
| 114 | + } |
| 115 | + |
| 116 | + const stat = fs.statSync(inputPath); |
| 117 | + |
| 118 | + if (stat.isDirectory()) { |
| 119 | + await optimizeDirectory(inputPath, options.output, opts); |
| 120 | + } else if (stat.isFile() && inputPath.endsWith('.epub')) { |
| 121 | + await optimizeSingleFile(inputPath, options.output, opts); |
| 122 | + } else { |
| 123 | + console.error('Input must be an EPUB file or directory containing EPUB files'); |
| 124 | + process.exit(1); |
| 125 | + } |
| 126 | + |
| 127 | + } catch (err) { |
| 128 | + console.error(`Error: ${err.message}`); |
| 129 | + process.exit(1); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | +function formatSize(bytes) { |
| 134 | + return (bytes / 1024).toFixed(1) + ' KB'; |
| 135 | +} |
| 136 | + |
| 137 | +async function optimizeSingleFile(inputPath, outputPath, opts) { |
| 138 | + if (!outputPath) { |
| 139 | + const dir = path.dirname(inputPath); |
| 140 | + const ext = path.extname(inputPath); |
| 141 | + const base = path.basename(inputPath, ext); |
| 142 | + outputPath = path.join(dir, `${base}_optimized${ext}`); |
| 143 | + } else if (fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory()) { |
| 144 | + outputPath = path.join(outputPath, path.basename(inputPath)); |
| 145 | + } else { |
| 146 | + outputPath = path.resolve(outputPath); |
| 147 | + } |
| 148 | + |
| 149 | + const filename = path.basename(inputPath); |
| 150 | + console.log(`Optimizing: ${filename}`); |
| 151 | + |
| 152 | + const result = await optimizeEpub(inputPath, outputPath, opts); |
| 153 | + |
| 154 | + console.log(` Output: ${result.outputPath}`); |
| 155 | + console.log(` Size: ${formatSize(result.originalSize)} -> ${formatSize(result.optimizedSize)} (${result.reductionPercent}% reduction)`); |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * Collect EPUB files from a directory, optionally recursive |
| 160 | + */ |
| 161 | +function collectEpubFiles(dir, opts, basedir) { |
| 162 | + basedir = basedir || dir; |
| 163 | + let results = []; |
| 164 | + const entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 165 | + const include = opts.include || '*.epub'; |
| 166 | + const exclude = opts.exclude || null; |
| 167 | + |
| 168 | + for (const entry of entries) { |
| 169 | + const fullPath = path.join(dir, entry.name); |
| 170 | + const relPath = path.relative(basedir, fullPath); |
| 171 | + |
| 172 | + if (entry.isDirectory() && opts.recursive) { |
| 173 | + results = results.concat(collectEpubFiles(fullPath, opts, basedir)); |
| 174 | + } else if (entry.isFile()) { |
| 175 | + if (!minimatch(entry.name, include)) continue; |
| 176 | + if (exclude && minimatch(entry.name, exclude)) continue; |
| 177 | + results.push({ absolute: fullPath, relative: relPath }); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + return results; |
| 182 | +} |
| 183 | + |
| 184 | +async function optimizeDirectory(inputDir, outputDir, opts) { |
| 185 | + const files = collectEpubFiles(inputDir, opts, inputDir); |
| 186 | + |
| 187 | + if (files.length === 0) { |
| 188 | + console.error('No EPUB files found in directory'); |
| 189 | + process.exit(1); |
| 190 | + } |
| 191 | + |
| 192 | + const inPlace = !outputDir; |
| 193 | + if (!outputDir) { |
| 194 | + outputDir = inputDir; |
| 195 | + } else { |
| 196 | + outputDir = path.resolve(outputDir); |
| 197 | + if (!fs.existsSync(outputDir)) { |
| 198 | + fs.mkdirSync(outputDir, { recursive: true }); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + console.log(`Optimizing ${files.length} EPUB file(s)...\n`); |
| 203 | + |
| 204 | + let successCount = 0; |
| 205 | + let failCount = 0; |
| 206 | + |
| 207 | + for (let i = 0; i < files.length; i++) { |
| 208 | + const file = files[i]; |
| 209 | + // Preserve relative directory structure in output |
| 210 | + let outputPath; |
| 211 | + if (inPlace) { |
| 212 | + // Add _optimized suffix to avoid overwriting originals |
| 213 | + const ext = path.extname(file.relative); |
| 214 | + const base = file.relative.slice(0, -ext.length); |
| 215 | + outputPath = path.join(outputDir, `${base}_optimized${ext}`); |
| 216 | + } else { |
| 217 | + outputPath = path.join(outputDir, file.relative); |
| 218 | + } |
| 219 | + |
| 220 | + console.log(`[${i + 1}/${files.length}] ${file.relative}`); |
| 221 | + |
| 222 | + try { |
| 223 | + const result = await optimizeEpub(file.absolute, outputPath, opts); |
| 224 | + |
| 225 | + console.log(` Output: ${path.basename(result.outputPath)}`); |
| 226 | + console.log(` Size: ${formatSize(result.originalSize)} -> ${formatSize(result.optimizedSize)} (${result.reductionPercent}% reduction)\n`); |
| 227 | + successCount++; |
| 228 | + |
| 229 | + } catch (err) { |
| 230 | + console.log(` Error: ${err.message}\n`); |
| 231 | + failCount++; |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + console.log(`\nOptimization complete: ${successCount} succeeded, ${failCount} failed`); |
| 236 | +} |
| 237 | + |
90 | 238 | async function convertSingleFile(inputPath, outputPath, settings) { |
91 | 239 | // Determine output path |
92 | 240 | if (!outputPath) { |
|
0 commit comments