|
| 1 | +import type { ImageFormat } from '@srcset/core' |
| 2 | +import { |
| 3 | + rest, |
| 4 | + alias, |
| 5 | + autocase, |
| 6 | + flag, |
| 7 | + option, |
| 8 | + readOptions |
| 9 | +} from 'argue-cli' |
| 10 | +import type { SrcSetCliRule } from './types.ts' |
| 11 | + |
| 12 | +export const usage = `srcset [...sources] [...options] |
| 13 | +
|
| 14 | + sources Source image(s) glob patterns. |
| 15 | + --help, -h Print this message. |
| 16 | + --verbose, -v Print processed images. |
| 17 | + --match, -m Glob pattern(s) or media query(ies) to match images by name or size. Multiple values must all match. |
| 18 | + --width, -w Output image(s) widths to resize, value less than or equal to 1 is treated as a multiplier. |
| 19 | + --format, -f Output image(s) formats to convert. |
| 20 | + --skip-optimization Do not optimize output images. |
| 21 | + --no-scaling-up Do not generate images larger than the source. |
| 22 | + --dest, -d Destination directory. |
| 23 | + --config, -c Config file path. Defaults to the \`srcset.config.js\` lookup. |
| 24 | + --concurrency Concurrency limit. |
| 25 | +` |
| 26 | + |
| 27 | +export interface CliArgs { |
| 28 | + help: boolean |
| 29 | + verbose: boolean | undefined |
| 30 | + sources: string[] |
| 31 | + rule: SrcSetCliRule | null |
| 32 | + skipOptimization: boolean | undefined |
| 33 | + scalingUp: boolean | undefined |
| 34 | + dest: string | undefined |
| 35 | + config: string | undefined |
| 36 | + concurrency: number | undefined |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Parse command line arguments. |
| 41 | + * @returns Parsed options. |
| 42 | + */ |
| 43 | +export function parseCliArgs(): CliArgs { |
| 44 | + const { |
| 45 | + help, |
| 46 | + verbose, |
| 47 | + match, |
| 48 | + width, |
| 49 | + format, |
| 50 | + skipOptimization, |
| 51 | + scalingUp, |
| 52 | + dest, |
| 53 | + config, |
| 54 | + concurrency |
| 55 | + } = readOptions( |
| 56 | + flag(alias('help', 'h')), |
| 57 | + flag(alias('verbose', 'v')), |
| 58 | + option(alias('match', 'm'), Array), |
| 59 | + option(alias('width', 'w'), Array), |
| 60 | + option(alias('format', 'f'), Array), |
| 61 | + flag(autocase('skipOptimization')), |
| 62 | + flag(autocase('scalingUp')), |
| 63 | + option(alias('dest', 'd'), String), |
| 64 | + option(alias('config', 'c'), String), |
| 65 | + option('concurrency', Number) |
| 66 | + ) |
| 67 | + const rule: SrcSetCliRule = { |
| 68 | + ...match && { |
| 69 | + match |
| 70 | + }, |
| 71 | + ...width && { |
| 72 | + width: width.map(Number) |
| 73 | + }, |
| 74 | + ...format && { |
| 75 | + format: format as ImageFormat[] |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return { |
| 80 | + help: Boolean(help), |
| 81 | + verbose, |
| 82 | + sources: rest(), |
| 83 | + rule: Object.keys(rule).length ? rule : null, |
| 84 | + skipOptimization, |
| 85 | + scalingUp, |
| 86 | + dest, |
| 87 | + config, |
| 88 | + concurrency |
| 89 | + } |
| 90 | +} |
0 commit comments